Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Flexbox Properties in CSS


Flexbox is a powerful layout model in CSS that provides a more efficient way to arrange items within a container, especially when the size of the items is unknown or dynamic. To make full use of Flexbox, several properties are essential, including flex-direction, flex-wrap, justify-content, align-items, and align-self. Let's look at these properties and their usage with examples.

1. flex-direction

The flex-direction property defines the direction in which the flex items are placed in the flex container. It can take the following values:

Example (Default flex-direction: row):

          <style>
            .container {
              display: flex;
              flex-direction: row;
            }
          </style>
          <div class="container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example, the flex items are arranged in a horizontal row because the default value of flex-direction is row.

2. flex-wrap

The flex-wrap property controls whether the flex container’s items should wrap onto the next line if there isn't enough space to fit them in a single row or column. It can take the following values:

Example (Using flex-wrap: wrap):

          <style>
            .container {
              display: flex;
              flex-wrap: wrap;
            }
          </style>
          <div class="container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
          </div>
        

In this example, the flex container's items will wrap onto the next line if there isn't enough space to fit them in one row.

3. justify-content

The justify-content property is used to align the flex items along the main axis (horizontal by default). It controls the spacing between the items and their alignment within the container. Possible values include:

Example (Using justify-content: center):

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

In this example, the flex items will be centered along the main axis (horizontal by default).

4. align-items

The align-items property aligns flex items along the cross axis (perpendicular to the main axis). Possible values include:

Example (Using align-items: center):

          <style>
            .container {
              display: flex;
              align-items: center;
            }
          </style>
          <div class="container">
            <div>Item 1</div>
            <div>Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example, the flex items will be aligned to the center along the cross axis (vertical axis by default).

5. align-self

The align-self property allows individual flex items to override the align-items setting and align themselves differently along the cross axis. It accepts the same values as align-items.

Example (Using align-self: flex-end):

          <style>
            .container {
              display: flex;
            }
            .item-2 {
              align-self: flex-end;
            }
          </style>
          <div class="container">
            <div>Item 1</div>
            <div class="item-2">Item 2</div>
            <div>Item 3</div>
          </div>
        

In this example, Item 2 will be aligned to the end of the cross axis, while the other items will follow the align-items default (centered).

Conclusion

Flexbox is a powerful tool for creating responsive and flexible layouts in CSS. By using properties like flex-direction, flex-wrap, justify-content, align-items, and align-self, you can easily control how flex items are arranged, spaced, and aligned. Understanding these properties is essential for designing flexible, modern web layouts.










Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML 5

Python

java

C++

C

JavaScript

Campus Learning

C

C#

java