Sub topic 4
Building the single-column base layout
This chapter assumes you know basic HTML structure and semantic elements, CSS selectors, the box model and units, and how to open your browser's developer tools. It also assumes you have set the viewport meta tag, so the layout viewport matches the device width.
What a fluid base layout is
A fluid layout sizes its parts relative to the available space instead of fixed pixel widths. On a phone, the simplest fluid layout is a single column. Each block stacks below the previous one in the order you ranked your content. You get this almost for free, because the browser's normal flow already stacks block elements vertically.
The goal of the base layout is not decoration. It is a working page for the smallest screen, with no media queries at all. Larger screens enhance it later.
Start with normal flow
Write your HTML in priority order. The most important content comes first in the source, so it comes first on a small screen.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Support team</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Our support team</h1>
</header>
<main>
<section class="card-list">
<article>
<h2>Chat support</h2>
<p>Talk to us any time between 8:00 and 18:00.</p>
</article>
<article>
<h2>Email support</h2>
<p>We reply within one working day.</p>
</article>
<article>
<h2>Phone support</h2>
<p>Call us for urgent problems.</p>
</article>
</section>
</main>
</body>
</html>Without any CSS, the three articles already stack in one column. Your CSS only needs to make that column comfortable.
Make the column fluid
Use the following rules in styles.css. They use the same .card-list rules that later chapters will enhance.
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
}
.card-list {
display: grid;
gap: 1rem;
padding: 1rem;
}Each rule has a purpose:
box-sizing: border-boxmakeswidthinclude padding and border. Without it, a block withwidth: 100%and padding becomes wider than the screen.margin: 0onbodyremoves the default browser margin, so you control the spacing yourself.display: gridwithgapspaces the cards evenly. A grid with no column definition creates one column that fills the container.padding: 1remkeeps text away from the screen edge.
Avoid fixed widths
Fixed widths are the most common cause of sideways scrolling on a phone. A width: 600px rule on a 360px screen forces the page wider than the viewport.
Prefer these approaches:
| Instead of | Use | Reason |
|---|---|---|
| `width: 600px` | `max-width: 100%` or no width | The element never exceeds its container |
| `width: 960px` on a wrapper | `max-width: 60rem` with `margin-inline: auto` | The wrapper shrinks on small screens |
| `height: 300px` on text blocks | `min-height` or no height | Text can wrap and grow |
Media such as images and video also need a limit. This rule stops them from overflowing:
img,
video {
max-width: 100%;
height: auto;
}How the pieces connect
Common errors
- Horizontal scrolling. Find the element that is too wide. In developer tools, inspect elements and look for one wider than the viewport. A fixed width, a long unbroken string or an unconstrained image is usually the cause.
- Missing
box-sizing. Padding added to a full-width element pushes it past the screen edge. - Reordering with CSS too early. If you rely on
orderor absolute positioning to move key content up, keyboard and screen reader users still follow the source order. Fix the HTML order first. - Testing only at one width. Drag the browser width narrower than 320px. The content should still be readable, even if cramped.
Exercise
A card has width: 100% and padding: 1rem, and it overflows the screen by 2rem on a phone. What single rule fixes this, and why?
Show answer
Add box-sizing: border-box to the card. With the default content-box, the width applies only to the content, so the padding is added on top of 100% and the card ends up 2rem wider than its container. With border-box, the padding is counted inside the 100% width.
Exercise
Why should the HTML source order match your content ranking, even though CSS can visually reorder elements?
Show answer
Keyboard focus order and screen readers follow the source order, not the visual order. Writing the HTML in priority order means all users reach the most important content first, and the page still works if the CSS fails to load.