Line Up Items With Flexbox

The events page’s header now sticks to the top of the window, but it’s 153 pixels tall. Most of that height is the menu, whose four links stack as a bulleted list, so the sticky header covers a big slice of every screen. Rosa would like the links in one row, the way most sites show their menu.

In this lesson, you’ll put the links in a row with flexbox, space them with gap, and learn to read a flex row’s two axes, the idea every other flexbox property builds on. Then you’ll check the result in DevTools. Start with the rule you already know where to put.

The links stack because each <li> is a block box in normal flow. To arrange the four list items, put display: flex on their parent, the <ul>. The <nav> has only one child, the list itself, so a rule on .site-nav would move nothing you can see.

Add this rule at the end of events/layout.css:

Language: CSS
.site-nav ul {
  display: flex;
}

Save, and look at the top of the page:

The header's four menu links now sit in one row, touching each other, with bullets squeezed in between them.
Four Links in a Row, With No Space Between Them

The <ul> is now a flex container, and its four <li> elements are its flex items. Flexbox places the items one after another, each as wide as its link, with nothing between them. That’s why the words run together.

The bullets are still there, too. Each <li> remains a list item, and the browser draws its marker outside the item’s left edge, so the marker now overlaps the end of the link before it.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.