CSS Basics · Lesson 7 of 11

Flexbox: Side by Side

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:

ValueResult
flex-startItems packed at the start (the default)
centerItems packed in the middle
flex-endItems packed at the end
space-betweenFirst item at the start, last at the end, equal space between
space-aroundEqual 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

  1. Turn .header into a flex container so the site name and the menu sit on one row.
  2. Push them to opposite ends with justify-content: space-between.
  3. Make .menu a flex container too, with a gap of 16px between its links.
  • .header is a flex container
  • The site name and the menu are pushed to opposite ends
  • .menu is a flex container with a 16px gap
  • The site name and the menu are on the same row