CSS Flexbox
Master one-dimensional layouts with CSS Flexbox. Perfect for component layout, navigation bars, and distributing space efficiently along a single axis.
What is CSS Flexbox?
Flexbox is a one-dimensional layout system: it arranges a container's children in a single row or column and distributes space between them. Set display: flex on the parent, then use justify-content to align items along the main axis and align-items to align them on the cross axis.
.container {
display: flex;
flex-direction: row; /* row | column */
justify-content: center; /* main axis */
align-items: center; /* cross axis */
gap: 1rem;
}
.item { flex: 1; } /* grow to fill space */Interactive Flexbox Demo
Code Editor
<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>Preview
Flexbox Fundamentals
The Flexible Box Layout (Flexbox) is a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spaces.
Core Concepts
Flex Container: Parent element with display: flex
Flex Items: Direct children of the container
Main Axis: Primary axis (horizontal by default)
Cross Axis: Perpendicular to main axis
Main Start/End: Start and end of main axis
Cross Start/End: Start and end of cross axis
Container Properties
Flex Direction
flex-direction: row; /* default */
flex-direction: column;
flex-direction: row-reverse;
flex-direction: column-reverse;Establishes the main axis direction
Justify Content
justify-content: flex-start;
justify-content: center;
justify-content: space-between;
justify-content: space-around;Aligns items along the main axis
Align Items
align-items: stretch;
align-items: center;
align-items: flex-start;
align-items: flex-end;Aligns items on cross axis
Flex Wrap
flex-wrap: nowrap;
flex-wrap: wrap;
flex-wrap: wrap-reverse;Controls item wrapping
Gap Property
gap: 1rem;
gap: 1rem 2rem;
row-gap: 1rem;
column-gap: 2rem;Spacing between flex items
Item Properties
Flex Shorthand Property
flex: 1; /* flex: 1 1 0% */
flex: 0 1 auto; /* initial */
flex: none; /* 0 0 auto */Shorthand for grow, shrink, and basis
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;Individual flex properties
Practical Examples
Navigation Bar
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
}
.logo { flex-shrink: 0; }
.nav-links { display: flex; gap: 2rem; }
.user-menu { flex-shrink: 0; }Perfect for header layouts with logo, navigation, and user menu.
Card Layout
.card {
display: flex;
flex-direction: column;
height: 100%;
}
.card-content { flex: 1; }
.card-actions { margin-top: auto; }Creates cards with content that grows and actions at the bottom.
Centered Content
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Perfect centering solution */The most reliable way to center content both horizontally and vertically.
Best Practices
Best Practices
- • Use flexbox for one-dimensional layouts
- • Prefer gap over margins for spacing
- • Use flex shorthand for common patterns
- • Test with varying content lengths
- • Consider flex-wrap for responsive design
Common Use Cases
- • Navigation bars and headers
- • Button groups and toolbars
- • Card layouts and components
- • Form field arrangements
- • Media object patterns
CSS Flexbox — step by step
The Flex Container
Setting display: flex on a parent turns it into a flex container. Its direct children become flex items. The main axis runs along flex-direction (row by default); the cross axis is perpendicular. All layout happens relative to these two axes.
🎯 Quick Check
What does justify-content control in a flex container?
CSS Flexbox: summary
Key points
- Flex properties go on two levels: the container (
display: flex,flex-direction,justify-content,align-items,flex-wrap,gap) and the items (flex-grow,flex-shrink,flex-basis,order,align-self). - The main axis follows
flex-direction. In a row it runs left to right; in a column it runs top to bottom, sojustify-contentthen works vertically. flex: 1is shorthand forflex-grow: 1; flex-shrink: 1; flex-basis: 0%, which makes items share the free space equally.- Items don't wrap by default. Add
flex-wrap: wrapso they flow onto a new line instead of shrinking. - Use
gapfor spacing between items instead of margins; it never adds space on the outer edges.
Common mistakes
- Putting
justify-contentoralign-itemson the child instead of the flex container. - Expecting
align-items: centerto centre vertically when the container has no height. The container must be taller than its content. - Using Flexbox for a two-dimensional grid of rows and columns. CSS Grid handles that with far less code.
Frequently asked questions
›How do I center a div with Flexbox?
On the parent, set display: flex; justify-content: center; align-items: center; and give the parent a height (for example min-height: 100vh). The child is then centered horizontally and vertically.
›What is the difference between Flexbox and Grid?
Flexbox lays out items in one dimension (a row or a column) and sizes them from their content. Grid lays out items in two dimensions (rows and columns at once) from a layout you define. Use Flexbox for components like nav bars and button groups, Grid for page layouts and card grids.
›What does flex: 1 mean?
flex: 1 makes an item grow to take an equal share of the remaining space. It expands to flex-grow: 1, flex-shrink: 1 and flex-basis: 0%.
›Why are my flex items not wrapping?
The default is flex-wrap: nowrap, so items shrink to fit on one line. Add flex-wrap: wrap to the container and give items a minimum width or flex-basis.