Thuta Learning
CSS
AdvancedWeb DevelopmentBeginner

Responsive Intro

CSSLesson 2725 min

What you'll walk away with

  • Explain why a page without the viewport meta tag looks tiny on a phone
  • Make a container fluid with width: min(100% - 32px, 1100px) and margin-inline: auto
  • Stop images and SVGs from overflowing with max-width: 100% and height: auto
  • Scale a heading smoothly with clamp() and wrap cards with repeat(auto-fit, minmax())

Explain it simply

Explain it simply

Responsive web design means one HTML file that rearranges itself so it is easy to read and tap on a laptop, a tablet or a phone. A page that looks great on desktop but makes a phone user scroll sideways usually loses that user to the back button within seconds.

Here is the interesting part: HTML is responsive by default. An unstyled paragraph already rewraps its lines when the window gets narrower. We break that behaviour ourselves the moment we write fixed sizes like width: 960px. This lesson teaches you to describe limits instead of fixed sizes: "at most this wide" and "never wider than the parent".

Problem → Why → Solution

The problem: perfect on a laptop, broken on a phone

The CSS below was written and tested on a 1440px laptop, and there everything looks right. Open the same page on a 375px phone and .page is still 960px wide, so the screen shows roughly 40% of it and the rest needs sideways scrolling. The fixed 960px hero image makes it worse, and long words in the 64px heading spill past the right edge.

If the viewport meta tag is missing, a second problem appears. The phone browser lays the page out at a desktop-like width (commonly around 980px) and zooms out to fit it on the screen, so all the text becomes too small to read.

styles.css
/* Looks perfect on a 1440px laptop */
.page {
  width: 960px;
  margin: 0 auto;
}

.hero img {
  width: 960px;
  height: 400px;
}

h1 {
  font-size: 64px;
}

Why does it break?

width: 960px orders the browser to make the box exactly 960px in every situation. When the screen is narrower, the browser still obeys and leaves the rest for scrolling. Responsive CSS describes limits and relationships instead of exact sizes, such as "at most 1100px, but never wider than the parent".

Core concept: limits instead of fixed sizes

Before media queries (the next lesson), five basics make most pages responsive on their own. Get these right first and you need far fewer breakpoints; the ones you still need go where the content actually breaks, not at a list of device widths.

1. Add the viewport meta tag

Put <meta name="viewport" content="width=device-width, initial-scale=1"> in the <head>. It tells a phone browser not to pretend to be a desktop but to lay out the page at the device's own width. Without it, the media queries from the next lesson also misbehave on phones.

2. Make the container fluid

width: min(100% - 32px, 1100px) takes the smaller of two values. On a 375px screen it is 343px, leaving a 16px gutter on each side; on a 1440px screen it stops at 1100px. margin-inline: auto splits the leftover space evenly so the container sits in the middle. min() accepts math like 100% - 32px directly, without calc().

3. Let media shrink

img, svg, video { max-width: 100%; height: auto; } caps media at the parent's width, and height: auto recalculates the height from the aspect ratio so nothing gets squashed. Keep the width and height attributes in the HTML: the browser uses them to reserve the right space before the image loads.

4. Make type fluid with clamp()

In clamp(1.75rem, 1rem + 4vw, 3rem) the middle value grows with the viewport, while the first and last values are the floor and the ceiling. Because rem is part of the formula, the heading still grows when a user raises the browser's default font size.

5. Let the grid choose the column count

grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) asks the browser how many columns of at least 220px fit. A narrow screen gets one, a wide one gets three or four, and no media query is involved.

Fixed habitResponsive replacement
width: 960pxwidth: min(100% - 32px, 960px)
img { width: 960px; }img { max-width: 100%; height: auto; }
font-size: 64pxfont-size: clamp(2rem, 1rem + 4vw, 4rem)
repeat(3, 300px)repeat(auto-fit, minmax(220px, 1fr))
Mental model: give limits, not orders
  1. Tell phones the real width
  2. Cap widths instead of fixing them
  3. Shrink media with max-width: 100%
  4. Size headings with clamp()
  5. Let the grid pick the column count

Only after these five, drag the window from 320px to wide. Add a media query where something still breaks, and nowhere else.

General pattern

