Flexbox Properties

📁 Flexbox

Display: Flex Flex-direction Justify-content Align-items Gap Flex-wrap Align-content Flex-grow Flex-shrink Flex-basis Flex (shorthand)

Assignment

Assignment 1 Assignment 2
display: flex
Enables flexbox on the container.
Example: display: flex;
Item 1
Item 2
Item 3
<div style="display:flex;"> <div class="demo-item">Item 1</div> <div class="demo-item">Item 1</div> <div class="demo-item">Item 1</div> </div>
📍 flex-direction
Definition: Defines the direction in which flex items should be arranged.
Sets the main axis direction - horizontal or vertical.
Default Value: row
Values: row | row-reverse | column | column-reverse

✦ Value 1: row (Default)

Items horizontally left to right
Item 1
Item 2
Item 3
.container { display: flex; flex-direction: row; /* Items horizontally */ }

✦ Value 2: row-reverse

Items arranged horizontally from right to left
Item 1
Item 2
Item 3
.container { display: flex; flex-direction: row-reverse; /* Items reverse horizontally */ }

✦ Value 3: column

Items arranged vertically from top to bottom
Item 1
Item 2
Item 3
.container { display: flex; flex-direction: column; /* Items vertically */ }

✦ Value 4: column-reverse

Items arranged vertically from bottom to top
Item 1
Item 2
Item 3
.container { display: flex; flex-direction: column-reverse; /* Items reverse vertically */ }
📍 justify-content
Definition: Aligns items along the main axis (usually horizontal).
Distributes space between and around items.
Default Value: flex-start
Values: flex-start | center | flex-end | space-between | space-around | space-evenly

✦ Value 1: flex-start (Default)

Items aligned at the start (left)
Box 1
Box 2
Box 3
.container { display: flex; justify-content: flex-start; /* Items at the start */ }

✦ Value 2: center

Items centered along the main axis
Box 1
Box 2
Box 3
.container { display: flex; justify-content: center; /* Items centered */ }

✦ Value 3: flex-end

Items aligned at the end (right)
Box 1
Box 2
Box 3
.container { display: flex; justify-content: flex-end; /* Items at the end */ }

✦ Value 4: space-between

Equal space between items - edges touch the container
Box 1
Box 2
Box 3
.container { display: flex; justify-content: space-between; /* Equal space between items - edges touch */ }

✦ Value 5: space-around

Equal space around each item
Box 1
Box 2
Box 3
.container { display: flex; justify-content: space-around; /* Equal space around each item */ }

✦ Value 6: space-evenly

Exactly equal space between all items and edges
Box 1
Box 2
Box 3
.container { display: flex; justify-content: space-evenly; /* All space exactly equal */ }
📍 align-items
Definition: Aligns items along the cross axis (usually vertical).
All items in a line are aligned to the same level.
Default Value: stretch
Values: flex-start | center | flex-end | stretch | baseline

✦ Value 1: flex-start

Items aligned at the start (top) of cross axis
Item 1
Item 2
Item 3
.container { display: flex; height: 120px; align-items: flex-start; /* Items at the start (top) */ }

✦ Value 2: center

Items centered vertically on cross axis
Item 1
Item 2
Item 3
.container { display: flex; height: 120px; align-items: center; /* Vertically center */ }

✦ Value 3: flex-end

Items aligned at the end (bottom) of cross axis
Item 1
Item 2
Item 3
.container { display: flex; height: 120px; align-items: flex-end; /* Items at the end (bottom) */ }

✦ Value 4: stretch (Default)

Items stretch to fill the entire container
Item 1
Item 2
Item 3
.container { display: flex; height: 120px; align-items: stretch; /* Pure container fill */ }

✦ Value 5: baseline

Items aligned based on their text baseline
Small
Large
Medium
.container { display: flex; height: 120px; align-items: baseline; /* Text baseline align */ }
📍 flex-wrap
Definition: Determines whether items should wrap into multiple lines or stay on one line.
When container space is limited, items wrap or shrink based on this property.
Default Value: nowrap
Values: nowrap | wrap | wrap-reverse

✦ Value 1: nowrap (Default)

Items stay on one line and can shrink
Item 1
Item 2
Item 3
Item 4
Item 5
.container { display: flex; flex-wrap: nowrap; /* Keep items on one line */ overflow-x: auto; /* Can scroll horizontally */ }

