CSS · Layout

Responsive Design

More than half of web traffic is on phones. Responsive design means one website that looks good on every screen, from a small phone to a wide monitor.

Intermediate 5 min Lesson 13 of 26 Runnable examples
Before you start:FlexboxCSS Grid
0/26 in CSS
On this page · 5 min · Intermediate

Learning objectives#

  • Explain mobile-first design
  • Use fluid widths and max-width
  • Make images and media flexible
  • Use clamp() for fluid font sizes
  • Build layouts that adapt without media queries

Explanation#

Start with the viewport meta tag. Without it, phones pretend to be desktops and shrink your page.

Mobile first: write the base styles for small screens, then add rules for bigger screens. Small screens get the simplest CSS, and it is easier to add columns than remove them.

Fluid sizes: use %, max-width, rem and fr instead of fixed pixel widths. img { max-width: 100%; height: auto; } stops images overflowing.

clamp(min, preferred, max) sets a size that grows with the screen but stays within limits: font-size: clamp(1.5rem, 5vw, 3rem).

Intrinsic layouts adapt without media queries: grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) creates as many columns as fit.

Code example: A layout that adapts on its own#

HTMLA layout that adapts on its own Runnable
<style>
  * { box-sizing: border-box; }
  body { margin: 0; font-family: sans-serif; }
  img { max-width: 100%; height: auto; display: block; }
  .container { width: min(100% - 2rem, 1000px); margin-inline: auto; }
  h1 { font-size: clamp(1.5rem, 5vw, 2.75rem); }
  .grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  }
  .card { border: 1px solid #ddd; border-radius: 8px; overflow: hidden; }
  .card p { padding: 0 12px; }
</style>


<div class="container">
  <h1>Our Destinations</h1>
  <div class="grid">
    <div class="card"><img src="https://picsum.photos/id/1015/400/250" alt="River valley"><p>Valley</p></div>
    <div class="card"><img src="https://picsum.photos/id/1016/400/250" alt="Canyon"><p>Canyon</p></div>
    <div class="card"><img src="https://picsum.photos/id/1018/400/250" alt="Mountains"><p>Mountains</p></div>
  </div>
</div>
Expected output
Cards show three per row on wide screens, two on tablets and one on phones — resize the preview to see it change, with no media queries.
Opens this example in the full playground

Step-by-step explanation#

  1. box-sizing: border-box makes widths include padding and border.
  2. Images never exceed their container.
  3. min(100% - 2rem, 1000px) gives a 1rem gutter on phones and a 1000px max on desktops.
  4. clamp() makes the heading grow smoothly between 1.5rem and 2.75rem.
  5. auto-fit + minmax(200px, 1fr) fits as many 200px+ columns as the width allows.

Real-world use case#

Online stores, news sites and dashboards serve the same HTML to phones and desktops; responsive CSS rearranges it.

Common mistakes#

Fixed pixel widths

width: 1200px causes a horizontal scrollbar on every phone. Use max-width instead.

Forgetting the viewport meta tag

Media queries and fluid layouts appear not to work on phones without it.

Testing only on desktop

Use DevTools device mode (Ctrl+Shift+M) and test on a real phone.

Best practices#

  • Write mobile-first CSS
  • Use max-width instead of width for containers
  • Make all images fluid
  • Prefer intrinsic layouts (auto-fit, flex-wrap) and add media queries only where needed
  • Make tap targets at least 44×44px

Remember this#

Mobile first: design for the smallest screen, then add.

max-width, not width. %, rem and fr, not px.

auto-fit + minmax = columns that count themselves.

Practice exercise#

Your task: Make a .gallery that shows images in as many 150px-minimum columns as fit, with 10px gaps, and fluid images.

Need a hint?
repeat(auto-fit, minmax(150px, 1fr)).
Show solution
CSS Illustrative
.gallery {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.gallery img { max-width: 100%; height: auto; display: block; }

Mini quiz#

Responsive Design quizMultiple choice Question 1 of 3

What does “mobile first” mean?

Score so far: 0/0

Want more? Practise all CSS MCQs · CSS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.