Flexbox in CSS - johnverz22/webdev1-lessons GitHub Wiki

What is Flexbox?

Flexbox (Flexible Box Layout) is a CSS layout model designed for aligning and distributing elements inside a container, even when their sizes are unknown.

Why Use Flexbox?

βœ… 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.


How Flexbox Works

Flexbox works with a parent container (flex container) and child items (flex items).

Basic Flexbox Properties

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

Step 1: Create a Flex Container

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.


Key Flexbox Properties

1. flex-direction (Row or Column?)

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

Next Steps

βœ… 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**

What is Flexbox?

Flexbox (Flexible Box Layout) is a CSS layout model designed for aligning and distributing elements inside a container, even when their sizes are unknown.

Why Use Flexbox?

βœ… 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.


How Flexbox Works

Flexbox works with a parent container (flex container) and child items (flex items).

Step 1: Create a Flex Container

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.


Key Flexbox Properties

1. flex-direction (Row or Column?)

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

2. justify-content (Align Horizontally)

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

Example:

.flex-container {
  display: flex;
  justify-content: space-between;
}

3. align-items (Align Vertically)

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

4. flex-wrap (Wrap Items to New Line)

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

5. gap (Spacing Between Items)

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.


Controlling Individual Flex Items

1. flex-grow (Expand to Fill Space)

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

Example:

.item1 { flex-grow: 2; }
.item2 { flex-grow: 1; }

βœ… item1 will be twice as wide as item2.


2. flex-shrink (Control Shrinking)

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.


3. flex-basis (Set Initial Size)

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

4. align-self (Override align-items for One Item)

Allows one item to have different vertical alignment.

.flex-item {
  align-self: flex-end;
}

βœ… Useful for adjusting specific items in a row.


Common Flexbox Layouts

1. Centering an Element

.flex-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

2. Creating a Navigation Bar

.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>

3. Equal-Height Cards

.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>

Flexbox Cheat Sheet

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
⚠️ **GitHub.com Fallback** ⚠️