✦ Value 2: wrap

Items wrap to multiple lines (from top to bottom)
Item 1
Item 2
Item 3
Item 4
Item 5
.container { display: flex; flex-wrap: wrap; /* Items wrap to next line */ } .item { flex: 0 0 calc(33% - 10px); /* 3 items per row */ }

✦ Value 3: wrap-reverse

Items wrap to multiple lines in reverse (from bottom to top)
Item 1
Item 2
Item 3
Item 4
Item 5
.container { display: flex; flex-wrap: wrap-reverse; /* Items wrap in reverse order */ }
📍 gap
Definition: Adds consistent spacing between flex items.
An alternative to margin and padding that provides clean spacing.
Sub-properties: row-gap (space between rows), column-gap (space between columns)
Values: Pixels (10px), Em (1em), Percentage (5%), etc.

✦ gap: 10px;

10px spacing applied everywhere
Item 1
Item 2
Item 3
.container { display: flex; gap: 10px; /* 10px spacing between items */ }

✦ gap: 20px;

20px spacing applied everywhere - larger gaps
Item 1
Item 2
Item 3
.container { display: flex; gap: 20px; /* 20px spacing */ }

✦ column-gap & row-gap

Separate spacing - different for horizontal and vertical
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
.container { display: flex; flex-wrap: wrap; column-gap: 15px; /* Horizontal spacing between items */ row-gap: 25px; /* Vertical spacing between rows */ }

✦ gap: 1em;

Can use relative units like em, rem, or percentage
Item 1
Item 2
Item 3
.container { display: flex; gap: 1em; /* 1em = one unit of current font size */ }
📍 align-content
Definition: When items wrap to multiple lines, aligns those LINES vertically.
Important: Only works when flex-wrap: wrap is set.
Default Value: stretch
Values: flex-start | center | flex-end | space-between | space-around | space-evenly | stretch

✦ Value 1: flex-start

Wrapped lines aligned at the start (top) of container
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: flex-start; /* Lines at the top */ }

✦ Value 2: center

Wrapped lines centered in the container
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: center; /* Lines centered */ }

✦ Value 3: flex-end

Wrapped lines aligned at the end (bottom) of container
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: flex-end; /* Lines at the bottom */ }

✦ Value 4: space-between

Equal space between lines - edges touch container
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: space-between; /* Equal space between lines */ }

✦ Value 5: space-around

Equal space around each line
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: space-around; /* Equal space around each line */ }

✦ Value 6: space-evenly

Exactly equal space - including edges
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: space-evenly; /* All space equal */ }

✦ Value 7: stretch (Default)

Wrapped lines stretch to fill the entire container
1
2
3
4
5
.container { display: flex; flex-wrap: wrap; height: 200px; align-content: stretch; /* Lines fill entire container */ }
flex-grow
Controls how much each item grows.
0 = no grow, 1+ = expand
Example: flex-grow: 1;
Grow 1
Grow 2
Grow 1
<div style="display:flex;"> <div class="demo-item" style="flex-grow:1;">Grow 1</div> <div class="demo-item" style="flex-grow:2;">Grow 2</div> <div class="demo-item" style="flex-grow:3;">Grow 3</div> </div>
flex-shrink
Controls how items shrink when space is small.
Example: flex-shrink: 0;
No Shrink
Shrink
Shrink
<div style="display:flex;"> <div class="demo-item" style="flex-shrink:0;">No Shrink</div> <div class="demo-item">Shrink</div> <div class="demo-item">Shrink</div> </div>
flex-basis
Sets initial size of item before growing/shrinking.
Example: flex-basis: 150px;
150px
100px
80px
<div style="display:flex;"> <div class="demo-item" style="flex-basis:150px;">150px</div> <div class="demo-item" style="flex-basis:100px;">100px</div> <div class="demo-item" style="flex-basis:80px;">80px</div> </div>
flex (shorthand)
Combines grow + shrink + basis.
Example: flex: 1 1 200px;
Example: flex: 1 2 120px;
Flex
Flex
Flex
<div style="display:flex;"> <div class="demo-item" style="flex:1 2 120px;">Flex</div> <div class="demo-item" style="flex:2 1 120px;">Flex</div> <div class="demo-item" style="flex:1 1 120px;">Flex</div> </div>