CSS Flexbox


Flexbox is a powerful layout module in CSS that simplifies arranging items within a container. It offers flexibility in controlling alignment, direction, and order, making it ideal for both small-scale component layouts and complex application designs.

Here’s a breakdown of key Flexbox concepts:

1. Flex Container:

The parent element containing the flex items. You enable flexbox by setting the display property to flex or inline-flex.

.container {
  display: flex; /* or inline-flex */
}

2. Flex Items:

The direct children of the flex container. Their layout is controlled by the flex container’s properties.

3. Main Axis & Cross Axis:

Flexbox lays out items along two axes:

4. Key Flex Container Properties:

5. Key Flex Item Properties:

Example:

<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>
.container {
  display: flex;
  justify-content: space-between; /* Space between items */
  align-items: center; /* Center vertically */
}

.item {
  background-color: lightblue;
  padding: 10px;
  border: 1px solid blue;
}

This example creates a flex container with three items spaced evenly apart and centered vertically. Experimenting with the different properties will give you a better understanding of how Flexbox works. Flexbox’s intuitive syntax and powerful features make it a must-know for modern web development.