Code library

Free snippets & functions

Copy, adapt and use them in your own projects.

CSS · Fluid sizes with clamp()Intermediate
h1 { font-size: clamp(1.8rem, 1.2rem + 3vw, 3.5rem); }
.section { padding-block: clamp(2rem, 6vw, 6rem); }
.container { width: min(1100px, 100% - 2rem); margin-inline: auto; }

A value that grows between a minimum and a maximum.

body { display: flex; flex-direction: column; min-height: 100vh; margin: 0; }
.middle { flex: 1; display: flex; }
.middle > nav, .middle > aside { flex: 0 0 200px; }
.middle > main { flex: 1; min-width: 0; }

@media (max-width: 720px) {
  .middle { flex-direction: column; }
  .middle > nav, .middle > aside { flex-basis: auto; }
}

Three columns in the middle, sidebars fixed, content flexible.

CSS · Items that span columnsIntermediate
.gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 140px;
  grid-auto-flow: dense;
  gap: 10px;
}
.gallery .wide { grid-column: span 2; }
.gallery .tall { grid-row: span 2; }
.gallery .big  { grid-column: span 2; grid-row: span 2; }
.gallery img { width: 100%; height: 100%; object-fit: cover; }

Make some items bigger than others.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr));
  gap: 1.25rem;
}

As many columns as fit, each at least 240px.

CSS · Page layout with grid areasIntermediate
.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "head head"
    "side main"
    "foot foot";
  min-height: 100vh;
}
.page > header { grid-area: head; }
.page > aside  { grid-area: side; }
.page > main   { grid-area: main; }
.page > footer { grid-area: foot; }

@media (max-width: 720px) {
  .page { grid-template-columns: 1fr; grid-template-areas: "head" "main" "side" "foot"; }
}

Name the regions and place them like a map.

CSS · Dark mode with CSS variablesIntermediate
:root { --bg: #ffffff; --text: #0b1220; }

@media (prefers-color-scheme: dark) {
  :root { --bg: #0b1220; --text: #eef2fb; }
}

body { background: var(--bg); color: var(--text); }

Define colours once, override them for dark mode.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 24px;
}

Cards fill the row and wrap by themselves.