Sub topic 9
Lay out the navigation and header with Flexbox
What you need first
This chapter assumes you already have the page skeleton from the earlier chapters. That means a header containing a nav with a list of anchor links, and a linked styles.css. You should also know what a CSS rule and a selector are, and how the box model handles padding and margin.
The working model
Flexbox is a CSS layout mode that arranges items along one line. You switch it on with display: flex on a parent element, called the flex container. The direct children of that container become flex items.
A flex container has two axes. The main axis is the direction items flow, which is left to right by default. The cross axis runs at a right angle to it. Most Flexbox properties are defined in terms of these two axes, not in terms of "horizontal" and "vertical".
Turn the nav list into a row
By default, list items stack vertically, each on its own line. The links sit inside li elements, and the li elements are the direct children of the ul. The ul is therefore the element that needs display: flex.
nav ul {
display: flex;
gap: 1.5rem;
justify-content: center;
margin: 0;
padding: 1rem;
list-style: none;
}display: flexmakes theula flex container, so the items form a row.gapadds even space between items. It adds no space at the outer edges.justify-content: centerpositions the row along the main axis.margin: 0andpadding: 1remreplace the browser's default list spacing with your own.list-style: noneremoves the bullets.
Put a site name beside the navigation
A common header has the site name on the left and the links on the right. Add a link for the name inside the header:
<header>
<a class="site-name" href="#about">Jane Doe</a>
<nav aria-label="Main">
<ul>
<li><a href="#about">About</a></li>
<li><a href="#projects">Projects</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>Now make the header a flex container as well. Its two children are the name link and the nav. The nav ul rule changes too: justify-content: flex-end replaces center, because the list now sits in the right-hand part of the header.
header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 1rem;
}
.site-name {
font-weight: 700;
text-decoration: none;
}
nav ul {
display: flex;
gap: 1.5rem;
justify-content: flex-end;
margin: 0;
padding: 1rem;
list-style: none;
}space-between pushes the first and last items to opposite ends. align-items: center aligns them on the cross axis, so the name and the links share a vertical center.
Containers can nest. Here the header is a flex container, and the ul inside it is a second, separate flex container.
Properties you will use most
| Property | Applies to | What it controls |
|---|---|---|
| `display: flex` | container | Turns on Flexbox for the direct children |
| `flex-direction` | container | Main axis direction: `row` or `column` |
| `justify-content` | container | Placement along the main axis |
| `align-items` | container | Placement along the cross axis |
| `gap` | container | Space between items |
| `flex-wrap` | container | Whether items may wrap onto a new line |
Common errors
- Setting
display: flexon the wrong element. Flex applies to direct children only. If you set it onnav, the singleulbecomes the flex item and the links stay stacked. - Expecting
justify-contentto be horizontal. It follows the main axis. Withflex-direction: column, it works vertically. - Seeing no effect from
align-items. The cross axis needs spare room. If the container is only as tall as its content, there is nothing to align. - Using margins for spacing. Margins on each item add space at the edges too.
gapis simpler.
Exercise
A nav has <nav><ul><li>…</li></ul></nav>. You write nav { display: flex; } and the links stay in a vertical stack. Why, and what is the fix?
Show answer
Flex only affects direct children. The only direct child of nav is the ul, so the li items are not flex items. Apply display: flex to nav ul.