Advanced Flexbox
Complex alignments, wrapping, gaps, and flex patterns.
Overview
Flexbox handles one-dimensional layouts with powerful alignment.
Key Concepts
- Alignment — justify-content, align-items
- Wrapping — flex-wrap for multi-line
- Gap — Space between items
- Order — Visual ordering
- Flex Patterns — Common layout patterns
Code Examples
/* Centering */
.center-everything {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Space between */
.space-between {
display: flex;
justify-content: space-between;
align-items: center;
}
/* Sticky footer */
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-content {
flex: 1;
}
/* Card grid with equal heights */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 300px;
display: flex;
flex-direction: column;
}
.card-content {
flex: 1;
}
/* Holy grail layout */
.layout {
display: flex;
min-height: 100vh;
}
.sidebar-left {
flex: 0 0 250px;
}
.sidebar-right {
flex: 0 0 250px;
}
.main {
flex: 1;
}
/* Navigation */
.nav {
display: flex;
gap: 2rem;
padding: 1rem;
}
.nav-link {
position: relative;
}
.nav-link::after {
content: '';
position: absolute;
bottom: -4px;
left: 0;
width: 0;
height: 2px;
background: var(--primary);
transition: width 0.3s ease;
}
.nav-link:hover::after {
width: 100%;
}
/* Form layout */
.form-row {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.form-group {
flex: 1 1 200px;
}
/* Media object */
.media {
display: flex;
gap: 1rem;
align-items: flex-start;
}
.media-image {
flex-shrink: 0;
}
.media-content {
flex: 1;
}
Practice
Build a complex navigation and card layout using advanced Flexbox.