Advanced Flexbox Techniques
Master complex layouts with nested flexboxes, wrapping, and advanced alignment. Nested Flexbox = infinite layout possibilities.
Create complex layouts by nesting flex containers inside flex items. This is the secret to building real-world interfaces.
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.header, .footer { flex: 0 0 auto; }
.main {
flex: 1;
display: flex; /* Nested flexbox */
gap: 2rem;
}
.sidebar {
flex: 0 0 250px; /* Fixed sidebar */
}
.content {
flex: 1; /* Takes remaining space */
display: flex; /* Nested flexbox for cards */
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
padding: 1.5rem;
background: #f3f4f6;
}
Pro Tip: Nesting is powerful but keep it to 2-3 levels max for maintainability. Each level = a new flex context.
Control how items wrap when they exceed container size using flex-wrap and align-content.
.container {
display: flex;
flex-wrap: wrap;
align-content: space-between; /* Aligns wrapped rows */
gap: 1rem;
height: 400px;
}
.item {
flex: 1 1 150px;
}
/* Without wrapping — items shrink to fit */
.nowrap { flex-wrap: nowrap; }
/* With wrapping — items wrap to next line */
.wrap { flex-wrap: wrap; align-content: center; }
/* Reverse wrap direction */
.wrap-reverse { flex-wrap: wrap-reverse; }
Important: align-content only works when there are multiple lines (items have wrapped). It's like justify-content but for wrapped lines instead of items.
Use margin: auto, order, and align-self for sophisticated layout control:
.container {
display: flex;
height: 200px;
}
/* Margin: auto — absorbs space in that direction */
.item1 { margin-right: auto; } /* Pushes everything right */
.item3 { margin-left: auto; } /* Pushes itself right */
/* Order — changes visual order */
.item3 { order: -1; } /* Moves to first position */
.item1 { order: 1; } /* Moves to last position */
/* Align-self — individual item alignment */
.item2 { align-self: flex-end; } /* This item goes to bottom */
.item4 { align-self: center; } /* This item centers vertically */
margin: auto on a flex item is a powerful centering technique. margin-left: auto pushes the item to the right. margin: auto centers in both axes.Build real UI components with Flexbox:
/* Pattern 1: Centered Navigation */
.navbar { display: flex; justify-content: space-between; align-items: center; padding: 1rem 2rem; }
.nav-links { display: flex; gap: 1.5rem; list-style: none; }
.btn-group { display: flex; gap: 1rem; }
/* Pattern 2: Responsive Card Grid */
.grid { display: flex; flex-wrap: wrap; gap: 1.5rem; }
.card { flex: 1 1 250px; background: #f3f4f6; border-radius: 8px; padding: 1.5rem; }
/* Pattern 3: Form Layout */
.form { display: flex; flex-direction: column; gap: 1.5rem; }
.form-group { display: flex; flex-direction: column; gap: 0.5rem; }
.input { padding: 0.75rem; border: 1px solid #ddd; border-radius: 4px; }
/* Pattern 4: Media Object */
.media { display: flex; gap: 1rem; align-items: flex-start; }
.media-image { flex: 0 0 80px; border-radius: 50%; }
.media-body { flex: 1; }Practice advanced Flexbox techniques below.
Create advanced-flexbox.html.
Build a page with: header, sidebar, main content with card grid, and footer.
Try these experiments:
Done — Key Points:
- ✅ Nested flexboxes = infinite layout possibilities. Each level is a new flex context. Keep to 2-3 levels.
- ✅
flex-wrap: wrapenables multi-line layouts.align-contentcontrols wrapped lines alignment. - ✅
margin: autoon flex items absorbs space.margin-left: autopushes item right. - ✅
orderchanges visual order without affecting HTML structure for screen readers. - ✅
align-selfoverrides align-items for individual items. Great for special positioning. - ✅ Real patterns: navbar, card grid, forms, media objects — all built with nested flexboxes.
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login