What Is Flexbox?
Flexbox (Flexible Box Layout) is a CSS layout model designed to arrange, align, and distribute space between items in a container efficiently, even when their sizes are unknown or dynamic.
It works in one dimension at a time:
- Row or
- Column
🧱 Basic Flexbox Setup
.container {
display: flex;
}
This makes all child elements flex items.
✅ Why Flexbox Is Useful
✔ 1. Easy Alignment
Center items horizontally and vertically with just a few lines:
.container {
display: flex;
justify-content: center;
align-items: center;
}
✔ 2. Flexible Layouts
Items resize automatically based on available space.
.item {
flex: 1;
}
✔ 3. Direction Control
Control row or column layouts easily.
flex-direction: row; /* default */
flex-direction: column;
✔ 4. Spacing Between Items
Even spacing without margins.
justify-content: space-between;
✔ 5. Responsive by Nature
Flexbox adapts automatically to different screen sizes.
flex-wrap: wrap;
🔑 Key Flexbox Properties
Container properties:
display: flex;
flex-direction
justify-content
align-items
flex-wrap
gap
Item properties:
flex
align-self
order
🧪 Simple Example
.container {
display: flex;
gap: 10px;
}
.card {
flex: 1;
}
✔ Cards adjust automatically
✔ No fixed widths needed
🎯 Short Interview Answer
Flexbox is a one-dimensional CSS layout system that helps align and distribute space among elements efficiently.
It simplifies layout creation by providing easy alignment, flexible sizing, and responsive behavior, making it ideal for navigation bars, cards, and UI components.
⭐ One-line Summary
Flexbox makes layout alignment simple, flexible, and responsive with minimal CSS.