Sub topic 9
Laying out the page with grid and flexbox
This chapter assumes you know basic HTML tags, CSS selectors and properties, and how to inspect a page in your browser's developer tools. You will use CSS grid and flexbox to position the header, main, and footer. The footer will stay at the bottom of short pages.
Two tools, two jobs
CSS grid and flexbox both arrange child elements inside a container, but they suit different shapes.
- Grid works in two dimensions, rows and columns together. Use it for the overall page frame and for card layouts.
- Flexbox works along one axis at a time. Use it to line up items in a row or column, such as the contents of a header.
A common pattern is grid for the page frame and flexbox inside each region.
| Need | Tool | Key properties |
|---|---|---|
| Header, main, footer stacked, with main filling the space | Grid | `grid-template-rows` |
| Logo on the left, links on the right | Flexbox | `justify-content`, `align-items` |
| Navigation links in a row with even spacing | Flexbox | `display: flex`, `gap` |
The page frame with grid
Short pages are a common problem. If the content is only a few lines, the footer sits in the middle of the window with empty space below it. You can fix this by making body a grid with three rows.
body {
margin: 0;
min-height: 100vh;
display: grid;
grid-template-rows: auto 1fr auto;
}The three row sizes map to the three direct children of body: header, main, and footer.
autosizes a row to its content. The header and footer take only the height they need.1frgives the middle row all the remaining space. Thefrunit means one share of the free space.min-height: 100vhmakes the body at least as tall as the viewport.vhis one percent of the viewport height.
Because the body is always at least as tall as the window, main stretches to fill it and pushes the footer down. On a long page, the body grows past the viewport and the footer simply follows the content.
The header with flexbox
Inside the header there are two children: the site name and the navigation. You want them on opposite sides of one row, centered vertically.
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
header nav ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}In a flex container, justify-content controls the main axis, which is horizontal by default. The value space-between pushes the first child to the start and the last child to the end. align-items: center centers the children on the cross axis, which is vertical here.
The second rule turns the navigation list into a row. The gap property sets the space between items, so you do not need margins on each link. Resetting list-style, margin, and padding removes the bullets and the default indent.
Using grid inside main
Grid also works well inside a section. A card grid can fit as many columns as the width allows.
.card-grid {
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Each column is at least 16rem wide. Columns share any extra space equally because of 1fr. When the container is too narrow for another column, the cards wrap to the next row. No media query is needed.
Common errors
- The footer does not reach the bottom. Check that
min-height: 100vhis on the same element that hasdisplay: grid. Also check thatbodyhas no extra wrapper element between it and the three regions. A wrapper becomes the only grid child. - A scrollbar appears on short pages. The default
bodymargin of 8px adds to the 100vh height. Settingmargin: 0fixes it. - Items in the header do not spread out.
justify-contentonly works on an element withdisplay: flexordisplay: grid. - The nav links stay stacked. The
display: flexrule must be on theul, not thenav, because thelielements are children of theul.
Exercise
A page has a header, a main element with two short paragraphs, and a footer. The footer appears halfway up the window. Which two declarations on body fix this, and what does each one do?
Show answer
Use display: grid with grid-template-rows: auto 1fr auto, together with min-height: 100vh. The min-height makes the body at least as tall as the window. The row template sizes the header and footer to their content and gives all the leftover height to main, which pushes the footer to the bottom.
Exercise
You write nav { display: flex; gap: 1.5rem; } but the links in the ul still stack vertically. Why, and how do you fix it?
Show answer
The li elements are children of the ul, not of the nav, so the flex container must be the ul. Move the rule to header nav ul.