Flex

CSS flexbox is a layout model that allows you to arrange elements (such as text, images, or other HTML elements) in a flexible way. With flexbox, you can specify how you want the elements to be positioned, how much space they should take up, and how they should respond to changes in the size of the container they're in.

At a high level, there are two main components of a flexbox layout: the container and the items. The container is the element that holds all the items you want to arrange, and the items are the elements themselves. Here we will understand how to use the flexbox properties using the following example as base.

1
2
3

Getting started

To make a container a flexbox, you simply set its display property to flex. Once you've done that, you can use a variety of other properties to control how the items inside the container are arranged.

1
2
3

Gap between elements

The gap property is a shorthand property that sets the gap between flex items in both the main and cross axes of the flex container.

1
2
3

Direction

The flex-direction property determines the main axis and the direction in which the flex items are laid out within the flex container.

Horizontal

row: This is the default value, and it lays out the flex items from left to right in a horizontal row. The main axis runs horizontally from left to right, and the cross axis runs vertically from top to bottom.

row-reverse: This lays out the flex items from right to left in a horizontal row. The main axis still runs horizontally from left to right, but the order of the items is reversed.

flex-direction

This will control the main axis orientation

1
2
3
main
cross

Vertical

column: This lays out the flex items from top to bottom in a vertical column. The main axis runs vertically from top to bottom, and the cross axis runs horizontally from left to right.

column-reverse: This lays out the flex items from bottom to top in a vertical column. The main axis still runs vertically from top to bottom, but the order of the items is reversed.

flex-direction

This will control the main axis orientation

1
2
3
cross
main

Positioning

These are the 2 main properties that deals with positioning

justify-content controls the alignment of items along the main axis of the flex container, while align-items controls the alignment of items along the cross axis of the flex container.

The main axis and cross axis are determined by the flex-direction property. If flex-direction is set to row, the main axis runs horizontally from left to right, while the cross axis runs vertically from top to bottom. If flex-direction is set to column, the main axis runs vertically from top to bottom, while the cross axis runs horizontally from left to right.

flex-direction

This will control the main axis orientation

justify-content

This will control the items position on the main axis

align-items

This will control the items position on the cross axis

1
2
3
main
cross