Loading...

HTML essentialsAdvanced

Responsive design & the viewport meta tag

6 min lesson

Next
  • Responsive design means a page looks and works well on phones, tablets and desktops without separate sites.
  • The viewport meta tag tells the browser to use the device's real width instead of a pretend desktop width.
  • CSS media queries apply different styles at different widths, like changing columns to a single stack.
  • Flexible images and fluid widths let content fit any screen.
  • Think of it as water in a glass: the same water takes the shape of whatever container it is in.
  • This builds on Media and Responsive images: those controlled files, and this lesson controls layout.
  • The example sets the viewport meta and a media query that stacks two boxes on small screens.
  • Next, Internationalization: lang, dir & encoding adds support for other languages and writing directions.
Keep in mind
  • Always add the viewport meta tag; without it, phones show a tiny desktop page.
  • Design mobile first, then add media queries for larger screens.
  • Use relative units like % and rem instead of fixed pixels for widths.
  • Test by resizing the browser and on a real phone.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <!-- The viewport meta makes phones use their real width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Responsive design</title>
  <style>
    /* By default the two boxes sit side by side */
    .row { display: flex; gap: 12px; }
    .box { flex: 1; padding: 12px; background: #eee; }
    /* On screens narrower than 600px, stack the boxes */
    @media (max-width: 600px) {
      .row { flex-direction: column; }
    }
  </style>
</head>
<body>
  <div class="row">
    <div class="box">Left box</div>
    <div class="box">Right box</div>
  </div>
</body>
</html>

What happens: On wide screens, two light grey boxes labeled "Left box" and "Right box" sit side by side. On screens narrower than 600 pixels, the same boxes stack vertically.

Breaking it down

viewport meta

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

Tells phones to use their real width. Without it, the page is rendered at 980 pixels wide and shrunk, so text looks tiny.

flex layout

.row { display: flex; gap: 12px; }

Places the boxes in a row with space between. Flex layouts adapt to the available width.

media query

@media (max-width: 600px) { ... }

Applies CSS only when the screen is 600 pixels wide or narrower, which is typical for phones.

Responsive tools

ToolPurpose
viewport metaUse the real device width
media queryApply CSS at certain widths
flexboxLet items grow and shrink
max-widthPrevent content from getting too wide
relative unitsSizing that scales with the screen

A helpful analogy

Responsive design is like water poured into different glasses: the same content takes the shape of whatever screen it is in. The viewport meta tag is the instruction that says 'use the real glass size, not a pretend one.'

Nesting rules

  • 1.Put the viewport meta in head on every page.
  • 2.Write mobile styles first, then use min-width media queries to add features for larger screens.

Common mistakes

Forgetting the viewport meta

Avoid

<meta charset="UTF-8"> only

Use instead

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

Phones render the page at desktop width and zoom out, so everything looks tiny and taps miss.

Using fixed pixel widths everywhere

Avoid

.box { width: 800px; }

Use instead

.box { max-width: 800px; width: 100%; }

A fixed width overflows on small screens. A max-width with a fluid width fits every screen.

Quick tips

  • •In DevTools, use the device toolbar to simulate phones and tablets while you design.
  • •Shrink the browser window until the layout breaks; that is exactly where you need a media query.
Previous
Next