How do I use css flexbox to justify content in a container?



  • CSS flexbox, Flexible Box. Is a way of arranging elements within a container in a responsive way. A responsive design adapts to various device screen sizes.

    Starting with the following demo HTML/CSS code.

    HTML. Create a container assign it two classes, one generic and the second unique to the element.

    ...
     <div class="container">
          <div class="item item-1">1</div>
          <div class="item item-2">2</div>
          <div class="item item-3">3</div>
          <div class="item item-4">4</div>
          <div class="item item-5">5</div>
    </div>
    ...
    

    CSS.

    ...
     .container {
            margin-top: 50px;
            padding: 20px 0;
            background-color: brown;
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;     
    }
    
          .item {
            padding: 30px;
            background-color: burlywood;
            text-align: center;
            font-size: 30px;
    }
    ...
    

    The result is:

    513cdbd9-eb0e-4cfc-bd18-abe8711372b4-image.png

    If we add various values to the CSS property justify-content it will position the contents of the container accordingly.

    Note: justify-content positions items along the main axis. If the flex-direction is changed the result will be different, because the container by default takes up as much as needed to display its content.

    Adding justify-content: flex-end; to our CSS you see the following result.

    e7e74913-0897-4ec0-841c-04483c3a1137-image.png

    Change the value to justify-content: center;

    af3b38ee-1645-4f96-aacb-8874b153da86-image.png

    Change the value to justify-content: space-between;

    c2766d5b-3fe8-49c5-ba1c-b6a070fce8f8-image.png

    Change the value to justify-content: space-around;

    a2fc25a8-b422-4f6b-a4d1-f92a7259a35b-image.png

    Change the value to justify-content: space-evenly;

    43b2fe2c-b5ba-4841-bf3a-f8c75a004f4b-image.png


Log in to reply
 

© Lightnetics 2024