CSS logoCSS vCSS3INTERMEDIATE

CSS Flexbox

Master CSS Flexbox with visual examples, from basic layouts to advanced techniques

6 min read
cssflexboxlayoutresponsive

Flex Container

Create Flex Container

Transform any element into a flexible box layout container that arranges its children

css
📄 HTMLhtml
🎨 Display Flex
1
2
3
💡 Creates flex container
📦 Direct children become flex items
⚡ Enables flex properties

Flex Direction

Set the main axis direction to control how flex items flow within the container

css
📄 HTMLhtml
🎨 Row
1
2
3
🎨 Column
1
2
3
➡️ Sets main axis direction
🔄 Changes item flow
📱 Use media queries for responsive

Flex Wrap

Control whether flex items wrap to new lines when they overflow the container

css
📄 HTMLhtml
🎨 No Wrap
1
2
3
4
🎨 Wrap
1
2
3
4
📏 Controls line wrapping
💡 wrap allows multiple lines
⚡ wrap-reverse flips line order

Gap

Add consistent spacing between flex items without using margins

css
📄 HTMLhtml
🎨 Gap Demo
1
2
3
4
✨ Spacing between items
💡 Replaces margin hacks
📏 Works with wrap

Alignment

Justify Content (Main Axis)

Align and distribute flex items along the main axis (horizontal by default)

css
📄 HTMLhtml
🎨 Center
1
2
3
🎨 Space Between
1
2
3
↔️ Controls main axis alignment
💡 Distributes extra space
⚡ space-between for nav bars

Align Items (Cross Axis)

Align flex items along the cross axis (vertical by default) within their line

css
📄 HTMLhtml
🎨 Stretch
1
2
3
🎨 Center
S
M
L
↕️ Controls cross axis alignment
💡 stretch is default
🎯 center for perfect centering

Align Content

Align and distribute wrapped lines of flex items within the container

css
📄 HTMLhtml
📦 Aligns wrapped lines
⚠️ Only works with flex-wrap: wrap
💡 Similar to justify-content

Flex Items

Flex Grow

Define how much a flex item should grow relative to other items to fill available space

css
📄 HTMLhtml
🎨 Flex Grow
0
1
2
📈 Controls growth ratio
💡 0 means no growth
⚡ Distributes extra space

Flex Shrink

Define how much a flex item should shrink relative to other items when space is limited

css
📄 HTMLhtml
🎨 Flex Shrink
0
1
2
📉 Controls shrink ratio
⚠️ 0 prevents shrinking
💡 Handles overflow

Flex Basis

Set the initial main size of a flex item before free space is distributed

css
📄 HTMLhtml
🎨 Flex Basis
auto
200px
30%
📏 Sets initial main size
⚡ Before grow/shrink
💡 Overrides width/height

Flex Shorthand

Combine grow, shrink, and basis values in a single flexible property

css
📄 HTMLhtml
🎨 Flex Shorthand
flex: 1
flex: 2
none
🎯 Combines grow, shrink, basis
💡 flex: 1 for equal columns
⚡ Most common flex property

Align Self

Override the container's align-items value for individual flex items

css
📄 HTMLhtml
🎨 Align Self
Default
Start
Center
End
🎯 Overrides align-items
💡 For individual items
⚡ Useful for exceptions

Order

Change the visual order of flex items without modifying the HTML structure

css
📄 HTMLhtml
🎨 Order
HTML 1st
HTML 2nd
HTML 3rd
🔄 Changes visual order
💡 Doesn't affect HTML
⚠️ Affects tab order

Common Patterns

Center Content

Perfect centering technique using flexbox for both horizontal and vertical alignment

css
📄 HTMLhtml
🎯 Perfect centering pattern
💡 Works for any content
⚡ No more transform hacks

Navigation Bar

Common flexbox pattern for creating responsive navigation bars with logo and links

css
📄 HTMLhtml
🧭 Common navbar pattern
💡 space-between for logo/links
📱 Easy mobile adaptation

Card Grid

Responsive card layout that automatically adjusts columns based on available space

css
📄 HTMLhtml
🃏 Responsive card layout
💡 Auto-adjusting columns
⚡ No media queries needed

Sidebar Layout

Classic two-column layout with fixed sidebar and flexible main content area

css
📄 HTMLhtml
📐 Classic sidebar pattern
💡 Fixed sidebar width
📱 Stack on mobile