CodeGym /Courses /Frontend SELF EN /Main Axis Alignment

Main Axis Alignment

Frontend SELF EN
Level 17 , Lesson 3
Available

7.1 The justify-content Property

Flexbox gives you some sweet tools for handling alignment and distributing space between elements within a Flex container. One of the key properties for this is justify-content, which deals with alignment of Flex items along the main axis.

The justify-content property specifies how the Flex items are distributed along the main axis of the Flex container. The main axis direction depends on the flex-direction: value.

  • If flex-direction is set to row or row-reverse, the main axis will be horizontal
  • If flex-direction is set to column or column-reverse, the main axis will be vertical

Values:

  • flex-start: items are aligned at the start of the container (default value)
  • flex-end: items are aligned at the end of the container
  • center: items are centered inside the container
  • space-between: items are evenly distributed with equal spaces between them
  • space-around: items are evenly distributed with spaces around them
  • space-evenly: items are evenly distributed with equal spaces between them and at the container edges

Example usage:

CSS
    
      .container {
        display: flex;
        border: 2px solid #000;
        padding: 10px;
        margin-bottom: 20px;
      }

      .item {
        background-color: #3498db;
        color: white;
        padding: 10px;
        margin: 5px;
      }

      .flex-start {
        justify-content: flex-start;
      }
    
  
HTML
    
      <div class="container flex-start">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

7.2 justify-content Values

1. flex-start

With the flex-start value, items are aligned at the start of the Flex container, meaning the left side if the main axis is horizontal (row), or the top side if the main axis is vertical (column).

Example:

CSS
    
      .container {
        display: flex;
        justify-content: flex-start;
      }
    
  
HTML
    
      <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

2. flex-end

With the flex-end value, items are aligned at the end of the Flex container, meaning the right side if the main axis is horizontal (row), or the bottom if the main axis is vertical (column).

Example:

CSS
    
      .container {
        display: flex;
        justify-content: flex-end;
      }
    
  
HTML
    
      <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

3. center

With the center value, items are centered inside the Flex container. This value is great for creating centered layouts.

Example:

CSS
    
      .container {
        display: flex;
        justify-content: center;
      }
    
  
HTML
    
      <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

4. space-between

With the space-between value, items are evenly distributed along the main axis with equal spaces between them. The first item is aligned at the start of the container and the last item at the end.

Example:

CSS
    
      .container {
        display: flex;
        justify-content: space-between;
      }
    
  
HTML
    
      <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

5. space-around

With the space-around value, items are evenly distributed with spaces around them. The spaces between items will be twice as large as the spaces at the container's edges.

Example:

CSS
    
      .container {
        display: flex;
        justify-content: space-around;
      }
    
  
HTML
    
      <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

6. space-evenly

With the space-evenly value, items are evenly distributed with equal spaces between them and around the container edges. This value ensures equal spacing throughout the container.

Example:

CSS
    
      .container {
        display: flex;
        justify-content: space-evenly;
      }
    
  
HTML
    
      <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
      </div>
    
  

7.3 Centering a Menu

Real-world usage example—centering a navigation menu:

CSS
    
      .nav {
        display: flex;
        justify-content: center;
        background-color: #333;
        padding: 10px;
      }

      .nav-item {
        color: white;
        padding: 10px 20px;
        text-decoration: none;
      }

      .nav-item:hover {
        background-color: #575757;
      }
    
  
HTML
    
      <nav class="nav">
        <a href="#" class="nav-item">Home</a>
        <a href="#" class="nav-item">About Us</a>
        <a href="#" class="nav-item">Services</a>
        <a href="#" class="nav-item">Contact</a>
      </nav>
    
  

Explanation of the code:

  • .nav: Flex container for the navigation menu with items centered
  • .nav-item: Flex items (links) with basic styling
1
Task
Frontend SELF EN, level 17, lesson 3
Locked
Using flex-start
Using flex-start
1
Task
Frontend SELF EN, level 17, lesson 3
Locked
Using flex-end and center
Using flex-end and center
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION