Sub topic 4
Building the single-column base layout
What this chapter assumes
You should know semantic HTML elements such as header, main and section. You should know CSS selectors, the box model and units such as rem, % and vw. You should be able to open browser developer tools and inspect an element. The viewport meta tag from the previous chapter is already in your page head:
<meta name="viewport" content="width=device-width, initial-scale=1">With that tag in place, the layout viewport is as wide as the device, often 360 to 430 CSS pixels on a phone. This chapter builds the layout that works at that width and stays correct at any other width. This base layout is the foundation that the min-width queries in the next chapter build on.
What "fluid" means
A fluid layout has no fixed widths. Its boxes take their size from the space the browser gives them, and the content inside reflows to fit. A single-column layout is the simplest fluid layout. Elements stack vertically in the order they appear in the HTML, and each one is as wide as its container.
You get most of this for free. Block elements such as div, p and section already fill the available inline space, and browsers have done this since the first stylesheet. Your job is mainly to avoid breaking it. Most mobile layout bugs come from CSS that overrides normal flow with a fixed width, an absolute position or a float that was only tested on a desktop.
Normal flow also matches your content ranking. If you put the most important content first in the HTML, the single column shows it first. You do not need CSS to reorder anything at this stage.
Start with box sizing
By default, width sets the content box only, and padding and borders are added on top. A box with width: 100% and padding: 1rem is therefore wider than its parent and causes horizontal scrolling. Switch every element to border-box so the declared size includes padding and border:
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}Removing the default body margin lets you control the page edge yourself with padding on a wrapper. This is easier to reason about than a browser default of 8px.
Constrain width with max values, not fixed widths
A fixed width: 960px forces horizontal scrolling on any screen narrower than 960 pixels. A cap does not. Use max-inline-size (the logical form of max-width) so the element can shrink and also stops growing on wide screens:
.page {
max-inline-size: 70rem;
margin-inline: auto;
padding-inline: 1rem;
}The three declarations do separate jobs:
max-inline-sizesets the upper limit.margin-inline: autocentres the box when the screen is wider than the limit.padding-inlinekeeps text off the screen edge, so it never touches the bezel.
Here margin-inline and padding-inline are logical properties. They map to left and right in left-to-right languages and flip automatically in right-to-left ones.
Percentages and rem are good units here. px is acceptable for hairlines such as 1px borders. Avoid vw for widths on containers. 100vw includes the width of any vertical scrollbar. On desktop browsers with classic scrollbars, width: 100vw is wider than the visible page and creates a horizontal scrollbar.
Space and stack the content
Between sections, use spacing that scales with the user's text size. Using rem for gaps and padding keeps the proportions right if someone raises their default font size. For stacking, you have two good tools.
Plain block flow with a flex or grid parent and gap avoids the old margin-collapsing and "last child" problems:
.stack {
display: grid;
gap: 1.5rem;
}A grid with no column definition has a single implicit column. Each child fills it. The gap property adds space only between items, so no extra space appears after the last one. Flexbox does the same job with display: flex; flex-direction: column; gap: 1.5rem;. Grid is a little more predictable for the single-column case, because items stretch to the column width by default.
The card list from the spine uses the same approach. The base rules are the single column, and wider layouts come later:
.card-list {
display: grid;
gap: 1rem;
padding: 1rem;
}Notice that the base rule contains no @media query and no column count. It is the mobile layout, and every browser applies it. Larger screens will get extra rules in the next chapter.
Make media and embedded content shrink
Images, video and iframes have an intrinsic size. An image 1600 pixels wide in a 360 pixel column overflows the page unless you tell it not to:
img,
video,
iframe {
max-inline-size: 100%;
}
img,
video {
block-size: auto;
}max-inline-size: 100% lets the element shrink below its natural size but never grow beyond it. block-size: auto keeps the aspect ratio as the width changes. If you also give the img width and height attributes, modern browsers use them to compute the aspect ratio before the file loads, which avoids layout shift. You will see more of this in the responsive images chapter.
Handle content that will not wrap
Real content is less polite than sample text. These cases cause overflow on narrow screens:
- A long URL or an unbroken string in a paragraph.
- A wide
preblock of code. - A data table with many columns.
- A grid or flex child whose minimum content width is larger than the column.
Handle each of them with a rule written for that case:
p,
li,
h1,
h2,
h3 {
overflow-wrap: anywhere;
}
pre {
overflow-x: auto;
}
.table-scroll {
overflow-x: auto;
}Wrap a wide table in a div with the table-scroll class, so that only the table scrolls sideways and the page does not.
The last case is subtle. Grid and flex items have an automatic minimum size equal to their content. A child containing a long word or a wide pre will refuse to shrink below that size and will push the track wider. The fix is to allow it to shrink:
.stack > * {
min-inline-size: 0;
}When you see a grid column that is wider than the screen for no obvious reason, check for this first.
Putting it together
This page is complete. Save it as index.html and open it in a browser, then narrow the window.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Base layout</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; font-size: 1rem; line-height: 1.5; }
.page { max-inline-size: 70rem; margin-inline: auto; padding: 1rem; }
.stack { display: grid; gap: 1.5rem; }
.stack > * { min-inline-size: 0; }
img { max-inline-size: 100%; block-size: auto; }
p, h1, h2 { overflow-wrap: anywhere; }
</style>
</head>
<body>
<div class="page">
<header><h1>Riverside Bakery</h1></header>
<main class="stack">
<section>
<h2>Opening hours</h2>
<p>Open every day from 7:00 to 18:00.</p>
</section>
<section>
<h2>Today's bread</h2>
<p>Sourdough, rye and seeded loaves.</p>
</section>
</main>
</div>
</body>
</html>There is no media query anywhere in it. The page is already usable at 320 pixels and readable at 1400 pixels, although at that width the text line will be long. That is the signal that a larger-screen rule is needed, which the next chapter covers.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Page scrolls sideways on a phone | A fixed `width` in px, or `100vw` | Use `max-inline-size` or `width: 100%` |
| Box sticks out past its parent by its padding | Default `content-box` sizing | Set `box-sizing: border-box` |
| Image is cut off or overflows | No size limit on the image | `max-inline-size: 100%; block-size: auto` |
| Grid column wider than the screen | Child has a large minimum content size | `min-inline-size: 0` on the child |
| Text touches the screen edge | No horizontal padding on the container | Add `padding-inline: 1rem` |
| Page zoomed out on a real phone | Missing viewport meta tag | Add the tag to `head` |
Summary of the rules
- Put content in the HTML in priority order and let normal flow stack it.
- Apply
border-boxto every element. - Cap widths with
max-inline-size, and avoid fixed widths and100vw. - Use
remandgapfor spacing, and padding at the page edge. - Make media shrink, and give wide content its own scroll area.
- Give grid and flex children
min-inline-size: 0when long content stretches the column.
Exercise
A page uses .hero { width: 100%; padding: 2rem; } and scrolls sideways on a phone, even though the viewport meta tag is present. What is the cause, and what is the smallest change that fixes it?
Show answer
With the default box-sizing: content-box, the 2rem of padding on each side is added to the 100% width, so the hero is wider than its parent. Add box-sizing: border-box to .hero, or better, to all elements with *, *::before, *::after { box-sizing: border-box; }.
Exercise
A two-item display: grid container has no column rules. One child contains a pre block with a line of 90 characters, and the container becomes wider than a 360 pixel screen. Give two CSS changes that stop it, and say what each one does.
Show answer
Add min-inline-size: 0 to the grid children so they may shrink below their content size, and add overflow-x: auto to the pre so the long line scrolls inside its own box instead of widening the column.