Master modern alignment with Flexbox and Grid for powerful layouts
CSS3 introduced Flexbox and Grid, revolutionizing how we create layouts. These powerful tools make alignment, distribution, and responsive design much easier than traditional methods.
Flexbox is a one-dimensional layout system for arranging items in rows or columns.
/* Make element a flex container */
.container {
display: flex;
}
/* Flex direction */
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 alignment along the main axis (horizontal in row, vertical in column).
/* Main axis alignment */
justify-content: flex-start; /* Start (default) */
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 */
Controls alignment along the cross axis (vertical in row, horizontal in column).
/* Cross axis alignment */
align-items: stretch; /* Default: fill container */
align-items: flex-start; /* Top (in row) */
align-items: flex-end; /* Bottom (in row) */
align-items: center; /* Center */
align-items: baseline; /* Align text baselines */
Combine justify-content and align-items to center elements perfectly.
/* Perfect centering with Flexbox */
.center-container {
display: flex;
justify-content: center; /* Horizontal */
align-items: center; /* Vertical */
height: 100vh; /* Full viewport height */
}
/* Center multiple items */
.center-box {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column; /* Stack vertically */
gap: 20px;
}
Controls whether flex items wrap to new lines.
/* Wrapping behavior */
flex-wrap: nowrap; /* Default: single line */
flex-wrap: wrap; /* Wrap to multiple lines */
flex-wrap: wrap-reverse; /* Wrap in reverse */
/* Shorthand for direction + wrap */
flex-flow: row wrap; /* direction + wrap */
Modern way to add spacing between flex/grid items.
/* Gap between items */
.container {
display: flex;
gap: 20px; /* Equal gap */
gap: 20px 10px; /* row-gap column-gap */
}
/* Individual gaps */
row-gap: 20px;
column-gap: 10px;
Grid is a two-dimensional layout system for rows and columns.
/* Basic grid */
.grid-container {
display: grid;
grid-template-columns: 200px 200px 200px;
grid-template-rows: 100px 100px;
gap: 20px;
}
/* Fractional units (fr) */
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* Equal columns */
grid-template-columns: 2fr 1fr; /* 2:1 ratio */
}
/* Repeat notation */
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-columns: repeat(4, 200px); /* 4 x 200px columns */
}
Create responsive grids without media queries.
/* Responsive grid with auto-fit */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
/* auto-fill vs auto-fit:
- auto-fill: Creates empty tracks
- auto-fit: Collapses empty tracks
*/
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
Grid uses similar alignment properties to Flexbox.
/* Align entire grid */
justify-content: center; /* Horizontal */
align-content: center; /* Vertical */
/* Align items within cells */
justify-items: center; /* Horizontal */
align-items: center; /* Vertical */
/* Align individual item */
.item {
justify-self: center; /* Horizontal */
align-self: center; /* Vertical */
}
/* Shorthand for both */
place-items: center; /* align-items + justify-items */
place-content: center; /* align-content + justify-content */
place-self: center; /* align-self + justify-self */
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
background: white;
}
.nav-links {
display: flex;
gap: 2rem;
list-style: none;
}
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
}
.card {
background: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.hero {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
gap: 2rem;
}
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: auto 1fr;
height: 100vh;
gap: 1rem;
}
.sidebar {
grid-column: 1;
grid-row: 1 / -1; /* Span all rows */
}
.header {
grid-column: 2;
grid-row: 1;
}
.main {
grid-column: 2;
grid-row: 2;
}
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
.modal {
background: white;
padding: 2rem;
border-radius: 10px;
max-width: 500px;
}
Q1: Which property centers items horizontally in Flexbox (row)?
Q2: What does "fr" unit stand for in CSS Grid?
Q3: How do you perfectly center an element with Flexbox?
Q4: Which creates a responsive grid without media queries?
Q5: When should you use Flexbox over Grid?