How Should You Build Layouts: Grid, Flexbox, or Positioning?

When building layouts in CSS, the two main tools you’ll rely on are Flexbox and Grid.
Don’t rely on position: absolute;
for everyday layouts.
Many beginners try to build entire pages with position: absolute
and fixed widths/heights.
This leads to fragile layouts that break on different screen sizes.
Use positioning only for fine-tuning or special effects (like tooltips, popups, or badges).
For general layouts, use Grid and Flexbox.
What Is CSS Flexbox?
Flexbox (Flexible Box Layout) is a one-dimensional layout system designed to arrange elements in a row or column and align them neatly.
Parent element → becomes a flex container with
display: flex;
Children → become flex items that adjust size and position automatically
Key Features of Flexbox
Easily center items (horizontally & vertically).
Control spacing between items.
Make layouts responsive without floats or manual positioning.
Best for lining things up neatly in one direction (1D).
Example: Navbar with Flexbox
nav {
display: flex;
justify-content: space-between; /* spread items */
align-items: center; /* vertical centering */
}
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
The items flow horizontally and stay evenly spaced, even if one link has more text.
Flex container properties (apply to the parent)
display: flex
/inline-flex
— create a flex formatting context.flex-direction
—row | row-reverse | column | column-reverse
→ main axis direction.flex-wrap
—nowrap | wrap | wrap-reverse
→ allow items to wrap to new lines.flex-flow
— shorthand forflex-direction
+flex-wrap
.justify-content
—flex-start | flex-end | center | space-between | space-around | space-evenly
→ distribute items on the main axis.align-items
—stretch | flex-start | flex-end | center | baseline
→ align items on the cross axis.align-content
—stretch | center | space-between | space-around | flex-start | flex-end
→ alignment of multiple rows (only when there is wrapping).gap
(androw-gap
,column-gap
) — spacing between flex items (modern and recommended).
Example: center content and space items horizontally
.container {
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
}
Flex item properties (apply to children)
order
— integer, controls visual order (does not change DOM order).flex-grow
— number, how much the item grows relative to others.flex-shrink
— number, how it shrinks when space is tight.flex-basis
— initial main-size before growing/shrinking (like starting width).flex
— shorthand:flex: <grow> <shrink> <basis>
(common:flex: 1
orflex: 0 1 auto
).align-self
— overridealign-items
for an individual item.min-width
/min-height
andmax-*
— useful to prevent items from collapsing or overflowing.margin: auto
— can be used to push items (e.g.,margin-left: auto
pushes an item to the right).
Important snippet (prevent overflow with long content):
.item {
flex: 1; /* share available space */
min-width: 0; /* allow the item to shrink properly (common gotcha) */
}
Common Flexbox patterns (with small examples)
Centered block:
.container { display:flex; justify-content:center; align-items:center; height:200px; }
Equal-width columns:
.row { display:flex; gap:16px; }
.col { flex:1; min-width:0; } /* min-width prevents overflow when text is long */
Push right (classic nav):
nav { display:flex; align-items:center; }
nav .spacer { margin-left:auto; } /* pushes following items to the right */
Flexbox pitfalls & pro tips
Use
gap
instead of manual margins for consistent spacing.min-width:0
on flex items is often needed to avoid overflow of long text.Avoid heavy use of
order
— it changes visual order but not DOM order (accessibility/readers).align-content
only applies when there are multiple lines (i.e., items wrapped).Test vertical centering with
align-items:center
— it solves most centering headaches.
What Is CSS Grid?
CSS Grid is a two-dimensional layout system that lets you create rows and columns to structure an entire page.
Parent element → becomes a grid container with
display: grid;
Children → become grid items placed in cells.
Key Features of Grid
Create layouts with both rows + columns.
Control gaps (gutters) between items.
Build complex, responsive designs with ease.
Example: Page Layout with Grid
.container {
display: grid;
grid-template-columns: 200px 1fr; /* sidebar + main */
grid-template-rows: 80px 1fr 60px; /* header, content, footer */
gap: 10px;
}
<div class="container">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main Content</main>
<footer>Footer</footer>
</div>
The header spans full width, sidebar stays on the left, main content grows, and footer sits below — all with just a few lines of CSS.
Grid container properties (apply to the grid parent)
display: grid
/inline-grid
— create grid context.grid-template-columns
/grid-template-rows
— define explicit tracks (e.g.,200px 1fr 100px
).grid-template-areas
— named areas for readable layouts.gap
,row-gap
,column-gap
— gutters between grid cells.grid-auto-rows
/grid-auto-columns
— size of implicitly created tracks.grid-auto-flow
—row | column | dense
— controls how auto-placed items flow.justify-items
/align-items
— alignment of items inside their cell.justify-content
/align-content
— alignment of the whole grid within the container.place-items
/place-content
— shorthand for the above.
Grid item / placement properties
grid-column
/grid-row
— placement in the grid (1 / -1
,span 2
).grid-area
— either a name (from template-areas) orrow-start / column-start / row-end / column-end
.justify-self
/align-self
— override cell alignment for the item.place-self
— shorthand forjustify-self
+align-self
.
Example: spanning full width
.header { grid-column: 1 / -1; } /* spans from first column to last */
Powerful functions and units (grid magic)
repeat()
→ DRY:grid-template-columns: repeat(3, 1fr);
fr
unit → fraction of remaining space (1fr
,2fr
).minmax(min, max)
→ enforce min/max sizes:grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
auto-fit
vsauto-fill
→ helpful for responsive card grids.fit-content()
/min-content
/max-content
→ intrinsic sizing helpers.subgrid
→ (advanced) inherit parent tracks — limited support in some environments.
Responsive Grid example (cards)
.cards {
display: grid;
gap: 16px;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
This creates a responsive card layout that adds/removes columns as the container width changes.
Grid pitfalls & pro tips
Use
gap
not margins between grid items.Use
minmax()
+auto-fit
orauto-fill
for responsive cards — this is often simpler than media queries.grid-auto-flow: dense
can backfill small items but may reorder DOM visual placement (use with care).Named areas (
grid-template-areas
) make complex templates readable — great for maintainability.Negative indices in
grid-column: 1 / -1
are powerful;-1
is the last line.Debugging tip: temporarily apply borders/backgrounds or use the browser’s grid inspector to see tracks and areas.
Respect DOM order for accessibility — avoid reordering content solely with grid unless necessary.
How Do Flexbox and Grid Work Together?
Think of them as teammates:
Grid = the overall scaffolding (page skeleton).
Flexbox = the fine-tuning (aligning items inside each section).
Example: Use Grid to divide the page into header, sidebar, main, footer. Then use Flexbox inside the header to neatly space out the logo and navigation.
Mini Webpage Demo: Grid + Flexbox
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid + Flexbox Demo</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: sans-serif; background: #f4f4f4; }
/* GRID LAYOUT */
.container {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: 70px 1fr 50px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
height: 100vh;
gap: 10px;
}
header { grid-area: header; background: #0077cc; color: white; padding: 0 1rem; }
aside { grid-area: sidebar; background: #e0e0e0; padding: 1rem; }
main { grid-area: main; background: white; padding: 1rem; }
footer { grid-area: footer; background: #333; color: white; text-align: center; padding: 1rem; }
/* FLEXBOX INSIDE HEADER */
header {
display: flex;
justify-content: space-between;
align-items: center;
}
nav a {
color: white;
margin-left: 1rem;
text-decoration: none;
font-weight: bold;
}
nav a:hover { text-decoration: underline; }
</style>
</head>
<body>
<div class="container">
<header>
<h1>MySite</h1>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
</header>
<aside>
<p><strong>Sidebar</strong></p>
<ul>
<li>Dashboard</li>
<li>Settings</li>
<li>Profile</li>
</ul>
</aside>
<main>
<h2>Main Content</h2>
<p>This is the main area of the page. Grid places it in its own cell, while Flexbox spaces out the header items above.</p>
</main>
<footer>
<p>© 2025 MySite</p>
</footer>
</div>
</body>
</html>
Key Takeaway:
Grid = Skeleton → Use it for the big structure (header, sidebar, main, footer).
Flexbox = Muscles → Use it to align items neatly inside those sections.
Positioning = Special Effects → Use only for extras like tooltips, popups, or badges (not for main layouts).
Pro Tips & Tricks
Start with Grid for page layout → Divide your page into header, sidebar, main, and footer.
Use Flexbox inside grid areas → Perfect for navbars, buttons, or centering text.
Avoid
position: absolute
for layouts → It breaks responsiveness. Save it for overlays or floating elements.Combine Grid + Flexbox → Grid makes the “map”, Flexbox handles the “details.”
Try
gap
instead of margins → Works in both Grid and Flexbox to add spacing cleanly.Use
align-items: center;
in Flexbox → Quick way to vertically center without hacks.Responsive Trick → Use
fr
units in Grid andflex: 1
in Flexbox for automatic resizing.
Join the conversation
Sign in to share your thoughts and engage with other readers.
No comments yet
Be the first to share your thoughts!