CSS Flexbox
Flexbox (Flexible Box Layout) is a one-dimensional layout system in CSS. It makes aligning, distributing, and ordering items within a container incredibly easy — no more float and clearfix hacks. It's the most-used CSS layout tool in modern web development.
Why Flexbox?
Before Flexbox, developers struggled with vertical centering, equal-height columns, and responsive card layouts. Flexbox solves all of this with simple properties.
- Easy horizontal & vertical centering
- Automatic equal spacing between items
- Flexible sizing and wrapping
- Order items visually without changing HTML
Flex Container vs Flex Items
Add display: flex to a parent — it becomes a flex container. Its direct children become flex items.
.container {
display: flex;
}
/* HTML */
<div class="container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
flex-direction — Main Axis Direction
Controls the direction of items.
.container { flex-direction: row; } /* Default — left to right */
.container { flex-direction: row-reverse; } /* Right to left */
.container { flex-direction: column; } /* Top to bottom */
.container { flex-direction: column-reverse; } /* Bottom to top */
justify-content — Align Along Main Axis
Distributes items horizontally (in row direction).
.container { justify-content: flex-start; } /* Left (default) */
.container { justify-content: flex-end; } /* Right */
.container { justify-content: center; } /* Center */
.container { justify-content: space-between; } /* Equal gaps, first/last at edges */
.container { justify-content: space-around; } /* Equal margin around each */
.container { justify-content: space-evenly; } /* Perfectly even spacing */
space-between is the go-to for navigation bars and card grids. center is your friend for perfect horizontal centering.
align-items — Align Along Cross Axis
Aligns items vertically (in row direction).
.container { align-items: stretch; } /* Default — fill height */
.container { align-items: flex-start; } /* Top */
.container { align-items: flex-end; } /* Bottom */
.container { align-items: center; } /* Vertical center */
Perfect Centering Trick
The most famous Flexbox use-case — center anything horizontally and vertically:
.center-box {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
flex-wrap — Multi-line Flex
.container { flex-wrap: nowrap; } /* Default — single line */
.container { flex-wrap: wrap; } /* Wrap to next line */
.container { flex-wrap: wrap-reverse; }/* Wrap upward */
gap — Spacing Between Items
.container {
display: flex;
gap: 20px; /* Equal gap both directions */
gap: 20px 40px; /* row-gap column-gap */
}
Flex Item Properties
flex-grow
How much an item grows to fill extra space.
.item-1 { flex-grow: 1; }
.item-2 { flex-grow: 2; } /* Takes twice as much space */
flex-shrink
How much an item shrinks when there's not enough space.
.item { flex-shrink: 0; } /* Won't shrink — stays fixed */
flex-basis
Initial size of an item before growing/shrinking.
.item { flex-basis: 200px; }
flex Shorthand
/* flex: grow shrink basis */
.item { flex: 1 1 auto; } /* Common: flexible */
.item { flex: 0 0 250px; } /* Fixed 250px width */
.item { flex: 1; } /* Fill available space */
align-self — Override align-items
.container { align-items: flex-start; }
.special-item {
align-self: flex-end; /* This item goes to bottom */
}
order — Visual Reordering
.item-1 { order: 3; }
.item-2 { order: 1; } /* Appears first */
.item-3 { order: 2; }
Real-World Example 1 — Navbar
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 40px;
}
.logo { font-size: 1.2rem; }
.nav-links {
display: flex;
gap: 24px;
list-style: none;
}
Real-World Example 2 — Card Grid
.cards {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.card {
flex: 1 1 300px; /* Grow, shrink, min 300px */
padding: 24px;
border-radius: 16px;
background: #f0f0f0;
}
Real-World Example 3 — Center Login Form
.login-page {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #05060a;
}
.login-form {
width: 100%;
max-width: 400px;
padding: 32px;
background: white;
border-radius: 20px;
}
justify-content and align-items swap meaning when flex-direction: column. justify-content becomes vertical, align-items becomes horizontal. Remember: main axis vs cross axis.
Build a responsive pricing card layout with 3 cards using Flexbox. Requirements: equal height cards, gap of 24px, cards wrap on mobile, each card has a "Buy Now" button that sticks to the bottom of the card.
Quick Revision
display: flexmakes a flex container.flex-direction: row, column, reverse.justify-content— aligns along main axis.align-items— aligns along cross axis.gap— spacing between items.flex-wrap— allows multi-line layouts.- Item properties:
flex-grow,flex-shrink,flex-basis,align-self,order. - Perfect centering:
display:flex+justify-content:center+align-items:center.
Interview Questions
Q: Difference between justify-content and align-items?
A: justify-content aligns items along the main axis (horizontal in row direction). align-items aligns items along the cross axis (vertical in row direction). They swap when flex-direction: column.
Q: How do you center a div with Flexbox?
A: Set the parent to display: flex, justify-content: center, and align-items: center. This centers the child both horizontally and vertically.
Q: What is the difference between flex-grow and flex-shrink?
A: flex-grow controls how much an item grows when extra space is available. flex-shrink controls how much an item shrinks when there's not enough space. flex: 1 is shorthand for flex: 1 1 0.
Q: When should you use Flexbox vs Grid?
A: Use Flexbox for one-dimensional layouts (row OR column) — navbars, toolbars, card rows. Use Grid for two-dimensional layouts (rows AND columns) — full page layouts, complex dashboards, galleries.
Want to Master Web Development?
Join our Web Development course in Varanasi — practical classes and career support at our Sarnath centre.