Sub topic 4
Writing the small-screen base styles
This chapter assumes you know the CSS box model, selectors and the cascade, and that you can open browser developer tools. It also assumes you have read the earlier chapters on the viewport meta tag and relative units, so width=device-width and rem should already be familiar. Here you write the part of the stylesheet that applies everywhere: the base styles.
What the base styles are
In a mobile-first stylesheet, the base styles are every rule that sits outside a media query. They apply to all screens, including the smallest. Media queries then add changes for wider screens.
This gives the base styles two jobs:
- They must produce a complete, usable page on a narrow screen with no media query help.
- They must not contain anything you will need to undo on wider screens.
The second job is the one that fails most often. If a base rule sets width: 300px and a wide-screen rule then has to override it, your small-screen styles have already made a layout decision that does not belong to small screens.
Start from normal flow
A browser lays out HTML in normal flow without any CSS. Block elements stack vertically and fill the available width. That is already a good single-column mobile layout. The simplest base styles do very little to the layout and mostly set up spacing, type and media.
Avoid these in the base layer unless you have a reason:
- Fixed widths in
pxon containers. floatorposition: absolutefor page structure.display: flexwithflex-direction: rowon large groups of content.- Hiding content with
display: noneon small screens. Hidden content still downloads, and users on phones lose access to it.
Instead, let content stack, and add side-by-side arrangements later with min-width queries.
A minimal reset
A few small rules remove common surprises. Write them once at the top of the stylesheet.
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
img {
max-width: 100%;
height: auto;
}box-sizing: border-box makes width include padding and border. Without it, a box set to width: 100% with padding: 1rem is wider than its parent, and on a narrow screen that causes horizontal scrolling.
Setting font-size: 1rem on body keeps the text at the user's chosen default size, which is usually 16px. Users who raise their browser's default text size get larger text, and your rem-based spacing grows with it.
The img rule is the one from the spine. It stops an image from overflowing its container and keeps its aspect ratio. A later chapter covers srcset and sizes.
Spacing and sizing with relative units
Use rem for spacing that should follow the root font size, such as padding, margins and gaps. Use em when a value should follow the font size of the element itself, such as the padding of a button. Use % or no width at all for horizontal sizing.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}This grid has no grid-template-columns, so it has one implicit column that fills the container. The gap puts space between rows. The padding keeps text off the screen edge. This is the base from the spine's two-column example, and it is complete on its own.
A common error is to give .layout a max-width and margin: 0 auto here. That is fine for centering later, but check that it does not constrain the small screen. A max-width in rem or ch only limits a container once the screen is wider than the limit, so it is safe. A width is not.
Build a navigation list as a stack
Navigation is a good test of the approach. On a phone, a vertical list of large links is easy to tap.
<nav class="nav" aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/guides">Guides</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>.nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.nav a {
display: block;
padding: 0.75rem 1rem;
text-decoration: none;
}Each link is a full-width block with 0.75rem of padding above and below. With a line height of 1.5 and a 1rem font, the link is about 2.75rem tall, which matches the touch target size used in the touch chapter. No float, no fixed widths, and no media query is needed to make it work.
On a wider screen, you could later add this inside a min-width query:
@media (min-width: 48em) {
.nav ul {
display: flex;
gap: 0.5rem;
}
}The query only adds. The base rules stay as they are.
Putting it together
This is a complete page you can save and open. It uses only base styles, and it is already usable on a phone.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trail Notes</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
img { max-width: 100%; height: auto; }
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
.nav ul { list-style: none; margin: 0; padding: 0; }
.nav a { display: block; padding: 0.75rem 1rem; text-decoration: none; }
</style>
</head>
<body>
<div class="layout">
<nav class="nav" aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/guides">Guides</a></li>
</ul>
</nav>
<main>
<h1>Trail Notes</h1>
<p>Short write-ups of day hikes, with distances and difficulty.</p>
</main>
<aside>
<p>Updated every Friday.</p>
</aside>
</div>
</body>
</html>The order of elements in the HTML is the order users see on a phone. Put the most important content first, because the single-column layout follows source order.
Common errors
Most problems in the base layer come from a few habits.
- Setting widths in pixels.
width: 960pxforces horizontal scrolling on any screen narrower than 960px. Usemax-widthor let blocks fill their container. - Forgetting the viewport meta tag. Without it, mobile browsers lay out the page at a wide virtual viewport, and your carefully written base styles are scaled down and look tiny.
- Using
max-widthqueries for the base. If you write the desktop layout first and then wrap small-screen fixes inmax-width, you are writing desktop-first CSS. It works, but you keep undoing earlier rules. - Overriding instead of adding. If a wider query has to set a property back to
initialornone, the base probably set something it should not have. - Relying on
vhfor full-height sections. On mobile browsers, the visible height changes as the address bar shows or hides. Prefermin-block-sizewith content, or test the newerdvhunit in your target browsers.
Checking your base
Run through this list when you finish the base styles:
| Check | What to look for |
|---|---|
| Horizontal scroll | None at 320px width |
| Text size | Body text is at least 1rem and not set in `px` |
| Tap targets | Links and buttons are at least about 2.75rem tall |
| Images | No image is wider than its container |
| Source order | Content reads in a sensible order as a single column |
| Overrides | No wider query needs to undo a base declaration |
Exercise
A stylesheet has .sidebar { width: 300px; float: right; } outside any media query, and a rule inside @media (max-width: 47.99em) that sets .sidebar { width: auto; float: none; }. Rewrite it in the mobile-first style. Which declarations belong in the base, and which belong in a query?
Show answer
Put nothing about width or float in the base, so the sidebar stacks below the main content as a normal block. Add the wide-screen layout in a min-width query, for example @media (min-width: 48em) { .sidebar { float: right; width: 300px; } }. The max-width override disappears because the base no longer sets values that have to be undone.
Exercise
Your page scrolls sideways at 320px wide. The viewport meta tag is present, and the only layout rule is .box { width: 100%; padding: 1rem; border: 1px solid #ccc; }. What is the likely cause, and what is the fix?
Show answer
Without box-sizing: border-box, the width of 100% applies to the content box, so padding and border make the element wider than its parent. Add *, *::before, *::after { box-sizing: border-box; } to the base styles.