Flexbox Layout System
Create flexible, responsive layouts with ease. Flexbox is perfect for one-dimensional layouts (rows or columns).
💻 Flexbox Basics
/* Enable Flexbox */
.container {
display: flex; /* Block-level flex container */
/* OR */
display: inline-flex; /* Inline-level flex container */
}
/* Basic terminology */
/* Container: Element with display: flex */
/* Items: Direct children of container */
/* Main axis: Primary direction (default: horizontal) */
/* Cross axis: Perpendicular to main axis */
🎯 Container Properties
flex-direction
/* Direction of main axis */
.container {
display: flex;
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 */
}
/* Example: Vertical layout */
.vertical {
display: flex;
flex-direction: column;
}
flex-wrap
/* Control wrapping */
.container {
display: flex;
flex-wrap: nowrap; /* Default: single line */
flex-wrap: wrap; /* Multi-line */
flex-wrap: wrap-reverse; /* Multi-line, reversed */
}
/* Example: Wrap items */
.gallery {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
flex-flow (Shorthand)
/* Combines direction and wrap */
.container {
display: flex;
flex-flow: row wrap;
/* Same as: */
/* flex-direction: row; */
/* flex-wrap: wrap; */
}
.container {
flex-flow: column nowrap;
}
justify-content (Main Axis)
/* Align items on main axis */
.container {
display: flex;
justify-content: flex-start; /* Default: start */
justify-content: flex-end; /* End */
justify-content: center; /* Center */
justify-content: space-between; /* Equal space between */
justify-content: space-around; /* Equal space around */
justify-content: space-evenly; /* Equal space everywhere */
}
/* Examples */
.nav {
display: flex;
justify-content: space-between;
}
.buttons {
display: flex;
justify-content: center;
gap: 10px;
}
align-items (Cross Axis)
/* Align items on cross axis */
.container {
display: flex;
align-items: stretch; /* Default: fill container */
align-items: flex-start; /* Start of cross axis */
align-items: flex-end; /* End of cross axis */
align-items: center; /* Center */
align-items: baseline; /* Baseline of text */
}
/* Examples */
.card {
display: flex;
align-items: center;
padding: 20px;
}
.header {
display: flex;
align-items: baseline;
}
align-content (Multiple Lines)
/* Align lines when wrapping */
.container {
display: flex;
flex-wrap: wrap;
align-content: flex-start; /* Lines at start */
align-content: flex-end; /* Lines at end */
align-content: center; /* Lines centered */
align-content: space-between; /* Space between lines */
align-content: space-around; /* Space around lines */
align-content: stretch; /* Default: stretch lines */
}
/* Only works with flex-wrap: wrap */
gap
/* Space between items */
.container {
display: flex;
gap: 20px; /* Space between all items */
/* OR separate values */
gap: 10px 20px; /* row-gap column-gap */
/* OR individual properties */
row-gap: 10px;
column-gap: 20px;
}
/* Much cleaner than using margins */
.grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
🎨 Item Properties
flex-grow
/* How much item grows relative to others */
.item {
flex-grow: 0; /* Default: don't grow */
flex-grow: 1; /* Grow to fill space */
flex-grow: 2; /* Grow twice as much */
}
/* Example: Flexible columns */
.sidebar {
flex-grow: 0;
width: 250px;
}
.content {
flex-grow: 1; /* Takes remaining space */
}
flex-shrink
/* How much item shrinks when space is limited */
.item {
flex-shrink: 1; /* Default: can shrink */
flex-shrink: 0; /* Don't shrink */
flex-shrink: 2; /* Shrink twice as much */
}
/* Example: Fixed sidebar */
.sidebar {
flex-shrink: 0;
width: 250px; /* Won't shrink below 250px */
}
.content {
flex-shrink: 1; /* Will shrink if needed */
}
flex-basis
/* Initial size before growing/shrinking */
.item {
flex-basis: auto; /* Default: based on content */
flex-basis: 200px; /* Fixed size */
flex-basis: 50%; /* Percentage */
flex-basis: 0; /* Ignore content size */
}
/* Example: Equal width columns */
.column {
flex-basis: 0;
flex-grow: 1;
}
flex (Shorthand)
/* Combines grow, shrink, basis */
.item {
flex: 0 1 auto; /* Default: grow shrink basis */
flex: 1; /* Same as: 1 1 0% */
flex: 2; /* Same as: 2 1 0% */
flex: 0 0 200px; /* Fixed 200px */
}
/* Common patterns */
.equal-width {
flex: 1; /* Equal width columns */
}
.fixed-width {
flex: 0 0 250px; /* Fixed 250px */
}
.auto-width {
flex: 0 1 auto; /* Based on content */
}
align-self
/* Override align-items for single item */
.container {
display: flex;
align-items: center;
}
.special-item {
align-self: flex-start; /* Override to flex-start */
align-self: flex-end;
align-self: center;
align-self: baseline;
align-self: stretch;
}
/* Example: Different alignment */
.header .logo {
align-self: center;
}
.header .user-menu {
align-self: flex-end;
}
order
/* Change visual order */
.item {
order: 0; /* Default */
}
.item-first {
order: -1; /* Appears first */
}
.item-last {
order: 1; /* Appears last */
}
/* Example: Reorder on mobile */
@media (max-width: 768px) {
.sidebar {
order: 2; /* Move sidebar after content */
}
.content {
order: 1;
}
}
🎯 Practical Examples
/* Navigation bar */
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
}
nav .logo {
flex: 0 0 auto;
}
nav .menu {
display: flex;
gap: 2rem;
}
nav .actions {
display: flex;
gap: 1rem;
}
/* Card layout */
.card {
display: flex;
gap: 20px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
}
.card img {
flex: 0 0 150px;
height: 150px;
object-fit: cover;
border-radius: 4px;
}
.card-content {
flex: 1;
display: flex;
flex-direction: column;
gap: 10px;
}
/* Gallery grid */
.gallery {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.gallery-item {
flex: 1 1 300px; /* Grow, shrink, min 300px */
max-width: 400px;
}
.gallery-item img {
width: 100%;
height: 250px;
object-fit: cover;
border-radius: 8px;
}
/* Centered content */
.center-box {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Sticky footer */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1; /* Takes remaining space */
}
footer {
flex: 0 0 auto;
}
/* Form layout */
.form-row {
display: flex;
gap: 20px;
margin-bottom: 20px;
}
.form-row .field {
flex: 1;
}
.form-row .field.small {
flex: 0 0 150px;
}
/* Dashboard layout */
.dashboard {
display: flex;
gap: 20px;
padding: 20px;
}
.sidebar {
flex: 0 0 250px;
background: #f5f5f5;
padding: 20px;
border-radius: 8px;
}
.main-content {
flex: 1;
}
.widget-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.widget {
flex: 1 1 300px;
min-height: 200px;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* Button group */
.button-group {
display: flex;
gap: 10px;
}
.button-group.vertical {
flex-direction: column;
}
.button-group.spread {
justify-content: space-between;
}
.button-group button {
flex: 1;
padding: 10px 20px;
}
/* Pricing cards */
.pricing {
display: flex;
gap: 20px;
justify-content: center;
}
.pricing-card {
flex: 0 1 300px;
display: flex;
flex-direction: column;
padding: 30px;
border: 2px solid #e0e0e0;
border-radius: 8px;
text-align: center;
}
.pricing-card.featured {
border-color: #007bff;
transform: scale(1.05);
}
.pricing-card h3 {
margin-bottom: 20px;
}
.pricing-card .price {
font-size: 2rem;
font-weight: bold;
margin-bottom: 20px;
}
.pricing-card .features {
flex: 1;
text-align: left;
margin-bottom: 20px;
}
.pricing-card button {
margin-top: auto;
}
🎯 Key Takeaways
- display: flex: Enable flexbox on container
- flex-direction: row (horizontal) or column (vertical)
- justify-content: Align items on main axis
- align-items: Align items on cross axis
- flex-wrap: Allow items to wrap to multiple lines
- gap: Space between items (cleaner than margins)
- flex: Shorthand for grow, shrink, basis
- Flexbox is one-dimensional: Use Grid for two-dimensional layouts