Sub topic 3
Lay the links out in a row with Flexbox
This chapter assumes you know basic CSS selectors and properties, and the box model. It also assumes you have the semantic nav and ul markup from the first chapter, and that you have removed the default bullets and spacing from the list in the second. Here you turn that vertical list into a horizontal bar.
Why a list does not sit in a row by itself
Each li element is a block-level box by default. Block boxes stack vertically, so the four links appear one under another. You could change the li elements to display: inline, but that gives you whitespace gaps and little control over alignment. Flexbox is the better tool because it is designed to arrange a set of sibling boxes along one line.
Flex containers and flex items
Setting display: flex on an element makes it a flex container. Its direct children become flex items. By default, flex items are placed in a row, from left to right in left-to-right languages.
The important detail is which element gets the property. The li elements are children of the ul, so the ul must be the flex container.
The markup
This is the markup you are working with. It uses the nav landmark, an unordered list, and aria-current on the link for the current page.
<nav aria-label="Main">
<ul class="nav-list" id="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>The CSS
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}Here is what each declaration does:
display: flexmakes theula flex container, so the fourliitems line up in a row.gap: 0.25remadds space between items only. It adds nothing before the first item or after the last one, which is why it is easier to reason about than margins on eachli.margin: 0andpadding: 0remove the browser's default indentation, so the bar starts at the edge of its container.list-style: noneremoves the bullets.backgroundcolors the whole bar. The container stretches the full width of its parent, because a flex container is a block-level box.
Controlling the row
Three properties on the container cover most navigation layouts.
| Property | Default | What it does |
|---|---|---|
| `justify-content` | `flex-start` | Distributes items along the main axis (the row). Values such as `center` and `space-between` spread the items out. |
| `align-items` | `stretch` | Aligns items on the cross axis (vertical in a row). `stretch` makes every item as tall as the tallest one. |
| `flex-wrap` | `nowrap` | Controls whether items move to a new line when the row is full. |
To center the links in the bar, add justify-content: center to .nav-list. To keep a logo at the left and the links at the right, you can give one item an automatic left margin:
.nav-list .push-right {
margin-left: auto;
}An auto margin on a flex item absorbs all the free space on that side. Add class="push-right" to the li you want pushed to the right edge, and it and any items after it move there.
Common errors
- Setting
display: flexonnav. Thenavhas a single child, theul, so you get a one-item row and the list stays vertical. Put the property on theul. - Forgetting the reset. If the bullets or left padding are still visible, the reset from the previous chapter is missing or has a selector that does not match your list.
- Using
floatorinline-blockand expecting the same behavior. Those methods leave gaps from whitespace and make alignment harder. Prefer Flexbox for a single row. - Letting the row overflow. With the default
flex-wrap: nowrap, many links can run off the edge on a narrow screen. A later chapter on responsive layout handles this properly.
Check your understanding
Exercise
Your four links still appear stacked vertically after you add display: flex to the nav element. Which element should have the property instead, and why?
Show answer
Put display: flex on the ul (.nav-list). Flex items are the direct children of the flex container. The li elements are the children of the ul, so only the ul can lay them out in a row. The nav has only one child, the ul, so making it a flex container does not affect the li elements.
Exercise
Which declaration keeps the Home link at the left edge but moves the Contact link and everything after it to the right edge of the bar?
Show answer
Give the Contact li a class such as push-right and set margin-left: auto on it. The auto margin takes all the free space to its left, which pushes that item to the right edge.