1. What responsive design means
A responsive page adapts its layout to the space available. For example, a row of cards may fit in three columns on a wide screen, two on a medium screen and one on a narrow screen.
Responsive design isn’t just about making things smaller. It’s about keeping content readable and usable as the available space changes.
First, include the viewport meta tag in the <head> of your HTML:
<meta name="viewport" content="width=device-width, initial-scale=1">Without it, some mobile browsers may use a wider virtual layout viewport and scale the page down, making your layout harder to control.
2. Start with a narrow-screen layout
A common approach is mobile-first design: write the base CSS for a narrower screen, then add styles at wider widths when the content needs them.
.cards {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 42rem) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 64rem) {
.cards {
grid-template-columns: repeat(3, 1fr);
}
}The base layout has one column. At wider viewport widths, the media queries add columns.
The values 42rem and 64rem here are examples—not universal device breakpoints. Choose breakpoints by testing your content and adding a change where the layout starts to feel cramped or stretched.
Quick check 1: In a mobile-first layout, do you start with the narrow-screen or wide-screen styles?
3. Media queries
A media query applies CSS only when a condition is true. The most common layout condition is viewport width.
@media (min-width: 48rem) {
.sidebar {
display: block;
}
}This applies the rule at a viewport width of at least 48rem.
You can use media queries to change columns, spacing, font sizes or visibility. Keep changes purposeful: not every element needs a special rule at every width.
Some other useful features include:
@media (orientation: landscape) {
/* Styles for a landscape-oriented viewport */
}
@media (prefers-reduced-motion: reduce) {
/* Adjust non-essential movement */
}4. Fluid sizing between breakpoints
Media queries make layout changes at particular thresholds. Fluid sizing lets values change gradually between them.
Use clamp(minimum, preferred, maximum) to keep a value within a range:
h1 {
font-size: clamp(2rem, 5vw, 4rem);
}The heading scales with the viewport, but won’t become smaller than 2rem or larger than 4rem.
A centred container can grow while keeping a maximum width:
.container {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}This leaves side space on narrow screens and stops the content from becoming excessively wide on larger ones.
Quick check 2: What are the minimum and maximum values in clamp(1rem, 3vw, 2.5rem)?
5. Responsive Grid without a media query
Grid can make a card layout responsive automatically:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1rem;
}The browser fits as many columns as the available space allows. Each column aims to be at least 16rem, but min(100%, 16rem) prevents the minimum from being wider than the container.
Use this when you want cards to reflow naturally without specifying exact column counts at breakpoints. You can still combine it with media queries if your design needs a specific change.
6. Flexible rows with Flexbox
Flexbox can allow items to wrap when there isn’t enough room:
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
}This can work well for groups of controls, navigation items or tags. For layouts that need explicit rows and columns, Grid may be a better fit.
7. Responsive images
Keep images inside their containers and preserve their proportions:
img {
display: block;
max-width: 100%;
height: auto;
}The image can shrink to fit its container, while height: auto preserves its aspect ratio.
If you need to provide different image sources for different situations, HTML offers srcset and the <picture> element. For this lesson, focus on the basic rule above and check that images don’t cause horizontal overflow.
8. Usability at different sizes
As you resize and test a page, check more than the layout:
- Text should remain readable; users should be able to zoom.
- Buttons and links should be comfortable to tap and not crowded together.
- Keyboard focus should remain visible.
- Layout changes should preserve a sensible reading and navigation order.
- Don’t rely on colour alone to communicate important information.
- Avoid unnecessary motion, and respect reduced-motion preferences.
For example, you can reduce non-essential transitions when a user has requested less motion:
@media (prefers-reduced-motion: reduce) {
.animated-element {
animation: none;
transition: none;
}
}Apply this to the parts of your page where motion is optional. Don’t remove feedback that people need to understand an interaction.
9. Test the layout
Use your browser’s responsive design tools or resize the browser window. Test:
- A narrow width.
- A medium width.
- A wide width.
- Widths just before and after each breakpoint.
- Zoomed-in content or larger text.
- Keyboard navigation and visible focus.
Look for horizontal scrolling, overlapping content, cramped controls, images that distort, and lines of text that become uncomfortably long. Testing only at preset phone and desktop sizes can miss problems between them.
Exercises
Try these before opening the answer key.
1. Write the viewport meta tag that belongs in the document <head>.
2. Your .cards layout starts with one column and changes to two columns at 48rem. Write the base rule and media query.
3. Write a heading size that scales fluidly from 1.75rem to 3.5rem, using 5vw as the preferred value.
4. Create a centred content container that has 2rem total side space on narrow screens and a maximum width of 70rem.
5. What does flex-wrap: wrap let a Flexbox row do?
6. Write CSS to keep images from overflowing their containers without distorting their proportions.
7. What should guide your choice of a breakpoint?
8. Build it. Make a card page that starts with one column, uses two columns when the cards have enough room, and has a maximum-width content container. Add a responsive image and test at narrow, medium and wide widths.
9. Check accessibility. Navigate your page using only the keyboard. Zoom in and check that the content remains usable. If you’ve added animation, test your reduced-motion rule.
Free resources
- MDN Web Docs: search for Using media queries, CSS values and units and Responsive images for explanations and examples.
- Browser DevTools: use responsive design mode to test different viewport widths and inspect layout issues.
- Your own page: test widths just before and after each breakpoint; this often reveals problems that device presets miss.
Key takeaways
- Start with a usable base layout, then add breakpoints where the content needs a change.
-
Use
clamp(),min()andminmax()to create fluid sizes within sensible limits. - Grid and Flexbox can adapt layouts by changing columns or wrapping.
-
max-width: 100%andheight: autohelp images fit without distortion. - Test intermediate widths, zoom, keyboard navigation and motion preferences—not just a phone and desktop preset.
Quick check 1: Start with the narrow-screen styles.
Quick check 2: 1rem is the minimum and 2.5rem is the maximum.
Exercise 1:
<meta name="viewport" content="width=device-width, initial-scale=1">Exercise 2:
.cards {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 48rem) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}Exercise 3:
h1 {
font-size: clamp(1.75rem, 5vw, 3.5rem);
}Exercise 4:
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}Exercise 5: It allows flex items to move onto another line when there isn’t enough space on the current line.
Exercise 6:
img {
max-width: 100%;
height: auto;
}Exercise 7: Let the content guide the breakpoint. Add one where the layout needs to change, rather than assuming one value suits every device.
Exercise 8: A good solution starts with one column, adds a wider layout using a media query or a responsive Grid pattern, and constrains the content with a maximum width. The image should scale inside its container without changing its proportions.
Exercise 9: Check that focus is visible, zoomed content remains usable, and any non-essential animation is reduced when the reduced-motion preference is active.