What is Flexbox?
Flexbox is a CSS layout system used to flexibly position and align elements on a web page and manage the spacing between them. The full name of Flexbox is Flexible Box Layout. In simple terms, if you want to place several elements side by side, one below another, in the center, or with equal spacing between them, Flexbox makes this process much easier. For example, on a website header, the logo may need to be positioned on the left while the menu is placed on the right. Or you may want to display three product cards in a single row with equal spacing between them. Layouts like these can be created easily using Flexbox. Flexbox is widely used in Front-End Development, especially for creating navigation menus, button groups, cards, forms, headers, and responsive interfaces.
What Is Flexbox Used For?
In the past, developers often used float, inline-block, and various manual margin calculations to position elements correctly on a web page. Flexbox provides a more systematic approach to positioning and aligning elements.
With Flexbox, you can:
- position elements horizontally or vertically;
- center elements;
- create equal spacing between elements;
- distribute available space between elements;
- resize elements according to available space;
- move elements to the next row when necessary;
- create responsive layouts.
One of the main advantages of Flexbox is that it allows you to manage the positioning of parent and child elements without writing large amounts of additional CSS.
How Does Flexbox Work?
To use Flexbox, you first apply:
display: flex;
to the parent element.
For example:
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
CSS:
.container {
display: flex;
}
After adding display: flex, .container becomes the flex container, while its direct child elements become flex items.
In this example:
container → Flex Container
item → Flex Items
The main Flexbox properties can then be applied to the container and its items.
What Is a Flex Container?
A Flex Container is the parent element to which the Flexbox layout is applied.
For example:
.container {
display: flex;
}
The direct child elements inside this element become flex items.
The following properties are commonly used on a Flex Container:
flex-direction
justify-content
align-items
flex-wrap
align-content
gap
These properties determine how elements are positioned inside the container.
What Is a Flex Item?
A Flex Item is a direct child element inside a Flex Container.
For example:
<div class="container">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
Here, .container is the Flex Container, while the three .card elements are Flex Items. Properties such as flex-grow, flex-shrink, flex-basis, flex, align-self, and order can be applied to Flex Items.
What Are the Main Axis and Cross Axis?
To understand Flexbox correctly, it is important to understand the concepts of the Main Axis and Cross Axis. Main Axis is the primary direction in which flex elements are positioned. Cross Axis is the direction perpendicular to the Main Axis.
If:
flex-direction: row;
is used, the main direction is horizontal.
In this case:
Main Axis → horizontal
Cross Axis → vertical
If:
flex-direction: column;
is used:
Main Axis → vertical
Cross Axis → horizontal
Understanding these concepts is especially important for understanding the difference between justify-content and align-items.
What Is flex-direction?
flex-direction determines the direction in which Flex Items are positioned.
The main values are:
flex-direction: row;
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;
The default value is:
flex-direction: row;
Therefore, elements are usually positioned from left to right in a single row.
For example:
.container {
display: flex;
flex-direction: column;
}
When this is used, the elements are positioned vertically, one below another.
What Is justify-content?
justify-content determines how Flex Items are positioned along the Main Axis.
The most commonly used values are:
justify-content: flex-start;
justify-content: flex-end;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
justify-content: space-evenly;
For example:
.container {
display: flex;
justify-content: center;
}
centers the elements along the Main Axis.
If:
justify-content: space-between;
is used, the first and last elements are placed at the edges, while the remaining space is distributed between the elements. This property is frequently used in navigation menus and card layouts.
What Is align-items?
align-items determines how elements inside a Flex Container are aligned along the Cross Axis.
The main values are:
align-items: stretch;
align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: baseline;
For example:
.container {
display: flex;
align-items: center;
}
centers the elements along the Cross Axis. The main difference between justify-content and align-items is that they work along different axes.
How Can You Center an Element with Flexbox?
One of the most common uses of Flexbox is centering an element both horizontally and vertically.
For example:
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
Here:
justify-content: center — centers along the Main Axis;
align-items: center — centers along the Cross Axis. This approach can be used for login forms, modal windows, and various UI blocks.
What Is gap?
gap is used to define the spacing between Flex Items.
For example:
.container {
display: flex;
gap: 20px;
}
In this case, there will be a 20px space between all Flex Items. Previously, developers often had to add separate margins to child elements to achieve the same result. The gap property allows spacing to be managed at the container level.
For example, for three product cards:
.products {
display: flex;
gap: 24px;
}
may be enough.
What Is flex-wrap?
By default, Flexbox tries to keep elements in a single row. To allow elements to move to a new row when they no longer fit inside the container, flex-wrap is used.
For example:
.container {
display: flex;
flex-wrap: wrap;
}
This property is especially useful in card layouts. For example, four cards may appear in one row on a wide screen, while some cards automatically move to the next row when the screen becomes narrower.
The main values are:
flex-wrap: nowrap;
flex-wrap: wrap;
flex-wrap: wrap-reverse;
The default value is nowrap.
What Is flex-grow?
flex-grow determines how much of the remaining available space inside the container a Flex Item should receive.
For example:
.item {
flex-grow: 1;
}
If applied to all elements, the available free space can be distributed between them.
For example:
.item-1 {
flex-grow: 1;
}
.item-2 {
flex-grow: 2;
}
In this case, the second element may receive a larger share of the available space than the first element.
What Is flex-shrink?
flex-shrink determines how much a Flex Item can shrink when there is not enough space inside the container.
For example:
.item {
flex-shrink: 1;
}
allows the element to shrink when necessary.
If:
.item {
flex-shrink: 0;
}
is used, the element will try not to shrink according to the Flexbox mechanism when available space decreases. This property can be useful when you need to preserve the minimum visual size of certain elements.
What Is flex-basis?
flex-basis determines the initial size of a Flex Item along the Main Axis before the available space is distributed.
For example:
.card {
flex-basis: 300px;
}
In this case, the card's initial main size is considered to be 300px. flex-basis becomes especially powerful when used together with flex-grow and flex-shrink.
What Is flex?
flex is a shorthand property that allows flex-grow, flex-shrink, and flex-basis to be written in a single line.
For example:
.item {
flex: 1 1 300px;
}
Here, the values correspond respectively to:
flex-grow
flex-shrink
flex-basis
In practical projects, you may also frequently see the shorter form:
.item {
flex: 1;
}
What Is align-self?
While align-items affects all Flex Items together, align-self allows you to change the alignment of a specific Flex Item along the Cross Axis.
For example:
.item-2 {
align-self: flex-end;
}
In this case, only the second element can be positioned differently from the other items.
What Is order?
order allows you to change the visual order of Flex Items.
For example:
.item-1 {
order: 2;
}
.item-2 {
order: 1;
}
In this case, the second item may visually appear before the first one. However, order should only be considered a tool for changing visual order. Changing the logical HTML structure purely for visual purposes can create accessibility and keyboard navigation problems. For this reason, it is better to preserve the correct HTML order and use the order property only when necessary.
What Is the Difference Between Flexbox and CSS Grid?
Flexbox and CSS Grid are both used to create layouts on web pages, but they are better suited to solving different types of problems. Flexbox mainly works in one dimension.
In other words, it is suitable for managing elements primarily in:
- a single row;
- or a single column.
CSS Grid, on the other hand, is a two-dimensional layout system that allows developers to manage both rows and columns at the same time. For example, Flexbox may be more convenient for positioning a navigation menu. For a complex dashboard layout consisting of multiple rows and columns, CSS Grid may be more suitable. In real projects, Flexbox and Grid are often used together rather than as direct alternatives to each other.
Is Flexbox Used for Responsive Design?
Yes. Flexbox makes it easier to create responsive interfaces because it allows elements to grow, shrink, and move to new rows.
For example:
.cards {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 280px;
}
In this example, the cards can resize according to the available space and move to the next row when the container becomes narrower. However, for fully responsive design, Media Queries, CSS Grid, and flexible sizing units can also be used together with Flexbox.
Where Is Flexbox Used?
Flexbox can be used to solve many Front-End layout problems.
Common examples include:
- navigation menus;
- headers;
- button groups;
- product cards;
- form elements;
- modal windows;
- sidebar structures;
- various content blocks.
For example, a simple navigation bar can be created like this:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
Here, the logo and menu can be positioned in the same row, while the available space between them is distributed automatically.
What Are the Most Common Mistakes When Using Flexbox?
One of the most common mistakes beginners make is confusing the justify-content and align-items properties. The direction in which these properties work depends on the value of flex-direction. Another common problem is trying to place many elements in one row without using flex-wrap. In this case, elements may become overly compressed or cause overflow on smaller screens. It is also not a good approach to try to build every layout using only Flexbox. CSS Grid may be more suitable for some two-dimensional page structures. The purpose of Flexbox is not to solve every CSS layout problem, but to make it easier to manage elements flexibly in one main direction when appropriate.
Where Should You Start Learning Flexbox?
Before learning Flexbox, it is useful to understand the fundamentals of HTML and CSS. In particular, knowledge of HTML elements, classes, CSS selectors, the Box Model, width, height, margin, and padding provides a strong foundation.
After that, you can follow this learning path:
display: flex → Main/Cross Axis → flex-direction → justify-content → align-items → gap → flex-wrap → flex-grow / flex-shrink / flex-basis
After learning these concepts, you can move on to CSS Grid, Media Queries, and Responsive Design. One of the best ways to learn Flexbox effectively is to build real components. Small projects such as navigation menus, product cards, pricing sections, login pages, and responsive card layouts can help you understand the topic through practice.
If you want to learn Flexbox, CSS Grid, Responsive Design, HTML, CSS, JavaScript, React.js, and other modern web technologies through practical projects, you can explore JET Academy's Frontend Programming Course.
