Responsive Design

Responsive design ensures that a single codebase delivers an optimal experience on phones, tablets, laptops, and large monitors. Modern CSS has evolved well beyond the original media-query approach, giving developers Flexbox, Grid, container queries, and viewport units that make layouts genuinely fluid.

Hub: Graphics & Web Design Related: CSS Techniques | Web Accessibility

Media Queries

Media queries are the original responsive mechanism and remain essential for top-level layout shifts:

/* Mobile-first base styles */
.container {
  padding: 1rem;
}

/* Tablet */
@media (min-width: 768px) {
  .container {
    max-width: 720px;
    margin: 0 auto;
  }
}

/* Desktop */
@media (min-width: 1200px) {
  .container {
    max-width: 1140px;
  }
}

/* Preference queries */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a2e;
    --text: #eaeaea;
  }
}

The Mobile-First Approach

Mobile-first means writing base styles for the smallest viewport and layering on complexity with min-width queries. This approach has two advantages: mobile users download only the CSS they need, and the cascade naturally flows from simple to complex.

/* Base: single column */
.grid { display: grid; gap: 1rem; }

/* Medium screens: two columns */
@media (min-width: 640px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

/* Large screens: three columns */
@media (min-width: 1024px) {
  .grid { grid-template-columns: repeat(3, 1fr); }
}

Fluid Grids with Percentages

Before Grid and Flexbox, percentage-based widths were the primary fluid technique. They still have their place for simple proportional layouts:

.sidebar { width: 30%; float: left; }
.main    { width: 68%; float: right; }

/* Modern equivalent without floats */
.layout {
  display: flex;
  gap: 2%;
}
.layout .sidebar { flex: 0 0 30%; }
.layout .main    { flex: 1; }

Flexbox

Flexbox excels at one-dimensional layouts -- rows or columns of items that need to wrap, align, and distribute space.

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.card-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
}

.card-row > .card {
  flex: 1 1 300px;       /* Grow, shrink, base width 300px */
  min-width: 0;          /* Prevent overflow from long content */
}

Key Flexbox properties at a glance:

Property Purpose
display: flex Establish flex container
justify-content Distribute space along main axis
align-items Align items along cross axis
flex-wrap: wrap Allow items to flow to next line
gap Gutters between items

CSS Grid

CSS Grid is the definitive tool for two-dimensional layouts. It handles rows and columns simultaneously.

/* Fixed column count */
.page {
  display: grid;
  grid-template-columns: 250px 1fr 250px;
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
  min-height: 100dvh;
}

/* Responsive auto-fill with minmax */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1rem;
}

auto-fill vs. auto-fit

Both generate as many columns as will fit. The difference appears when there are few items:

/* auto-fill: keeps empty tracks, items stay at their min width */
.auto-fill {
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

/* auto-fit: collapses empty tracks, items stretch to fill */
.auto-fit {
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

Use auto-fill when you want consistent column widths regardless of item count. Use auto-fit when you want items to expand and fill available space.

The fr Unit

The fr unit distributes remaining space proportionally:

.layout {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;   /* 25% / 50% / 25% of free space */
  gap: 1rem;
}

Viewport Units

Modern viewport units solve the "mobile browser chrome" problem where 100vh was taller than the visible area:

.hero {
  min-height: 100dvh;    /* Dynamic viewport height -- adjusts for browser chrome */
}

.full-width-banner {
  width: 100vw;
  margin-left: calc(-50vw + 50%);
}

.fluid-text {
  font-size: clamp(1rem, 2.5vw, 2rem);
}
Unit Meaning
vw / vh 1% of viewport width / height
dvh / dvw Dynamic viewport (shrinks/grows with browser chrome)
svh / lvh Smallest / largest viewport height

Container Queries

Container queries let a component respond to its own container's size rather than the viewport. This is a paradigm shift for component-based architectures.

/* Step 1: Define a containment context */
.card-wrapper {
  container-type: inline-size;
  container-name: card;
}

/* Step 2: Write queries against the container */
@container card (min-width: 400px) {
  .card {
    display: grid;
    grid-template-columns: 150px 1fr;
    gap: 1rem;
  }
}

@container card (min-width: 600px) {
  .card {
    grid-template-columns: 200px 1fr auto;
  }
  .card .actions {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}

Container queries mean the same .card component adapts whether it sits in a narrow sidebar or a wide main content area, without any knowledge of the overall page layout.

Bringing It Together

A robust responsive strategy layers these tools:

:root {
  --content-max: 1200px;
  --gutter: clamp(1rem, 3vw, 2rem);
}

body {
  margin: 0;
  font-size: clamp(1rem, 1vw + 0.75rem, 1.25rem);
}

.page {
  display: grid;
  grid-template-columns:
    [full-start] minmax(var(--gutter), 1fr)
    [content-start] min(var(--content-max), 100% - var(--gutter) * 2)
    [content-end] minmax(var(--gutter), 1fr)
    [full-end];
}

.page > * {
  grid-column: content;
}

.page > .full-bleed {
  grid-column: full;
}

This creates a centered content column with fluid gutters and allows specific elements to break out to full width. Combined with component-level container queries and Flexbox for inline elements, this approach handles virtually any layout requirement without a single JavaScript calculation.