CSS Flexbox is a layout module in CSS that makes it easier to design flexible and responsive layouts. It allows elements within a container to be arranged horizontally or vertically and to have flexible dimensions to fit the available space.
One of the key features of flexbox is the ability to align and distribute space between elements within a container, even when the size of the elements is unknown or dynamic. This makes it an ideal tool for creating responsive layouts that adapt to different screen sizes and devices.
To use flexbox, you need to specify a parent element as a flex container by setting its display
property to flex
or inline-flex
. You can then use various flexbox properties to control the layout of the child elements within the container.
Here are some of the main flexbox properties that you can use to control the layout:
flex-direction
The flex-direction
property specifies the direction of the elements within the flex container. You can set it to row
, row-reverse
, column
, or column-reverse
. The default value is row
.
For example, if you set the flex-direction
property to column
, the elements will be arranged vertically from top to bottom. If you set it to row-reverse
, the elements will be arranged horizontally from right to left.
justify-content
The justify-content
property aligns the elements within the flex container along the main axis (which is the horizontal axis for a row, and the vertical axis for a column). You can set it to flex-start
, flex-end
, center
, space-between
, space-around
, or space-evenly
.
For example, if you set the justify-content
property to flex-start
, the elements will be aligned to the start of the main axis. If you set it to center
, the elements will be centered along the main axis.
align-items
The align-items
property aligns the elements within the flex container along the cross axis (which is the vertical axis for a row, and the horizontal axis for a column). You can set it to flex-start
, flex-end
, center
, baseline
, or stretch
.
For example, if you set the align-items
property to flex-start
, the elements will be aligned to the start of the cross axis. If you set it to center
, the elements will be centered along the cross axis.
flex-wrap
The flex-wrap
property specifies whether the elements within the flex container should wrap onto multiple lines if they don’t fit on a single line. You can set it to nowrap
, wrap
, or wrap-reverse
. The default value is nowrap
.
For example, if you set the flex-wrap
property to wrap
, the elements will wrap onto multiple lines if they don’t fit on a single line. If you set it to wrap-reverse
, the elements will wrap onto multiple lines in the opposite direction.
flex-flow
The flex-flow
property is a shorthand property for setting the flex-direction
and flex-wrap
properties at the same time. You can set it to a combination of the values for flex-direction
and flex-wrap
, separated by a space.
For example, you can set the flex-flow
property to row wrap
to specify a row layout with wrapping enabled.