Today we use Flexbox and Grid together.
Often you'll use both on the same page!
.container { display: flex; }
This makes all direct children flex items that:
.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; }
.container { display: flex; justify-content: flex-start; /* default */ justify-content: flex-end; /* end of container */ justify-content: center; /* center */ justify-content: space-between; /* equal space between */ justify-content: space-around; /* equal space around */ justify-content: space-evenly; /* truly equal spacing */ }
.container { display: flex; align-items: stretch; /* default: fill height */ align-items: flex-start; /* top */ align-items: flex-end; /* bottom */ align-items: center; /* middle */ }
.container { display: flex; justify-content: center; /* horizontal center */ align-items: center; /* vertical center */ height: 100vh; /* full viewport height */ }
This is the easiest way to center anything!
.container { display: flex; flex-wrap: nowrap; /* default: all on one line */ flex-wrap: wrap; /* wrap to next line */ }
.container { display: flex; gap: 20px; /* space between items */ row-gap: 10px; /* vertical gap only */ column-gap: 20px; /* horizontal gap only */ }
.item { flex-grow: 1; /* grow to fill space (0 = don't grow) */ flex-shrink: 1; /* shrink if needed (0 = don't shrink) */ flex-basis: 200px; /* starting size */ /* Shorthand */ flex: 1; /* grow: 1, shrink: 1, basis: 0 */ flex: 0 0 200px; /* don't grow, don't shrink, 200px */ }
<nav class="navbar"> <a href="/">Logo</a> <ul class="nav-links"> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> </ul> </nav>
.navbar { display: flex; justify-content: space-between; align-items: center; padding: 1rem; }
.container { display: grid; grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */ grid-template-rows: 100px 200px; /* 2 rows */ gap: 20px; }
.container { display: grid; /* Fixed widths */ grid-template-columns: 200px 300px 200px; /* Flexible with fr (fraction) */ grid-template-columns: 1fr 2fr 1fr; /* Repeat */ grid-template-columns: repeat(3, 1fr); /* Auto-fit for responsive */ grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); }
.item { grid-column: 1 / 3; /* span columns 1-2 */ grid-row: 1 / 2; /* row 1 only */ /* Shorthand */ grid-column: span 2; /* span 2 columns */ grid-row: span 3; /* span 3 rows */ }
.container { display: grid; grid-template-areas: "header header header" "sidebar main main" "footer footer footer"; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } .main { grid-area: main; } .footer { grid-area: footer; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; padding: 20px; } .card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
Use Flexbox when:
Use Grid when: