Last Updated : 28 Jun, 2025
CSS Flexbox, short for the Flexible Box Layout module, is a powerful layout tool designed to simplify web page layouts by arranging items in rows or columns with ease.
The Flexible Box Layout module introduces a one-dimensional layout system that handles space distribution and item alignment effectively. It works seamlessly for horizontal (row) or vertical (column) arrangements, making it a go-to solution for responsive designs.
Key Features of CSS Flexbox
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: #f2f2f2;
padding: 10px;
}
.flex-item {
background-color: #4CAF50;
color: white;
margin: 5px;
padding: 20px;
text-align: center;
flex: 1;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
</div>
</body>
</html>
Output:
In this Example,
Flexbox operates on two axes:
For Row:-
Flexbox for Row AlignmentFor Column:-
Flexbox for Column Alignment 1. Main Axis:The cross axis will be perpendicular to the main axis.
Flexbox provides several properties for aligning and justifying content within the container:
More Examples of CSS Flexbox
Responsive Design with FlexboxFlexbox excels in creating responsive designs by adjusting items to fit various screen sizes. You can use media queries in combination with Flexbox properties to ensure your layout adapts seamlessly to different devices.
HTML
<html>
<head>
<style>
.flex-container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
background-color: #32a852;
}
.flex-container div {
background-color: #c9d1cb;
margin: 10px;
padding: 20px;
flex: 1 1 200px;
}
@media (max-width: 600px) {
.flex-container {
flex-direction: column;
}
}
</style>
</head>
<body>
<h2>Responsive Flexbox</h2>
<div class="flex-container">
<div>Item1</div>
<div>Item2</div>
<div>Item3</div>
</div>
</body>
</html>
In this example,
<html>
<head>
<style>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #4CAF50;
padding: 10px;
}
.navbar a {
color: white;
text-decoration: none;
padding: 10px 15px;
}
.navbar a:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="navbar">
<a href="#home">Home</a>
<a href="#services">Services</a>
<a href="#contact">Contact</a>
</div>
</body>
</html>
Output:
In this example,
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4