Sub topic 4
Writing the small-screen base styles
This chapter assumes you know HTML structure, CSS selectors, the box model and the cascade, and that you can use browser developer tools. It also assumes you have read the earlier chapters on the viewport meta tag and relative units. The base styles are the rules that apply on every screen, before any media query. In mobile-first CSS, you write them for the narrowest screen first.
What belongs in the base
Base styles describe a single-column page that works at about 320 CSS pixels wide. They contain everything a small screen needs and nothing that only a wide screen needs. Typical contents:
- Typography and colors, using
remso text follows the user's font-size setting. - Spacing between and inside blocks, in
remorem. - Single-column stacking of the main regions.
- Touch-friendly sizing for links and buttons.
Block elements already stack in normal flow. Many mobile-first layouts therefore need very little code at the base level. Grid with a gap is a clear way to state the stacking intent.
A worked base layout
Start with the viewport tag in the <head>, then add a stylesheet.
<meta name="viewport" content="width=device-width, initial-scale=1">The next rule sets the base for a page with a main column and a sidebar. On a narrow screen they stack.
/* Base: small screens */
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}There is no @media block yet. This code is the complete small-screen layout. The sidebar appears below the main content because that is the source order. A phone user sees the main content first, which is usually what you want.
Add, don't undo
The key habit is that wider rules add to the base. They do not cancel it. Compare two approaches for the same layout.
/* Desktop-first habit: base is the wide layout, small screens undo it */
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
@media (max-width: 47.99em) {
.layout {
grid-template-columns: none;
padding: 1rem;
}
}Here the small screen has to reset values. Phones download and parse rules they then override. Each override is also something you can forget when you add a new property.
The mobile-first version states the small layout once and adds the wide one:
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}The second version has no reset. If the media query fails to apply, for example because of a typo, the page still shows a usable single column.
Common errors
- Writing the wide layout first and patching it down. This is the desktop-first pattern above. It produces
max-widthresets and long override chains. - Fixed widths in the base. A rule like
width: 600pxon a container makes a 360px phone scroll sideways. Usemax-widthor let the element fill its parent. - Putting the sidebar first in the source. The base layout follows source order, so the sidebar would sit above the main content on phones. Order the HTML for the smallest screen.
- Hiding content on small screens by default. If something matters, make it fit. Hiding it with
display: nonestill downloads it and drops information for phone users. - Skipping the viewport tag. Without it, mobile browsers lay out a wide virtual page, and your base styles are shrunk to fit.
Exercise
A stylesheet sets .layout { display: grid; grid-template-columns: 2fr 1fr; } with no media query, then uses @media (max-width: 47.99em) to set grid-template-columns: none. Rewrite it in the mobile-first style and say what changes for a phone.
Show answer
Put display: grid; gap: 1rem; padding: 1rem; in the base rule with no column definition. Then add @media (min-width: 48em) { .layout { grid-template-columns: 2fr 1fr; } }. On a phone, the browser no longer applies a rule only to cancel it, and if the query fails the page still shows a single column.
Exercise
Why might a base rule .container { width: 600px; } break a 360px-wide phone, and what would you use instead?
Show answer
The container is wider than the screen, so the page scrolls horizontally. Use max-width: 600px (or max-width: 37.5rem) so it can shrink to fit, or let it fill its parent with no width.