Modern CSS Layout: Grid, Flexbox, and Container Queries
Evolution of Layout Approaches
| Era | Approach | Issues |
|---|---|---|
| Table layout | <table> |
Wrong semantics, inflexible |
| Float layout | float + clearfix |
Collapsed height, complexity |
| Positioning | position: absolute |
Out of document flow |
| Flexbox | display: flex |
One-dimensional ✅ |
| CSS Grid | display: grid |
Two-dimensional ✅ |
| Container Queries | @container |
Component-level responsive ✅ |
Flexbox: One-Dimensional Layout
Best for nav bars, tool card rows, button groups—single row or column:
.tool-grid {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.tool-card {
flex: 1 1 280px; /* grow shrink basis */
min-width: 0; /* prevent overflow */
}
Common patterns
/* Center horizontally and vertically */
.center { display: flex; justify-content: center; align-items: center; }
/* Space between ends */
.navbar { display: flex; justify-content: space-between; align-items: center; }
/* Vertical stack */
.sidebar { display: flex; flex-direction: column; gap: 0.5rem; }
ToolsKu’s home page tool grid uses flex-wrap + flex-basis: 280px for adaptive columns.
CSS Grid: Two-Dimensional Layout
Best for page shells, category pages—rows and columns together:
.page-layout {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.sidebar { grid-row: 1 / -1; grid-column: 1; }
.header { grid-column: 2; }
.main { grid-column: 2; overflow: auto; }
.footer { grid-column: 2; }
Auto-fit grid
.tool-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1rem;
}
auto-fill + minmax computes column count automatically—4 columns when wide, 2 when narrow, no media queries needed.
Container Queries: Component-Level Responsive
Media queries use viewport width; Container Queries use parent container width:
.tool-card-container {
container-type: inline-size;
}
@container (max-width: 400px) {
.tool-card {
flex-direction: column;
padding: 0.75rem;
}
.tool-card .icon { display: none; }
}
@container (min-width: 401px) {
.tool-card {
flex-direction: row;
padding: 1.25rem;
}
}
Benefit: The same component adapts in a narrow sidebar vs a wide main area without global media queries.
ToolsKu Layout in Practice
Tool page layout
┌──────────────────────────────────┐
│ Header (flex, space-between) │
├────────┬─────────────────────────┤
│ Side │ Tool Area (grid) │
│ nav │ ┌─────┐ ┌─────┐ │
│ (grid │ │Input│ │Output│ │
│ 1fr) │ └─────┘ └─────┘ │
│ │ ┌─────────────────┐ │
│ │ │ Action Bar │ │
│ │ └─────────────────┘ │
├────────┴─────────────────────────┤
│ Footer │
└──────────────────────────────────┘
Responsive breakpoints
| Breakpoint | Layout change |
|---|---|
| < 640px | Single column; sidebar becomes drawer |
| 640–1024px | Two-column tool grid |
| > 1024px | Three-column grid + fixed sidebar |
Handy CSS Tools
- Unit converter: px ↔ rem ↔ em
- Aspect ratio calculator: simplest ratio
- Color contrast: WCAG compliance check
Summary
Flexbox handles one dimension, Grid handles two, Container Queries handle component-level responsiveness—together they cover modern web layout. ToolsKu’s UI is a practical application of this stack.
Try these browser-local tools — no sign-up required →