css
.wrapper {
  width: min(100% - var(--gutter) * 2, var(--max-width));
  margin-inline: auto;
}
img, svg, video { max-width: 100%; height: auto; }
.heading { font-size: clamp(var(--min), var(--fluid), var(--max)); }
.list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(var(--item-min), 1fr));
}

The wrapper caps its width and keeps a gutter, media never exceeds its parent, the heading grows between a floor and a ceiling, and the list creates as many columns as fit.

A responsive page with zero media queries

This page contains no media query, yet it never scrolls sideways from a 320px phone up to a wide monitor. The banner is a 1600×400 SVG, so it behaves like a large image without needing an image file. Run it and change the preview width, or narrow your browser window.

index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Responsive basics</title>
  <style>
    * { box-sizing: border-box; }
    body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.5; }

    .container {
      width: min(100% - 32px, 1100px);
      margin-inline: auto;
    }

    img, svg {
      display: block;
      max-width: 100%;
      height: auto;
    }

    h1 {
      font-size: clamp(1.75rem, 1rem + 4vw, 3rem);
    }

    .cards {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
      gap: 16px;
      margin-top: 16px;
    }

    .card { padding: 16px; border: 1px solid #cbd5e1; border-radius: 12px; }
  </style>
</head>
<body>
  <main class="container">
    <h1>Fluid by default</h1>
    <svg viewBox="0 0 1600 400" width="1600" height="400" role="img" aria-label="Banner">
      <rect width="1600" height="400" fill="#93c5fd" />
    </svg>
    <div class="cards">
      <article class="card">Fluid width</article>
      <article class="card">Flexible media</article>
      <article class="card">Fluid type</article>
    </div>
  </main>
</body>
</html>

Code breakdown

  1. Viewport meta tag<meta name="viewport" content="width=device-width, initial-scale=1">

    The phone lays the page out at the device's CSS width. Without this tag it would use a virtual width of around 980px and zoom out, shrinking everything.

  2. Fluid containerwidth: min(100% - 32px, 1100px);

    At 320px this is 320 - 32 = 288px, at 1024px it is 992px, and it only stops at 1100px once the viewport is wider than 1132px.

  3. Flexible mediamax-width: 100%;

    The SVG's width attribute says 1600, but max-width keeps it within the container. With height: auto and the viewBox, the 4:1 ratio survives: 288px wide means 72px tall.

  4. Fluid headingfont-size: clamp(1.75rem, 1rem + 4vw, 3rem);

    At 320px it computes 16 + 12.8 = 28.8px, above the 28px floor, so that value is used. At 1024px the formula gives 56.96px, which the 3rem (48px) ceiling cuts down.

  5. Self-wrapping gridgrid-template-columns: repeat(auto-fit, minmax(220px, 1fr));

    In a 568px container, three columns would need 3×220 + 2×16 = 692px, which does not fit. Two columns (456px) do, so the third card drops to a new row.

You should see
320px: container 288px, banner 288×72, the three cards stacked.
600px: container 568px, two cards in the first row, the third below.
1024px: container 992px, three cards in one row, heading capped at 48px.
No horizontal scrollbar at any width.

Mobile-first, and how to test

Mobile-first means writing the styles for the narrowest screen first and adding to them as space grows. The phone layout is usually the simple one, blocks stacked top to bottom, and normal flow gives you that for free. Growing a simple layout is easier than starting from a complex desktop layout and taking pieces away.

To test, open the device toolbar in your browser's DevTools (Ctrl+Shift+M in Chrome and Edge) and drag the width slowly upward from 320px. Dragging shows you the exact moment content breaks better than picking device names from a list. Also try browser zoom at 200%: zooming narrows the CSS viewport, so you see the layout that people with low vision actually get.

Why 320px matters

WCAG's Reflow criterion (1.4.10) asks that vertically scrolling content be readable at a width of 320 CSS pixels without horizontal scrolling. Treat 320px as the narrowest width you always test.

In real projects

In real projects

Blogs and documentation sites often size the article body with width: min(100% - 2rem, 65ch). The 65ch cap stops each line at roughly 65 characters, so even on a wide monitor the reader's eyes never travel too far. Product grids on e-commerce sites lean on the repeat(auto-fill, minmax(...)) pattern, and design systems commonly define every heading size as a clamp() value, forming a fluid type scale.

The small details matter too. Code blocks inside articles get pre { overflow-x: auto; } so a long line scrolls inside its own box instead of widening the whole page. Many guidelines recommend tap targets of about 44×44 CSS pixels so buttons and links are easy to hit with a finger. Plenty of teams attach a 320px screenshot and a wide-screen screenshot to every pull request that touches layout.

Common mistakes

Common mistakes

Forgetting the viewport meta tag

Testing in a desktop browser shows no difference, so this mistake hides easily. Only on a real phone do you see the whole page zoomed out and your media queries reacting as if the screen were desktop-wide. Put the tag in the <head> of every page template.

Using width: 100vw for full width

100vw is the full viewport width, and on desktop browsers with classic scrollbars it includes the scrollbar. The element ends up about 15px wider than the page and a horizontal scrollbar appears. A block element already fills its parent, so leaving width out or using 100% is the safer choice.

Sizing text with vw alone

font-size: 5vw becomes 16px on a 320px phone and 96px on a 1920px monitor. With no floor or ceiling it is either too small or absurdly large, and it ignores the user's font size setting entirely. Combine clamp() with rem.

Mini challenge

Make an article page fluid

A blog article page has a large figure, some paragraphs and a <pre> block with a very long line of code. Without any media query, write the CSS so the page never scrolls sideways from 320px up to a wide screen.

  • Add the viewport meta tag to the <head>
  • Centre .article with width: min(100% - 2rem, 65ch)
  • Make the h1 grow between 2rem and 3.25rem with clamp()
  • The SVG figure must never exceed the article width and must keep its ratio
  • The long line inside <pre> must scroll inside its box, not widen the page
article.css
/* Markup: <article class="article"> holding an h1, an SVG
   (viewBox 0 0 1200 600), a paragraph and a <pre><code> line.
   Also add the viewport meta tag to the page's <head>. */

.article {
  /* TODO: fluid width capped at 65ch */
  /* TODO: centre it horizontally */
}

h1 {
  /* TODO: clamp() between 2rem and 3.25rem */
}

/* TODO: one rule so media is never wider than its parent */

pre {
  /* TODO: dark background, light text, some padding */
  /* TODO: long lines scroll inside the box */
}

Expected: At 320px the article is 288px wide and the SVG is 288×144. The long code line scrolls inside its box and the page has no horizontal scrollbar. On a wide screen the article stops at 65ch and the h1 tops out at 52px (3.25rem).

Show hint

65ch is the width of 65 "0" characters in the current font. The SVG needs both max-width: 100% and height: auto, and the <pre> needs a single overflow-x: auto.

Show solution
article.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body { margin: 0; font-family: Georgia, serif; line-height: 1.7; }

    .article {
      width: min(100% - 2rem, 65ch);
      margin-inline: auto;
    }

    h1 {
      font-size: clamp(2rem, 1.25rem + 3vw, 3.25rem);
      line-height: 1.2;
    }

    img, svg {
      display: block;
      max-width: 100%;
      height: auto;
    }

    pre {
      background: #0f172a;
      color: #e2e8f0;
      padding: 12px;
      overflow-x: auto;
    }
  </style>
</head>
<body>
  <article class="article">
    <h1>Why fluid layouts win</h1>
    <svg viewBox="0 0 1200 600" width="1200" height="600" role="img" aria-label="Chart">
      <rect width="1200" height="600" fill="#fde68a" />
    </svg>
    <p>Fixed widths look fine until someone opens the page on a phone.</p>
    <pre><code>const url = "https://api.example.com/v1/articles/2026/fluid";</code></pre>
  </article>
</body>
</html>

Quick quiz 1 / 3

With body margin 0 and a 375px-wide viewport, how wide is a container with width: min(100% - 32px, 1100px)?

Quick quiz 2 / 3

What usually happens when a page without the viewport meta tag opens on a phone?

Quick quiz 3 / 3

A 568px-wide container has grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px and three cards. How many cards sit in the first row?

What to remember

  • Add the viewport meta tag to every page; without it phones zoom out from a desktop width
  • Cap widths with min() instead of fixing them, e.g. min(100% - 32px, 1100px)
  • Give img, svg and video max-width: 100% and height: auto
  • Use clamp() so type scales fluidly between a floor and a ceiling
  • A repeat(auto-fit, minmax()) grid changes its column count without any media query

Responsive web design — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Responsive Intro | Thuta Learning