Block elements stack on top of each other. To put them next to each other, modern CSS uses flexbox.
Flexbox is switched on in the parent element. Its children then line up in a row:
.menu {
display: flex;
}
That one line is all it takes. The parent is called the flex container and its children are the flex items.
Space between items
gap puts space between the items, and only between them, not around the outside:
.menu {
display: flex;
gap: 16px;
}
Spreading items out
justify-content decides what happens to leftover space along the row:
| Value | Result |
|---|---|
flex-start | Items packed at the start (the default) |
center | Items packed in the middle |
flex-end | Items packed at the end |
space-between | First item at the start, last at the end, equal space between |
space-around | Equal space around every item |
A site header with the logo on the left and the menu on the right is usually just display: flex with justify-content: space-between.
Your Task
- Turn
.headerinto a flex container so the site name and the menu sit on one row. - Push them to opposite ends with
justify-content: space-between. - Make
.menua flex container too, with agapof16pxbetween its links.
.headeris a flex container- The site name and the menu are pushed to opposite ends
.menuis a flex container with a16pxgap- The site name and the menu are on the same row
Lesson complete!
Next Lesson: Flexbox: Alignment and Wrapping