TExcelTAD
Home › CSS › CSS Responsive Design

CSS Responsive Design

People visit your site on phones, tablets, laptops and huge monitors. Responsive web design means one page that adapts its layout to whatever screen it's on, rather than separate mobile and desktop sites. It combines a few ingredients you already know: fluid sizes, flexible images, Flexbox/Grid and media queries.

The viewport meta tag

Without it, mobile browsers pretend to be ~980px wide and zoom out, making text tiny. Every responsive page needs this in the <head>:

html
<meta name="viewport" content="width=device-width, initial-scale=1">

The building blocks

Technique Example
Fluid widths width: 100%, max-width: 1100px, fr units
Flexible images img { max-width: 100%; height: auto; }
Relative units rem for type, %/vw for widths
Fluid typography font-size: clamp(1.5rem, 5vw, 3rem)
Wrapping layouts flex-wrap: wrap, repeat(auto-fit, minmax(…))
Media queries @media (min-width: 600px) { … }
Container queries @container (min-width: 400px) { … }

Mobile first

Write the base styles for small screens (usually a single column), then add min-width media queries to enhance the layout as space grows. The playground frame is resizable — drag its edge to cross the 500px breakpoint.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  body { font-family: system-ui, sans-serif; margin: 8px; }
  .cols { display: grid; gap: 8px; }                 /* phone: one column */
  .cols div { padding: 16px; border-radius: 8px; color: white; background: #5e35b1; }
  @media (min-width: 500px) {
    .cols { grid-template-columns: 1fr 1fr 1fr; }    /* wider: three columns */
    .cols div { background: #00897b; }
  }
</style>
</head>
<body>
  <div class="cols"><div>One</div><div>Two</div><div>Three</div></div>
  <p>Purple = mobile layout, teal = wide layout.</p>
</body>
</html>

Tip: Pick breakpoints where your content starts to look wrong, not at specific device widths. Devices change every year; your content doesn't.

Layouts that adapt without media queries

Modern CSS can often respond by itself. auto-fit + minmax() creates as many columns as fit; flex-wrap with a flex-basis does the same in Flexbox.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 8px; }
  .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 12px; }
  .grid div { height: 60px; border-radius: 8px; background: #ffa726; display: grid; place-items: center; }
  .flex { display: flex; flex-wrap: wrap; gap: 10px; }
  .flex div { flex: 1 1 180px; padding: 14px; background: #42a5f5; color: white; border-radius: 8px; }
</style>
</head>
<body>
  <div class="grid"><div>A</div><div>B</div><div>C</div><div>D</div></div>
  <div class="flex"><div>Flex item grows</div><div>and wraps when</div><div>below 180px</div></div>
</body>
</html>

Fluid typography with clamp()

clamp(min, preferred, max) lets a heading scale smoothly with the viewport while staying within limits.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: Georgia, serif; margin: 12px; }
  h1 { font-size: clamp(1.4rem, 7vw, 3.2rem); margin: 0 0 8px; color: #c62828; }
  p { font-size: clamp(0.9rem, 2.5vw, 1.15rem); max-width: 60ch; line-height: 1.5; }
</style>
</head>
<body>
  <h1>Grows with the window</h1>
  <p>Resize the frame. The heading scales between 1.4rem and 3.2rem, and the paragraph between 0.9rem and 1.15rem — no breakpoints needed.</p>
</body>
</html>

Responsive images and hiding content

Images should never overflow their container. For art direction (different crops per size) HTML offers <picture> and srcset; CSS handles the sizing. Occasionally you'll hide secondary content on small screens with display: none inside a media query — use this sparingly, since mobile users deserve the same content.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 8px; }
  .card { display: flex; flex-direction: column; gap: 10px; background: #f1f8e9; padding: 10px; border-radius: 10px; }
  .pic { width: 100%; max-width: 100%; aspect-ratio: 16 / 9; border-radius: 8px; background: linear-gradient(135deg, #8bc34a, #009688); }
  .extra { display: none; color: #558b2f; }
  @media (min-width: 520px) {
    .card { flex-direction: row; align-items: center; }
    .pic { width: 200px; }
    .extra { display: block; }
  }
</style>
</head>
<body>
  <div class="card">
    <div class="pic"></div>
    <div><strong>Mountain trip</strong><p class="extra">Extra details appear on wider screens.</p></div>
  </div>
</body>
</html>

Test yourself

What does the viewport meta tag width=device-width do?

  1. Hides the scrollbar
  2. Makes the layout viewport match the device's screen width
  3. Sets body width to 100px
  4. Enables media queries in CSS

In a mobile-first stylesheet, which media query type do you mostly use?

  1. max-width
  2. min-width
  3. orientation only
  4. print

What does clamp(1rem, 4vw, 2rem) return on a very wide screen?

  1. 1rem
  2. 4vw always
  3. 2rem
  4. It is invalid