Skip to content
Tutorials Center
My learning

Topics

Lesson 80 of 139 ยท Responsive Design

Responsive Design Principles

Learn the core ideas of responsive design in CSS - fluid layouts, flexible images, the viewport meta tag and intrinsic layouts that adapt without breakpoints.

People read your pages on phones held in one hand, on tablets turned sideways, on laptops with a sidebar open, on huge monitors and in browser windows of every odd size in between. You can't design one fixed-width page for all of them. Responsive design is the practice of building one page that adapts to whatever space it gets.

This chapter shows you how. This first lesson covers the principles that do most of the work before you write a single media query: fluid layouts, flexible media, the viewport meta tag, and layouts that size themselves from their content. The lessons after it add media queries, a mobile-first workflow, container queries and fluid typography.

The problem with fixed widths

Early websites were often built at a fixed width, such as 960 pixels, and centered on the screen. On a wide monitor that left big empty margins. On a phone it was worse: the page was wider than the screen, so people had to scroll sideways or zoom in and out to read each line.

The fix is a change of mindset. Instead of asking "how wide is the page?", ask "how should this content behave when the space changes?" Most layouts only need three answers:

  • Grow and shrink with the available space, up to a sensible maximum.
  • Wrap onto new lines or rows when there is no longer room side by side.
  • Never overflow the screen, so there is no sideways scrolling.

The browser already does much of this for you. A plain block element, like a <p> or a <div>, fills the width of its parent automatically, and text wraps by itself. Most responsive bugs come from code that fights those defaults, usually a fixed width in pixels.

Fluid layouts

A fluid layout sizes its boxes relative to the space they are in, instead of in fixed pixels. The most useful pattern is a container that fills the space but stops growing at a comfortable width:

CSS
.container {
  max-width: 60rem;
  margin-inline: auto;
  padding-inline: 1rem;
}

max-width is the key. A width: 60rem box would overflow any screen narrower than that. A max-width: 60rem box is simply as wide as its parent until it reaches 60rem, and then it stops. margin-inline: auto centers it once it has stopped growing, and the inline padding keeps text away from the screen edges on small phones.

Fixed width versus max-width

Drag the divider between the editor and the preview to change the preview's width, and watch the two boxes. The red one causes a horizontal scrollbar as soon as the space gets tight. The green one never does.

Other fluid tools you have already met work the same way:

  • Percentages size a box relative to its container, such as width: 50%.
  • The fr unit in Grid shares out the leftover space.
  • Flexbox lets items grow and shrink with flex (see Flex Item Properties).
  • min(), max() and clamp() set limits on fluid values (see calc(), min(), max() and clamp()).

A handy trick combines two of these: width: min(100%, 30rem) means "full width, but never more than 30rem", in one line.

Flexible images and media

Images have a fixed natural size. A photo that is 1600 pixels wide will happily burst out of a 375-pixel phone screen. One small rule fixes that for every image on the site:

CSS
img, svg, video {
  max-width: 100%;
  height: auto;
}

max-width: 100% lets an image shrink to fit its container but never stretches it beyond its natural size. height: auto keeps the proportions correct when the width shrinks; without it, an image with a height attribute would get squashed. Most modern resets include a rule like this.

Images that fit their container

Notice that the <img> still has width and height attributes. Keep them. The browser uses them to reserve the right amount of space before the image loads, which stops the page from jumping around. The CSS then overrides the actual size. When you need a box with a fixed shape no matter what the image is, reach for aspect-ratio and object-fit, covered in Sizing Images.

For sending smaller image files to smaller screens, which is a performance concern rather than a layout one, see Responsive Images in the HTML course.

The viewport meta tag

There is one line of HTML that every responsive page needs in its <head>:

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

Without it, mobile browsers assume your page was designed for a desktop. They pretend the screen is about 980 pixels wide, lay the page out at that width and then zoom out until it fits. Everything looks tiny, and your media queries see a "desktop" width, so none of your phone styles apply.

width=device-width tells the browser to use the real width of the device as the layout width, and initial-scale=1 starts at normal zoom. The HTML Responsive Design lesson covers this tag in more detail.

Don't block zooming. You may see viewport tags with maximum-scale=1 or user-scalable=no. These stop people from pinching to zoom, which makes the page much harder to use for anyone with low vision. Leave zoom alone.

Intrinsic layouts: let content decide

The newest and most powerful idea in responsive design is intrinsic design: instead of telling the browser exactly when to change a layout, you give it rules and let it work out the layout from the content and the space available. Flexbox and Grid make this possible.

Here is the most famous example. This grid fits as many columns as it can, as long as each column is at least 150px wide, and wraps the rest onto new rows:

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr));
  gap: 1rem;
}

There is no media query anywhere, yet it gives one column on a phone, two or three on a tablet and many on a desktop. The min(150px, 100%) part protects very narrow screens: if the container is narrower than 150px, the column shrinks to fit instead of overflowing. Grid Tracks explains auto-fit and minmax() in full.

Flexbox can do something similar with wrapping, which suits rows of items with different widths, such as buttons or tags.

A layout with no media queries

Resize the preview and watch the cards go from four columns to one, and the tags wrap line by line. Intrinsic layouts should be your first choice. Reach for a media query only when the layout needs a change that rules like these can't express, such as moving a navigation bar into a sidebar.

A responsive checklist

When you build or review a page, run through these questions:

  1. Is the viewport meta tag in the <head>?
  2. Are there fixed widths? Replace width with max-width, or use min(), wherever a box could be wider than a small screen.
  3. Do images and videos shrink? Check for the max-width: 100% rule.
  4. Can layouts wrap? Prefer flex-wrap and auto-fit grids to fixed column counts.
  5. Does long text break? Long URLs and words can overflow on narrow screens; overflow-wrap: anywhere is covered in Wrapping, Overflow and Hyphens.
  6. Does it work at 320px wide? That is a common small-phone width, and also roughly what a desktop page becomes when someone zooms to 400%. Accessibility guidelines ask that content still works there without sideways scrolling.

Test by dragging your browser window, and use the device toolbar in DevTools to try common phone sizes. Then test on a real phone if you can: touch, small text and the shrinking address bar all feel different in the hand. The Viewport Units lesson covers that address bar problem.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is max-width usually better than width for a page container?

    A box with max-width is as wide as its parent until it reaches the limit, so it never overflows small screens. You still need margin-inline auto to center it.
  2. 2. What does the rule img { max-width: 100%; height: auto; } do?

    Images can shrink but never grow beyond their natural size, and height auto keeps the aspect ratio correct as the width changes.
  3. 3. What happens on a phone if you leave out the viewport meta tag?

    Without width=device-width, mobile browsers assume a wide desktop layout of about 980px and shrink it to fit, and your phone media queries never apply.
  4. 4. Which grid creates a responsive number of columns without any media query?

    auto-fit with minmax() fits as many columns as there is room for and wraps the rest, so the column count follows the space available.

Summary

  • Responsive design means one page that adapts to any screen: content grows, shrinks and wraps, and never scrolls sideways.
  • Prefer max-width, percentages, fr, flexbox and min() to fixed pixel widths.
  • Make media flexible with max-width: 100%; height: auto, and keep width and height attributes on images.
  • Every responsive page needs <meta name="viewport" content="width=device-width, initial-scale=1">, and should never disable zooming.
  • Intrinsic layouts, such as auto-fit grids and wrapping flex rows, adapt without breakpoints; add media queries only for changes they can't express.

Next: Media Queries, where you learn to apply styles only when the screen is a certain size or the user has certain preferences.