CSS Flexbox

Table of contents

No heading

No headings in the article.

CSS Flexbox is a powerful layout tool that allows you to create flexible and responsive layouts in your web pages. With Flexbox, you can easily align and distribute elements along a single axis, either horizontally or vertically.

To use Flexbox, you need to define a flex container and its child elements. The flex container is set using the display property with the value flex or inline-flex. Once the container is defined, you can use various properties to control the layout and positioning of its child elements.

Here's an example of a simple Flexbox layout:

<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: center;
  align-items: center;
}

.item {
  background-color: #ccc;
  color: #fff;
  padding: 10px;
  margin: 10px;
}

In this example, the container has the display property set to flex, which makes it a flex container. The child elements of the container have the class item and will be positioned based on the flex properties set on the container.

The justify-content property is used to align the items along the horizontal axis. In this case, we've set it to center, which centers the items horizontally.

The align-items property is used to align the items along the vertical axis. In this case, we've also set it to center, which centers the items vertically.

The result is a simple layout where the three items are centered both horizontally and vertically inside the container.

You can also use Flexbox to create more complex layouts with multiple axes, flex wrap, and more. Here's an example of a two-column layout:

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

.item {
  background-color: #ccc;
  color: #fff;
  padding: 10px;
  margin: 10px;
  flex-basis: calc(50% - 20px);
}

In this example, we've set the flex container to wrap using the flex-wrap property. This allows the items to wrap to the next row if there's not enough space on the current row.

We've also set the justify-content property to space-between, which distributes the items along the horizontal axis with equal spacing between them.

The items themselves have the flex-basis property set to 50%, which means they take up half the available space. We've also subtracted the margin and padding from the calculation to ensure that there's enough space between the items.

The result is a two-column layout where the items are distributed evenly with equal spacing between them.

In conclusion, CSS Flexbox is a powerful tool that allows you to create flexible and responsive layouts with ease. By mastering the various properties and values, you can create complex layouts that work across different devices and screen sizes.