Mobile-first CSS: building up from small screens
Structure your stylesheet the professional way: base styles for phones, min-width queries for bigger screens, fluid type and wide content.
In this lesson you will
- Explain mobile-first design
- Write base styles then min-width queries
- Use fluid type and spacing
- Handle wide content on small screens
About 15 minutes
Start small, grow up
There are two ways to pack a suitcase. You can throw everything in and then remove things to fit a small bag, or you can start with the essentials and add extras if there is room. Mobile-first design is the second way. You write the CSS for the smallest screen first, with no media queries, then add min-width queries to enhance the layout for bigger screens. Almost every professional stylesheet works like this today.
Why mobile-first?
- Phones come first. Most visits come from small screens, so the default is what most people see.
- It forces focus. A small screen has room for one thing at a time. Deciding what matters most leads to a cleaner design.
- The CSS is simpler. The base layout is usually one column, which needs almost no code. You only add rules where space allows more.
- It is lighter on phones. They download and process fewer overriding rules.
- Search engines index the mobile version first, so it must be complete.
Desktop-first versus mobile-first
/* Desktop-first: start wide, undo for small screens */
.cards { display: grid; grid-template-columns: repeat(3, 1fr); }
@media (max-width: 899px) { .cards { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 599px) { .cards { grid-template-columns: 1fr; } }
/* Mobile-first: start narrow, add for bigger screens */
.cards { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 600px) { .cards { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 900px) { .cards { grid-template-columns: repeat(3, 1fr); } }
Both end in the same place. The second reads like a story that builds up: one column, then two, then three. And the base rules contain no desktop assumptions to cancel out.
A mobile-first workflow
- Write the HTML in a logical reading order.
- Style it for a narrow window of about 360 pixels, with no media queries. Single column, readable text, large tap targets.
- Widen the window slowly. When something looks stretched or lonely, add a
min-widthquery there. - In each query, change only what needs changing: columns, spacing, font size, visibility.
- Test at every width in between, and on a real phone.
Write mobile-first CSS: the boxes are stacked by default. Add a min-width: 380px media query that places them in a row with a gap.
<style>
.boxes div { background: #EFEAFD; padding: 18px; margin-bottom: 8px; border-radius: 8px; font-family: Arial; }
/* add a min-width query */
</style>
<div class="boxes"><div>One</div><div>Two</div><div>Three</div></div>
@media (min-width: 380px) { .boxes { display: flex; gap: 8px; } .boxes div { flex: 1; margin-bottom: 0; } }
<style>
.boxes div { background: #EFEAFD; padding: 18px; margin-bottom: 8px; border-radius: 8px; font-family: Arial; }
@media (min-width: 380px) { .boxes { display: flex; gap: 8px; } .boxes div { flex: 1; margin-bottom: 0; } }
</style>
<div class="boxes"><div>One</div><div>Two</div><div>Three</div></div>
Fluid typography and spacing
Instead of redefining font sizes at every breakpoint, let them scale on their own.
:root {
--space: clamp(1rem, 3vw, 2rem);
}
h1 { font-size: clamp(1.75rem, 1.2rem + 3vw, 3rem); }
.section { padding-block: var(--space); }
With clamp() the design glides between its smallest and largest sizes with no jumps. Fewer breakpoints mean less code and fewer bugs. (The var() function reads a custom property, which you learn in the advanced lessons.)
Responsive images
img, video { max-width: 100%; height: auto; }
.thumb { aspect-ratio: 16 / 9; object-fit: cover; width: 100%; }
You learned srcset and picture in the HTML course, which choose the best file for each screen. The CSS above makes sure that whichever file arrives fits its container and keeps its proportions.
Hiding and showing parts
.menu-full { display: none; }
@media (min-width: 900px) {
.menu-full { display: flex; }
.menu-toggle { display: none; }
}
Showing a small button on phones and a full menu on larger screens is a classic. Hiding is fine for navigation extras. Do not hide important content on mobile just because it is awkward. If it matters, make it fit.
Wide content on a narrow screen
.table-wrap { overflow-x: auto; }
pre { overflow-x: auto; }
.break { overflow-wrap: anywhere; }
A wide table can force the whole page to scroll sideways, which feels broken. Wrap it in a container with overflow-x: auto so only the table scrolls. Long words and web addresses can be allowed to break with overflow-wrap.
The container pattern
.container {
width: min(100% - 2rem, 1100px);
margin-inline: auto;
}
One short rule gives you side margins of 1 rem on phones and a centred 1100-pixel column on big screens, with no media queries at all.
Using max-width queries in a stylesheet that is otherwise mobile-first. Mixing both directions makes the cascade hard to follow, and you end up undoing rules you wrote earlier. Choose one direction and stay with it.
Do not name breakpoints after phones or tablets. Name them after the layout change they make, such as --bp-two-columns. Devices change every year. Your content does not.
Practise: a fluid container
Use the container pattern: set width to min(100% – 2rem, 600px), centre it with margin-inline: auto, and give it padding. Then see how it behaves in the preview.
<style>
body { margin:0; background:#E7E3F5; font-family: Arial; padding: 16px 0; }
.container { background:#fff; padding:16px; border-radius:12px; /* width and centring */ }
</style>
<div class="container"><h3>Fluid container</h3><p>I have side margins on small screens and stop growing on big ones.</p></div>
width: min(100% – 2rem, 600px); margin-inline: auto;
<style>
body { margin:0; background:#E7E3F5; font-family: Arial; padding: 16px 0; }
.container { background:#fff; padding:16px; border-radius:12px; width:min(100% - 2rem, 600px); margin-inline:auto; }
</style>
<div class="container"><h3>Fluid container</h3><p>I have side margins on small screens and stop growing on big ones.</p></div>
One line replaced a width, a max-width and two media queries. Small, modern tools like this are what make CSS pleasant once you know them.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does mobile-first mean?
- Designing only for phones
- Writing base CSS for small screens, then adding min-width queries
- Using only max-width queries
- Hiding the desktop
Small screens are the default, larger ones are enhancements.
Which media query type is used in a mobile-first stylesheet?
max-widthmin-widthprintorientation
min-width adds styles as the screen grows.
What does clamp(1rem, 3vw, 2rem) offer?
- A fixed size
- A fluid size between a minimum and a maximum
- A hidden element
- A colour
It scales smoothly and never exceeds the limits.
How should a wide table be handled on a phone?
- Hide it
- Wrap it in a container with
overflow-x: auto - Shrink the font to 4px
- Use position fixed
Let only the table scroll sideways.
Why name breakpoints by layout change instead of device?
- Devices change constantly
- It is required
- It is faster
- Browsers need it
Content-based names stay meaningful.
Summary
- Start with the small-screen layout and add min-width queries.
- clamp() reduces the number of breakpoints needed.
- Make images and videos fluid.
- Let wide content scroll inside its own container.
Questions
Is desktop-first wrong?
No. It works. Mobile-first is usually simpler and lighter, so it is the common choice.
What is the smallest width I should test?
320 to 360 pixels covers nearly all phones.
Should I hide content on mobile?
Only decoration or extras. Keep all important content available.
