Mastering CSS Flexbox: A Step-by-Step Guide


CSS Flexbox (Flexible Box Layout) is a modern and powerful layout module designed to provide an efficient way to align, distribute, and position elements within a container, even when their size is dynamic. Flexbox simplifies creating complex layouts without relying on float-based or grid-based hacks.
Key Features of Flexbox
One-dimensional layout: Flexbox manages layout in a single direction, either as a row (horizontal) or column (vertical).
Responsive design: It adapts elements dynamically to different screen sizes.
Alignment control: Provides powerful tools to align elements both horizontally and vertically.
Flexible item resizing: Elements can grow, shrink, or remain fixed in size based on available space.
Terminology
Before diving into examples, let's understand the key terms:
Flex Container: The parent element that contains the flex items. Apply
display: flex;
to make an element a flex container.Flex Items: The direct children of the flex container.
Main Axis: The primary axis along which the items are laid out. Defined by the
flex-direction
property.Cross Axis: The axis perpendicular to the main axis.
How to Use Flexbox
1. Setting Up a Flex Container
To enable Flexbox, apply the display: flex;
property to a container:
<div class="flex-container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<style>
.flex-container {
display: flex;
background-color: lightgray;
padding: 10px;
}
</style>
2. Flex Container Properties
flex-direction
Defines the main axis and the direction of items.
Values:
row
(default): Items are placed horizontally.row-reverse
: Items are placed horizontally, but in reverse order.column
: Items are placed vertically.column-reverse
: Items are placed vertically, but in reverse order.
Example:
<style>
.row {
display: flex;
flex-direction: row;
}
.column {
display: flex;
flex-direction: column;
}
</style>
<div class="row">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<div class="column">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
justify-content
Aligns items along the main axis.
Values:
flex-start
(default): Items align to the start.flex-end
: Items align to the end.center
: Items align to the center.space-between
: Items have equal space between them.space-around
: Items have space around them.
Example:
<style>
.justify {
display: flex;
justify-content: space-between;
background-color: lightblue;
padding: 10px;
}
</style>
<div class="justify">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
align-items
Aligns items along the cross axis.
Values:
stretch
(default): Items stretch to fill the container.flex-start
: Items align at the start of the cross axis.flex-end
: Items align at the end of the cross axis.center
: Items align at the center of the cross axis.baseline
: Items align along their baselines.
Example:
<style>
.align {
display: flex;
align-items: center;
height: 100px;
background-color: lightgreen;
}
</style>
<div class="align">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
flex-wrap
Controls whether items should wrap onto multiple lines if they overflow.
Values:
nowrap
(default): Items stay in a single line.wrap
: Items wrap onto multiple lines.wrap-reverse
: Items wrap onto multiple lines in reverse order.
Example:
<style>
.wrap {
display: flex;
flex-wrap: wrap;
background-color: lightcoral;
}
.item {
width: 100px;
margin: 5px;
}
</style>
<div class="wrap">
<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>
3. Flex Item Properties
flex
Controls the flexibility of individual items. It's a shorthand for:
flex-grow
: How much the item grows relative to others.flex-shrink
: How much the item shrinks relative to others.flex-basis
: The initial size of the item.
Example:
<style>
.container {
display: flex;
}
.flex1 {
flex: 1; /* Takes equal space */
background-color: lightblue;
}
.flex2 {
flex: 2; /* Takes twice the space */
background-color: lightgreen;
}
</style>
<div class="container">
<div class="flex1">1</div>
<div class="flex2">2</div>
</div>
order
Defines the order of items. By default, all items have order: 0
.
Items with a lower order value appear first.
Example:
<style>
.container {
display: flex;
}
.item1 {
order: 2;
}
.item2 {
order: 1;
}
</style>
<div class="container">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
</div>
align-self
Aligns an individual item along the cross axis, overriding align-items
.
Values:
auto
(default): Inherits fromalign-items
.flex-start
,flex-end
,center
,baseline
,stretch
.
Example:
<style>
.container {
display: flex;
height: 100px;
}
.item1 {
align-self: flex-start;
}
.item2 {
align-self: flex-end;
}
</style>
<div class="container">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
</div>
Real-World Example: Navigation Bar
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigation Bar</title>
<style>
.navbar {
display: flex;
justify-content: space-between;
background-color: navy;
padding: 10px;
color: white;
}
.logo {
font-size: 20px;
}
.links {
display: flex;
gap: 20px;
}
</style>
</head>
<body>
<div class="navbar">
<div class="logo">MySite</div>
<div class="links">
<a href="#" style="color:white;">Home</a>
<a href="#" style="color:white;">About</a>
<a href="#" style="color:white;">Contact</a>
</div>
</div>
</body>
</html>
Advantages of Flexbox
Simplifies alignment of items both horizontally and vertically.
Reduces the need for complex float-based or position-based layouts.
Automatically adjusts to different screen sizes.
Flexbox vs Grid
Flexbox is best for one-dimensional layouts (rows or columns).
Grid is better suited for two-dimensional layouts (rows and columns).
This is a detailed guide to using Flexbox for designing modern, responsive layouts. Flexbox is a versatile and powerful tool that can simplify CSS layout development significantly. If you have more questions or need advanced examples, let me know!
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: 🚀 In-depth explorations of emerging technologies 💡 Practical tutorials and how-to guides 🔧Insights on software development best practices 🚀Reviews of the latest tools and frameworks 💡 Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Let’s connect and grow together! 🌟