Understanding CSS Flexbox

Flexbox is a powerful layout module in CSS that allows you to design complex layouts with ease. Let’s dive into the fundamentals and practical applications of Flexbox.

Number Of Items 3
Number of children 4
.parent {
  display: grid;
  height: 200px;
}     

To start using Flexbox, you need a flex container. Flex items are the children of the flex container. Here’s a basic example:

In this example, .container is a flex container, and .item elements are flex items.

Main Axis and Cross Axis

Flexbox layout is based on two axes: the main axis and the cross axis. By default, the main axis runs horizontally.

Loading...

RESULT

In this example, justify-content: center centers the flex items along the main axis.

Flex Properties

Here are some commonly used flex properties:

  • flex-direction: Specifies the direction of the flex items (row, column, row-reverse, column-reverse).
  • justify-content: Aligns items along the main axis (flex-start, center, flex-end, space-between, space-around).
  • align-items: Aligns items along the cross axis (flex-start, center, flex-end, stretch).
  • align-self: Aligns a single item along the cross axis.
  • flex-wrap: Specifies whether the flex items should wrap if they exceed the container width.
  • flex-grow: Specifies how much a flex item should grow relative to the other items.
  • flex-shrink: Specifies how much a flex item should shrink relative to the other items.
  • flex-basis: Specifies the initial size of a flex item.
.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

Advanced Flexbox Techniques

Flexbox allows for more advanced layouts. Here’s an example of a card layout using Flexbox:

Loading...

RESULT

With Flexbox, creating responsive and adaptable layouts becomes much simpler.