CSS Flexbox, or Flexible Box Layout, is a CSS layout system used to arrange elements in rows or columns. It provides convenient control over alignment, spacing, sizing, and the order of elements.
In this chapter, we will learn the basics of Flexbox, its important properties, and how to use it to create responsive layouts.
Flexbox is a one-dimensional CSS layout model. It allows elements inside a container to be arranged along a main axis and aligned along a cross axis.
A Flexbox layout consists of:
For example:
.container {
display: flex;
}
The direct child elements of .container become flex items.
Flexbox works with two axes:
By default, Flexbox arranges items horizontally from left to right.
A flex container can be created using the display property.
This example creates a basic Flexbox container and places its child elements in a row.
<div class="container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
.container {
display: flex;
}
The flex-direction property specifies the direction of the main axis.
The main values are:
This example arranges the flex items vertically using flex-direction: column.
.container {
display: flex;
flex-direction: column;
}
The flex-wrap property determines whether flex items should remain on one line or move to additional lines.
The main values are:
This example allows flex items to move to another line when there is not enough available space.
.container {
display: flex;
flex-wrap: wrap;
}
The flex-flow property is a shorthand for flex-direction and flex-wrap.
This example arranges items in a row and allows them to wrap onto additional lines.
.container {
display: flex;
flex-flow: row wrap;
}
The justify-content property controls the alignment of flex items along the main axis.
Common values include:
This example centers the flex items along the main axis.
.container {
display: flex;
justify-content: center;
}
The align-items property controls the alignment of flex items along the cross axis.
Common values include:
This example centers the items along the cross axis.
.container {
display: flex;
align-items: center;
height: 200px;
}
Flexbox makes it easy to center an element horizontally and vertically.
This example centers the content in both directions using justify-content and align-items.
.container {
display: flex;
justify-content: center;
align-items: center;
height: 300px;
}
The align-content property controls the alignment of multiple flex lines along the cross axis. It is useful when flex items wrap onto multiple lines.
This example centers multiple rows of flex items within the container.
.container {
display: flex;
flex-wrap: wrap;
align-content: center;
height: 400px;
}
The gap property specifies the space between flex items.
This example adds 20px of space between the flex items.
.container {
display: flex;
gap: 20px;
}
Flexbox also provides properties that can be applied directly to individual flex items:
The order property changes the visual order of flex items.
The default value is 0.
This example moves the second item before the other items.
.item2 {
order: -1;
}
The flex-grow property determines how much a flex item can grow when extra space is available.
This example allows the second item to receive twice as much extra space as the first item.
.item1 {
flex-grow: 1;
}
.item2 {
flex-grow: 2;
}
The flex-shrink property determines how much a flex item can shrink when there is not enough space.
This example prevents the second item from shrinking.
.item1 {
flex-shrink: 1;
}
.item2 {
flex-shrink: 0;
}
The flex-basis property specifies the initial size of a flex item along the main axis.
This example gives the flex item an initial main size of 200px.
.item {
flex-basis: 200px;
}
The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis.
This example sets the grow factor, shrink factor, and initial size of the flex item.
.item {
flex: 1 1 200px;
}
The align-self property allows an individual flex item to have a different cross-axis alignment from the other items.
This example aligns one item at the start while other items can follow the container's alignment.
.container {
display: flex;
align-items: center;
}
.item {
align-self: flex-start;
}
Flexbox can be used to create layouts that adjust when the available space changes.
This example creates flexible cards that can wrap onto additional rows on smaller screens.
<div class="cards">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
.cards {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 250px;
padding: 20px;
border: 1px solid #ccc;
}
Flexbox is commonly used to arrange navigation links horizontally.
This example distributes navigation links across the available space.
<nav class="navbar">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</nav>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
An auto margin can be used to push a flex item toward the opposite side of the container.
This example pushes the second item to the right side of the flex container.
<div class="container">
<div>Logo</div>
<div class="login">Login</div>
</div>
.container {
display: flex;
}
.login {
margin-left: auto;
}
Both properties control alignment, but they work on different axes.
| Property | Purpose |
|---|---|
| justify-content | Aligns items along the main axis |
| align-items | Aligns items along the cross axis |
| align-self | Changes the alignment of an individual item |
| align-content | Aligns multiple flex lines |
Flexbox and CSS Grid are both CSS layout systems, but they are intended for different layout requirements.
| Flexbox | CSS Grid |
|---|---|
| Primarily one-dimensional | Two-dimensional |
| Works mainly with rows or columns | Works with rows and columns |
| Useful for component layouts | Useful for complete grid layouts |
| Excellent for alignment and spacing | Excellent for controlling rows and columns |
Flexbox is generally useful when the layout follows one primary direction, while Grid is more suitable when both rows and columns need to be controlled.
We request you to subscribe our newsletter for upcoming updates.