Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Main Axis Alignment

Now let's lay out the navigation bar at the top of the page. We want the logo on the left and the links on the right, a layout you'll find on most websites.

The navigation bar (the nav element with the class navigation) has two children: the logo and the div with the class links, which holds the three links. When we make the navigation bar a flex container, these two children become its flex items.

How the items are spread along the main axis is set by the justify-content property of the flex container. Here are its values, shown on three items in a row:

flex-start: the items are at the start of the main axis. This is where they are when you don't set justify-content.

center: the items are in the middle.

flex-end: the items are at the end.

space-between: the first item is at the start, the last one at the end, and the spaces between the items are equal.

space-around: every item has the same space on both sides, so the spaces at the edges are half the size of the spaces between the items.

space-evenly: all the spaces are equal, between the items and at the edges.

The space that the items don't take up is called free space. With space-between, all of it goes between the items, which is just what our navigation bar needs.

Instructions

  1. 1

    In the rule for .navigation, add the declaration display: flex. The logo and the links now stand side by side.

  2. 2

    Add the declaration justify-content: space-between. The logo stays on the left, and the links move to the right.

Back

2/8

  1. 1. Flex Container
  2. 2. Main Axis Alignment
  3. 3. Cross Axis Alignment
  4. 4. Direction and Centering
  5. 5. Gaps Between Items
  6. 6. Wrapping Items
  7. 7. Growing Items
  8. 8. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.