Flexbox in CSS - johnverz22/webdev1-lessons GitHub Wiki
Flexbox (Flexible Box Layout) is a CSS layout model designed for aligning and distributing elements inside a container, even when their sizes are unknown.
β
Makes it easy to create responsive designs.
β
Aligns items horizontally and vertically without extra CSS tricks.
β
Reduces the need for floats and positioning.
β
Works well for navigation bars, cards, forms, and layouts.
Flexbox works with a parent container (flex container) and child items (flex items).
Property | Description |
---|---|
display: flex; |
Enables flexbox on an element |
flex-direction |
Defines the main axis (row , column , row-reverse , column-reverse ) |
justify-content |
Aligns items along the main axis (flex-start , center , space-between , etc.) |
align-items |
Aligns items along the cross-axis (flex-start , center , stretch , etc.) |
flex-wrap |
Allows items to wrap (nowrap , wrap , wrap-reverse ) |
align-content |
Aligns wrapped flex lines (flex-start , center , space-between , etc.) |
gap |
Adds space between flex items |
To use Flexbox, set display: flex;
on the parent element.
.flex-container {
display: flex;
background: lightgray;
padding: 10px;
}
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
β
Now, the child elements (.flex-item
) automatically align in a row.
Controls whether items are placed in a row (default) or column.
.flex-container {
display: flex;
flex-direction: row; /* default */
}
.flex-container-column {
display: flex;
flex-direction: column;
}
Value | Effect |
---|---|
row | Items placed left to right |
row-reverse | Items placed right to left |
column | Items placed top to bottom |
column-reverse | Items placed bottom to top |
β
Practice with Flexbox playgrounds like Flexbox Froggy.
β
Try building layouts using only Flexbox.
β
Experiment with combining Flexbox and Grid for complex designs.
Would you like interactive exercises for your students?
# **Beginnerβs Guide to Flexbox in CSS**Flexbox (Flexible Box Layout) is a CSS layout model designed for aligning and distributing elements inside a container, even when their sizes are unknown.
β
Makes it easy to create responsive designs.
β
Aligns items horizontally and vertically without extra CSS tricks.
β
Reduces the need for floats and positioning.
β
Works well for navigation bars, cards, forms, and layouts.
Flexbox works with a parent container (flex container) and child items (flex items).
To use Flexbox, set display: flex;
on the parent element.
.flex-container {
display: flex;
background: lightgray;
padding: 10px;
}
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
β
Now, the child elements (.flex-item
) automatically align in a row.
Controls whether items are placed in a row (default) or column.
.flex-container {
display: flex;
flex-direction: row; /* default */
}
.flex-container-column {
display: flex;
flex-direction: column;
}
Value | Effect |
---|---|
row |
Items placed left to right |
row-reverse |
Items placed right to left |
column |
Items placed top to bottom |
column-reverse |
Items placed bottom to top |
Controls how items spread along the main axis.
.flex-container {
display: flex;
justify-content: center;
}
Value | Effect |
---|---|
flex-start |
Items align left (default) |
flex-end |
Items align right |
center |
Items align center |
space-between |
Items spread with space between |
space-around |
Items spread with equal space around |
space-evenly |
Items spread with equal space between and outside |
.flex-container {
display: flex;
justify-content: space-between;
}
Controls how items align along the cross axis.
.flex-container {
display: flex;
align-items: center;
height: 200px;
border: 1px solid black;
}
Value | Effect |
---|---|
stretch |
Items stretch to fill height (default) |
flex-start |
Items align at the top |
flex-end |
Items align at the bottom |
center |
Items align centered |
baseline |
Items align by their text baseline |
By default, Flexbox keeps items on one line. Use flex-wrap: wrap;
to allow wrapping.
.flex-container {
display: flex;
flex-wrap: wrap;
}
Value | Effect |
---|---|
nowrap |
Items stay on one line (default) |
wrap |
Items wrap to a new line if needed |
wrap-reverse |
Items wrap in reverse order |
Instead of adding margins, use gap:
to set space between items.
.flex-container {
display: flex;
gap: 20px;
}
β
Works with flex-wrap
and makes layouts cleaner.
Controls how much an item grows relative to others.
.flex-item {
flex-grow: 1;
}
Value | Effect |
---|---|
0 |
Item does not grow (default) |
1+ |
Item grows relative to others |
.item1 { flex-grow: 2; }
.item2 { flex-grow: 1; }
β
item1
will be twice as wide as item2
.
Defines how much an item shrinks when space is limited.
.flex-item {
flex-shrink: 0;
}
Value | Effect |
---|---|
1 |
Item shrinks normally (default) |
0 |
Item does not shrink |
β Use when preventing items from becoming too small.
Defines the default size of an item before it grows or shrinks.
.flex-item {
flex-basis: 200px;
}
Value | Effect |
---|---|
auto |
Uses the itemβs width/height (default) |
px/%/rem |
Sets an exact size |
Allows one item to have different vertical alignment.
.flex-item {
align-self: flex-end;
}
β Useful for adjusting specific items in a row.
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.navbar {
display: flex;
justify-content: space-between;
background: #333;
padding: 10px;
}
.navbar a {
color: white;
text-decoration: none;
padding: 10px;
}
<div class="navbar">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
.card-container {
display: flex;
gap: 20px;
}
.card {
flex: 1;
background: lightgray;
padding: 20px;
}
<div class="card-container">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
Property | Description |
---|---|
display: flex; |
Enables Flexbox |
flex-direction |
Sets row/column layout |
justify-content |
Aligns items horizontally |
align-items |
Aligns items vertically |
flex-wrap |
Allows items to wrap |
gap |
Adds spacing between items |
flex-grow |
Controls item expansion |
flex-shrink |
Controls item shrinking |
flex-basis |
Sets default item size |
align-self |
Aligns one item differently |