CSS Flexbox Introduction
1 April 2025 | Category: CSS
Flexbox, short for Flexible Box Layout, is a powerful layout model in CSS that allows for the creation of flexible and responsive layouts. It provides a more efficient and predictable way to distribute space within a container, even when the sizes of the elements are unknown or dynamic.
Flexbox was introduced to solve common layout challenges like aligning items, distributing space, and ensuring that elements respond well to different screen sizes. It simplifies many of the traditional layout techniques (like float-based layouts and table-based layouts).
🔹 Basics of Flexbox
In Flexbox, there are two main components:
- Flex Container: The parent element that holds the flex items.
- Flex Items: The child elements inside the flex container.
To make an element a flex container, you use the display: flex
or display: inline-flex
property. By default, the items inside the container become flex items.
Example:
.container {
display: flex;
}
🔹 Flexbox Container Properties
Here are the main properties that you can apply to the flex container to control the layout of the flex items.
1. flex-direction
This property defines the direction in which the flex items are placed inside the container.
- row (default): Items are placed in a row, horizontally from left to right.
- row-reverse: Items are placed in a row but in reverse order.
- column: Items are placed in a column, vertically from top to bottom.
- column-reverse: Items are placed in a column but in reverse order.
Example:
.container {
display: flex;
flex-direction: row;
}
2. flex-wrap
By default, flex items are placed in a single line (or row). The flex-wrap
property allows items to wrap into multiple lines if necessary.
- nowrap (default): All flex items will be on a single line.
- wrap: Flex items will wrap onto multiple lines.
- wrap-reverse: Flex items will wrap onto multiple lines, but in reverse order.
Example:
.container {
display: flex;
flex-wrap: wrap;
}
3. justify-content
The justify-content
property aligns the flex items along the main axis (the direction defined by flex-direction
).
- flex-start (default): Align items to the start of the container.
- flex-end: Align items to the end of the container.
- center: Align items to the center.
- space-between: Distribute items evenly with the first item at the start and the last item at the end.
- space-around: Distribute items evenly with equal space around each item.
- space-evenly: Distribute items evenly with equal space between all items.
Example:
.container {
display: flex;
justify-content: center;
}
4. align-items
The align-items
property aligns the flex items along the cross axis (perpendicular to the main axis).
- stretch (default): Items stretch to fill the container.
- flex-start: Items align to the start of the container.
- flex-end: Items align to the end of the container.
- center: Items align in the center of the container.
- baseline: Items align based on their baseline (used for text alignment).
Example:
.container {
display: flex;
align-items: center;
}
5. align-content
The align-content
property is similar to align-items
, but it applies when there are multiple lines of flex items (i.e., when flex-wrap
is set to wrap
or wrap-reverse
).
- stretch (default): Lines stretch to fill the container.
- flex-start: Lines are packed at the start.
- flex-end: Lines are packed at the end.
- center: Lines are packed in the center.
- space-between: Lines are distributed with space between them.
- space-around: Lines are distributed with space around them.
Example:
.container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
🔹 Flexbox Item Properties
Flex items also have properties that allow you to control their individual behavior within the flex container.
1. flex-grow
The flex-grow
property defines how much a flex item will grow relative to the other items in the container when there is extra space. By default, flex-grow
is 0, meaning items will not grow.
- Value: A number (default: 0). The larger the value, the more the item will grow.
Example:
.item {
flex-grow: 1;
}
In this example, the item will grow to take up available space.
2. flex-shrink
The flex-shrink
property defines how much a flex item will shrink relative to the other items in the container when there is not enough space. By default, flex-shrink
is 1, meaning items can shrink.
- Value: A number (default: 1). The larger the value, the more the item will shrink.
Example:
.item {
flex-shrink: 1;
}
In this example, the item will shrink to fit within the container if necessary.
3. flex-basis
The flex-basis
property defines the initial size of a flex item before any space distribution (growth or shrinking). It can be set in pixels, percentages, or auto
.
- Value:
auto
(default),length
(e.g.,px
,%
), orcontent
(sets based on content size).
Example:
.item {
flex-basis: 200px;
}
In this example, the flex item will initially take up 200px of space.
4. flex
The flex
shorthand property is used to combine flex-grow
, flex-shrink
, and flex-basis
into a single declaration. This is the most common way of defining how an item behaves in a flex container.
- Syntax:
flex: [flex-grow] [flex-shrink] [flex-basis]
Example:
.item {
flex: 1 1 200px;
}
This means:
- The item can grow and shrink equally (
1 1
). - The initial size is
200px
.
5. align-self
The align-self
property allows an individual flex item to override the alignment set by the align-items
property of the flex container. It aligns an item along the cross axis (perpendicular to the main axis).
- Values:
auto
(default),flex-start
,flex-end
,center
,baseline
,stretch
.
Example:
.item {
align-self: flex-end;
}
In this example, the flex item will be aligned at the end of the container, even if align-items
is set to center
for the entire container.
🔹 Practical Example:
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
<style>
.container {
display: flex;
justify-content: space-between;
align-items: center;
height: 200px;
background-color: lightgray;
}
.item {
background-color: teal;
color: white;
padding: 20px;
flex: 1;
text-align: center;
}
</style>
In this example:
- The container uses Flexbox to align the items horizontally.
- The items are evenly spaced with
justify-content: space-between
and centered vertically withalign-items: center
. - Each item has equal space and adjusts according to the container size because of
flex: 1
.
🔹 Conclusion
CSS Flexbox is a versatile and powerful tool for creating responsive and flexible layouts. It allows you to manage the layout of elements easily, aligning and distributing space dynamically. By understanding the properties of both the flex container and flex items, you can create complex layouts with much less effort than traditional methods.
Some key benefits of Flexbox:
- Simplified alignment: Align items horizontally and vertically without complex calculations.
- Responsive design: Easily adjust layouts for various screen sizes.
- Dynamic spacing: Flex items grow and shrink based on available space.
Flexbox is a must-know layout technique for web developers who want to build modern, responsive websites with ease.