CSS Layout Techniques in 2026: Grid, Flexbox & Beyond
CSS layout has never been more powerful or more confusing. Between Grid, Flexbox, Container Queries, Subgrid, and a dozen other features, choosing the right approach for each layout problem requires understanding what each tool does best. The good news: the days of float hacks and clearfix are over. The modern CSS layout toolkit is expressive, predictable, and well-supported across browsers.
This guide covers the major layout methods available in 2026, when to use each one, and how they work together. Experiment with layouts in real time using our CSS Grid Generator and Flexbox Playground.
CSS Grid: The Two-Dimensional Layout Engine
CSS Grid is designed for two-dimensional layouts where you control both rows and columns simultaneously. It is the right tool for page-level layout, card grids, dashboards, and any design where items need to align on both axes.
Key Grid Concepts
- Explicit tracks: Define columns and rows with
grid-template-columnsandgrid-template-rows. Usefrunits for flexible proportions:grid-template-columns: 1fr 2fr 1frcreates a 3-column layout where the middle is twice as wide. - Implicit tracks: Rows created automatically when content overflows the explicit grid. Control their size with
grid-auto-rows. - Gap: The
gapproperty creates consistent spacing between grid items without margin hacks.gap: 1rem 2remsets row and column gaps separately. - Named areas:
grid-template-areaslets you define layout regions by name, making complex layouts readable and maintainable. - Auto-placement:
repeat(auto-fit, minmax(250px, 1fr))creates responsive grids without media queries. Items wrap to new rows automatically when space is insufficient.
/* Responsive card grid - no media queries needed */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.5rem;
}
/* Page layout with named areas */
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 250px 1fr;
grid-template-rows: auto 1fr auto;
}
Our CSS Grid Generator lets you visually design grid layouts and export production-ready CSS.
CSS Flexbox: The One-Dimensional Layout Model
Flexbox excels at distributing space along a single axis - either a row or a column. It is the right tool for navigation bars, form layouts, centering content, and any pattern where items need to share space in one direction.
Key Flexbox Concepts
- Main axis vs. cross axis:
flex-directionsets the main axis (row or column). Items are distributed along the main axis and aligned along the cross axis. - flex shorthand:
flex: 1means the item grows to fill available space.flex: 0 0 200pxmeans fixed at 200px with no growing or shrinking. - justify-content: Distributes space along the main axis.
space-betweenpushes items to the edges;centergroups them in the middle. - align-items: Aligns items along the cross axis.
centeris the most common value and solves the classic vertical centering problem. - flex-wrap:
wrapallows items to flow to the next line when they exceed container width.
/* Center anything vertically and horizontally */
.center {
display: flex;
justify-content: center;
align-items: center;
}
/* Navigation bar with logo left, links right */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 1rem;
}
Try different Flexbox configurations in our Flexbox Playground to see how each property affects layout.
Container Queries: Component-Level Responsiveness
Container Queries are the most significant addition to CSS layout since Grid. They let components respond to the size of their parent container rather than the viewport, enabling truly reusable components that adapt to any context.
/* Define a containment context */
.card-container {
container-type: inline-size;
container-name: card;
}
/* Style based on container width, not viewport */
@container card (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}
@container card (max-width: 399px) {
.card {
display: flex;
flex-direction: column;
}
}
This means the same card component automatically switches between vertical and horizontal layout depending on where it is placed - in a sidebar (narrow), main content (medium), or full-width hero (wide) - without any JavaScript or context-specific CSS classes.
Subgrid: Nested Alignment
CSS Subgrid solves a long-standing problem: aligning nested grid items with the parent grid's tracks. Without subgrid, each nested grid is independent and cannot reference the parent's column or row sizes.
/* Parent grid */
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1.5rem;
}
/* Card uses parent's row tracks for alignment */
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid; /* inherits parent's row tracks */
}
/* Now title, content, and footer align across all cards */
Subgrid is especially useful for card grids where you want every card's title, description, and call-to-action button to align perfectly regardless of content length.
Grid vs. Flexbox: When to Use Each
| Use Case | Best Tool | Why |
|---|---|---|
| Page layout (header, sidebar, main, footer) | Grid | Named areas, 2D control |
| Card grids | Grid | auto-fit/minmax for responsiveness |
| Navigation bar | Flexbox | Single-axis distribution |
| Centering content | Flexbox | 2 lines: display:flex + place-items |
| Form layout | Grid | Label-input alignment across rows |
| Toolbar/button group | Flexbox | Inline items with gap |
| Dashboard with mixed sizes | Grid | Spanning rows/columns |
In practice, most layouts combine both. Use Grid for the overall page structure and Flexbox for individual component internals.
Modern CSS Techniques Worth Knowing
- clamp() for fluid sizing:
font-size: clamp(1rem, 2.5vw, 2rem)creates responsive typography without media queries. Works for margins, padding, and gaps too. - aspect-ratio:
aspect-ratio: 16/9maintains proportions without the old padding-top hack. Perfect for video containers, images, and card thumbnails. - :has() selector: Parent selection based on children.
.card:has(img)styles cards that contain images differently from text-only cards. - Logical properties:
margin-inline,padding-block,inset-inline-startreplace directional properties for better internationalization support. - Scroll-driven animations: CSS-only scroll-linked effects without JavaScript. Progress bars, parallax, and reveal animations tied to scroll position.
Build Your Layouts
Our CSS Grid Generator lets you visually design grid layouts with drag-and-drop, then copy production-ready code. The Flexbox Playground shows how every Flexbox property affects your layout in real time. Both tools run 100% in your browser.