Building layouts that work on every screen size - the viewport meta tag, media queries, mobile-first development, breakpoints, responsive images, and fluid typography.
Responsive Design
Responsive design means your web page looks good on every screen size - from a small phone to a wide desktop monitor. Instead of building separate mobile and desktop sites, you write one set of HTML and CSS that adapts.
The viewport meta tag
Before writing any responsive CSS, your HTML must include this meta tag in the <head>:
Without it, mobile browsers render the page at a desktop width (typically 980px) and then zoom out to fit the screen. The result is tiny, unreadable text.
This tag tells the browser: "Use the actual device width as the viewport width and start at 1x zoom."
Note: Every responsive page needs this tag. If your page looks tiny on mobile, this is the first thing to check.
Media queries
Media queries let you apply CSS rules only when certain conditions are met, such as the screen being wider or narrower than a threshold:
On screens narrower than 768px, the sidebar is hidden. On screens 768px and wider, it appears.
Syntax
Common conditions:
You can combine conditions:
Mobile-first development
There are two approaches to responsive CSS:
- Desktop-first - write styles for large screens, then use
max-widthqueries to override for smaller screens - Mobile-first - write styles for small screens, then use
min-widthqueries to enhance for larger screens
Mobile-first is the recommended approach. Here is why:
- Mobile styles are simpler (single column, stacked layout)
- You progressively add complexity for larger screens instead of removing it
- Smaller CSS files for mobile users (they only load the base styles)
- Forces you to prioritise content
Mobile-first example
The default is a single column. At 768px, it becomes two columns. At 1024px, three columns.
Common breakpoints
There is no universally "correct" set of breakpoints, but these are widely used:
Tip: Do not obsess over exact breakpoint values. The best breakpoints are where your design breaks - resize the browser and add a breakpoint wherever the layout starts to look bad.
Defining breakpoints in CSS
With custom properties (chapter 13) you cannot use variables inside @media conditions. Instead, document your
breakpoints with comments:
Responsive layout patterns
The stack-to-horizontal pattern
The most common responsive pattern. Items stack on mobile, go side by side on desktop:
The sidebar pattern
Sidebar hidden on mobile, visible on desktop:
The auto-fit grid (no media queries needed)
As covered in chapter 8, this pattern is inherently responsive:
Responsive images
Images should never overflow their container. This base rule handles most cases:
max-width: 100% prevents the image from exceeding its container width. height: auto maintains the aspect ratio.
The HTML picture element
For art direction (showing different images at different sizes), use the <picture> element:
Object-fit for fixed-size images
When images must fit a fixed container, use object-fit:
Responsive typography
Font sizes should scale between breakpoints. Use clamp() for fluid scaling without media queries:
Or use media queries for step-based changes:
Responsive spacing
Scale padding and margins at different breakpoints:
Container width
Prevent content from stretching too wide on large screens:
Testing responsive designs
- Browser DevTools - toggle the device toolbar (Ctrl+Shift+M / Cmd+Shift+M) to simulate different screen sizes
- Resize the browser window - drag the edge of the window and watch your layout adapt
- Test on real devices - simulators are good but real devices reveal issues with touch, scrolling, and viewport quirks
Tip: Always test at sizes between your breakpoints, not just at the breakpoints themselves. The design should look good at every width, not just at 768px and 1024px.
Complete responsive example
Beyond the viewport - container queries
Media queries are ideal for page-level layout. When a reusable component must adapt to the width of its slot (for example a card in a sidebar versus the main column), use container queries instead of assuming the viewport width matches the available space.
You will meet them briefly in Modern CSS Features. For interactive demos and a full comparison with media queries, see CSS Container Queries.
What you learned
- The
<meta name="viewport">tag is required for responsive pages - Media queries apply CSS conditionally based on viewport size (or other conditions)
- Mobile-first means writing base styles for small screens and using
min-widthqueries for larger ones - Breakpoints should be based on where your design breaks, not on specific device sizes
max-width: 100%andheight: automake images responsiveclamp()creates fluid typography that scales smoothly- Use a
.containerwithmax-widthandmargin: 0 autoto constrain content width - Container queries (covered later) let components respond to their own slot size, not only the viewport
Next step
Your layouts are now responsive. The next chapter covers backgrounds, borders, and shadows - the visual effects that add depth and polish to your designs.