Comprehensive Guide Of Flexbox
3 min readMay 1, 2024
Flexbox, short for Flexible Box Layout, is a powerful layout model in CSS designed to simplify the process of creating complex layouts with ease and flexibility. It provides a more efficient way to distribute space among items in a container, align them, and control their sizing and order.
Key Points:
01. Flex Container and Flex Items:
- Flexbox operates on two main components: the flex container and flex items.
- The container is the parent element that holds the flex items.
- Flex items are the children of the flex container and are laid out within the container according to the flex layout rules.
02. Main Axis and Cross Axis:
- Flexbox introduces two axes: the main axis and the cross axis.
- The main axis is defined by the
flex-direction
property and determines the primary direction in which flex items are laid out. - The cross axis is perpendicular to the main axis.
03. Flex Direction:
- The
flex-direction
property specifies the direction of the main axis. - It can be set to
row
,row-reverse
,column
, orcolumn-reverse
, allowing for horizontal or vertical layouts.
04. Justify Content:
justify-content
aligns flex items along the main axis of the flex container.- It offers various options such as
flex-start
,flex-end
,center
,space-between
, andspace-around
.
05. Align Items and Align Content:
align-items
aligns flex items along the cross axis of the flex container.align-content
controls the alignment of flex lines within the flex container when there is extra space in the cross axis.- Both properties offer alignment options like
flex-start
,flex-end
,center
,stretch
, andbaseline
.
06. Flex Grow, Flex Shrink, and Flex Basis:
flex-grow
,flex-shrink
, andflex-basis
are three properties that control how flex items grow, shrink, and behave in relation to each other.flex-grow
determines the ability of a flex item to grow relative to other items.flex-shrink
dictates whether a flex item can shrink if necessary.flex-basis
sets the initial size of a flex item before any remaining space is distributed.
07. Flex Wrap:
- By default, flex items are laid out in a single line. However, the
flex-wrap
property allows flex items to wrap onto multiple lines if needed. - Options include
nowrap
,wrap
, andwrap-reverse
.
Example:
.container {
display: flex;
flex-direction: row; /* Main axis from left to right */
justify-content: space-between; /* Items are spaced evenly along the main axis */
align-items: center; /* Items are centered along the cross axis */
flex-wrap: wrap; /* Items wrap onto multiple lines if necessary */
}
.item {
flex-grow: 1; /* Items grow to fill available space equally */
flex-basis: 200px; /* Initial width of each item */
margin: 10px; /* Space between items */
}
<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>
In this example, we have a flex container with five flex items. The container’s properties dictate the layout and alignment of the items, while each item’s properties control its flexibility and size. The result is a responsive and dynamic layout that adapts to different screen sizes and content.