Duration: 90 minutes | Difficulty: Intermediate | Prerequisites: Lessons 1-4 completed
By the end of this lesson, you will be able to:
Flexbox (Flexible Box Layout) is a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space or shrink to fit into smaller spaces.
display: flex.container {
display: flex; /* Block-level flex container */
/* OR */
display: inline-flex; /* Inline-level flex container */
}
Controls the main axis direction.
.container {
flex-direction: row; /* Default: left to right */
flex-direction: row-reverse; /* Right to left */
flex-direction: column; /* Top to bottom */
flex-direction: column-reverse; /* Bottom to top */
}
Controls whether items wrap to new lines.
.container {
flex-wrap: nowrap; /* Default: single line */
flex-wrap: wrap; /* Multi-line, top to bottom */
flex-wrap: wrap-reverse; /* Multi-line, bottom to top */
/* Shorthand */
flex-flow: row wrap; /* flex-direction + flex-wrap */
}
Aligns items along the main axis.
.container {
justify-content: flex-start; /* Default: start of container */
justify-content: flex-end; /* End of container */
justify-content: center; /* Center of container */
justify-content: space-between; /* Equal space between items */
justify-content: space-around; /* Equal space around items */
justify-content: space-evenly; /* Equal space everywhere */
}
Aligns items along the cross axis.
.container {
align-items: stretch; /* Default: stretch to fill */
align-items: flex-start; /* Start of cross axis */
align-items: flex-end; /* End of cross axis */
align-items: center; /* Center of cross axis */
align-items: baseline; /* Baseline of text */
}
Aligns wrapped lines (only works with multiple lines).
.container {
flex-wrap: wrap;
align-content: flex-start;
align-content: flex-end;
align-content: center;
align-content: space-between;
align-content: space-around;
align-content: stretch; /* Default */
}
Adds space between items.
.container {
gap: 16px; /* Same gap for both directions */
row-gap: 16px; /* Gap between rows */
column-gap: 24px; /* Gap between columns */
/* Shorthand */
gap: 16px 24px; /* row-gap column-gap */
}
Controls how much an item should grow.
.item {
flex-grow: 0; /* Default: don't grow */
flex-grow: 1; /* Grow to fill available space */
flex-grow: 2; /* Grow twice as much as items with flex-grow: 1 */
}
Controls how much an item should shrink.
.item {
flex-shrink: 1; /* Default: can shrink */
flex-shrink: 0; /* Don't shrink */
flex-shrink: 2; /* Shrink twice as much as default */
}
Sets the initial main size before free space is distributed.
.item {
flex-basis: auto; /* Default: based on content */
flex-basis: 200px; /* Fixed width/height */
flex-basis: 20%; /* Percentage of container */
flex-basis: content; /* Based on content size */
}
.item {
/* flex: grow shrink basis */
flex: 1; /* flex: 1 1 0% */
flex: 1 1 200px; /* grow: 1, shrink: 1, basis: 200px */
flex: none; /* flex: 0 0 auto (inflexible) */
flex: auto; /* flex: 1 1 auto (flexible based on content) */
}
Overrides align-items for individual items.
.item {
align-self: auto; /* Default: inherit from container */
align-self: flex-start;
align-self: flex-end;
align-self: center;
align-self: baseline;
align-self: stretch;
}
<nav class="navbar">
<div class="nav-brand">Logo</div>
<ul class="nav-menu">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<div class="nav-actions">
<button class="btn-login">Login</button>
<button class="btn-signup">Sign Up</button>
</div>
</nav>
.navbar {
display: flex;
align-items: center;
padding: 1rem 2rem;
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.nav-brand {
font-size: 1.5rem;
font-weight: bold;
color: #2563eb;
}
.nav-menu {
display: flex;
list-style: none;
margin: 0 auto; /* Push to center, actions to right */
padding: 0;
gap: 2rem;
}
.nav-menu a {
text-decoration: none;
color: #374151;
font-weight: 500;
transition: color 0.2s;
}
.nav-menu a:hover {
color: #2563eb;
}
.nav-actions {
display: flex;
gap: 1rem;
}
.btn-login, .btn-signup {
padding: 0.5rem 1rem;
border: 1px solid #2563eb;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.btn-login {
background: transparent;
color: #2563eb;
}
.btn-signup {
background: #2563eb;
color: white;
}
<div class="card-grid">
<div class="card">
<img src="image1.jpg" alt="Card 1">
<div class="card-content">
<h3>Card Title 1</h3>
<p>Card description goes here...</p>
</div>
</div>
<!-- More cards... -->
</div>
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 2rem;
padding: 2rem;
}
.card {
flex: 1 1 300px; /* Grow, shrink, min-width 300px */
max-width: 400px;
background: white;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
overflow: hidden;
transition: transform 0.3s;
}
.card:hover {
transform: translateY(-4px);
}
.card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.card-content {
padding: 1.5rem;
}
<div class="hero">
<div class="hero-content">
<h1>Welcome to Our Site</h1>
<p>Beautiful layouts made easy with Flexbox</p>
<button class="cta-button">Get Started</button>
</div>
</div>
.hero {
display: flex;
align-items: center; /* Vertical center */
justify-content: center; /* Horizontal center */
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
text-align: center;
}
.hero-content h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
.hero-content p {
font-size: 1.25rem;
margin-bottom: 2rem;
}
.cta-button {
padding: 1rem 2rem;
font-size: 1.125rem;
background: white;
color: #667eea;
border: none;
border-radius: 6px;
cursor: pointer;
transition: transform 0.2s;
}
.cta-button:hover {
transform: scale(1.05);
}
justify-content and align-items?flex: 1 mean?In Lesson 6, weβll explore CSS Positioning, learning how to precisely place elements using static, relative, absolute, fixed, and sticky positioning.
Flexbox is incredibly powerful for creating flexible, responsive layouts. Master these concepts to build modern web interfaces with ease.