Sub topic 13
When to use positioning, Flexbox, or Grid
This chapter assumes you know normal flow, the position values (relative, absolute, fixed, sticky), containing blocks, and z-index. It also assumes you have written basic display: flex and display: grid rules. Here you will learn how to choose among the three tools, and how to combine them.
Three tools, three jobs
All three tools can put a box in a particular place, but they answer different questions.
| Tool | Question it answers | Takes part in sizing its container? |
|---|---|---|
| Normal flow with Flexbox | How do these items share space along one line? | Yes |
| Grid | How do I divide space into rows and columns, and place items in them? | Yes |
| Positioning (`absolute`, `fixed`) | Where does this one box go relative to something else? | No, it is removed from flow |
| Positioning (`relative`, `sticky`) | How do I nudge or pin a box without changing the space it takes? | Yes, its original space is kept |
Flexbox and Grid are layout systems. The container knows about its children, measures them, and distributes space. Positioning is a placement tool. An absolutely positioned box is invisible to its parent's sizing, so the parent does not grow to contain it and siblings do not make room for it.
Why positioning is a poor default for layout
Imagine building a toolbar with a title on the left and two buttons on the right, using absolute positioning.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fragile toolbar</title>
<style>
.toolbar { position: relative; height: 3rem; background: #eee; }
.title { position: absolute; left: 1rem; top: 0.75rem; }
.actions { position: absolute; right: 1rem; top: 0.5rem; }
</style>
</head>
<body>
<div class="toolbar">
<span class="title">Inbox</span>
<span class="actions"><button>Edit</button> <button>Delete</button></span>
</div>
</body>
</html>This works only while three things hold:
- The toolbar height is hard-coded, because the children cannot make it grow.
- The
topvalues are tuned by eye to center the text. - The title and buttons never collide, because nothing prevents overlap when the title gets longer or the viewport narrows.
The Flexbox version states the intent instead of the coordinates.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flex toolbar</title>
<style>
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 0.5rem 1rem;
background: #eee;
}
</style>
</head>
<body>
<div class="toolbar">
<span class="title">Inbox</span>
<span class="actions"><button>Edit</button> <button>Delete</button></span>
</div>
</body>
</html>The height follows the content, vertical centering is one declaration, and gap stops items touching. If the title wraps or the text size changes, the layout adapts. This is the general rule: if several boxes must relate to each other, use a layout system. If one box must relate to something else, use positioning.
Choosing between Flexbox and Grid
Flexbox is content-driven and one-dimensional. It lays items along a main axis, and the items decide how much space they want. Use it for toolbars, nav bars, button groups, centering one thing, and rows of items that may wrap.
Grid is container-driven and two-dimensional. You define tracks (rows and columns), then place items into them. Use it for page skeletons, card galleries where rows and columns must align, dashboards, and forms with labels aligned in a column.
Two quick tests help:
- If you want items in different rows to line up in columns, use Grid. Wrapped Flexbox rows size each row independently, so columns do not align.
- If you want the content to decide each item's size and the container simply distributes the rest, use Flexbox.
They nest well. A common structure is a Grid page skeleton, with Flexbox inside each region for its toolbar or button row.
What positioning is still best at
Some jobs have no layout-system equivalent, because they depend on taking a box out of flow or tying it to the viewport or scroll position.
- Corner badges and decorations: a small box pinned to a corner of a card. Use
position: relativeon the card andposition: absoluteon the badge. - Overlays and modals: a backdrop with
position: fixed; inset: 0, plus a dialog above it. - Sticky headers and sidebars:
position: stickyreacts to scrolling, which Flexbox and Grid cannot do. - Tooltips and dropdowns: a box attached to a trigger element but not affecting surrounding layout.
- Small nudges:
position: relativewith an offset to shift an icon a pixel or two without disturbing neighbors.
A useful test is whether the box should reserve space. If yes, it belongs in the layout system. If it should float over the content, it is a positioning job.
Combining the tools
Real pages mix all three. This page uses Grid for the skeleton, Flexbox for a button row, and positioning for a badge and a sticky sidebar.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Combined layout</title>
<style>
body {
margin: 0;
display: grid;
grid-template-columns: 12rem 1fr;
gap: 1rem;
}
aside {
position: sticky;
top: 1rem;
align-self: start;
background: #f4f4f4;
padding: 1rem;
}
.card {
position: relative;
padding: 1rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
background: tomato;
color: white;
padding: 0.25rem 0.5rem;
}
.actions {
display: flex;
gap: 0.5rem;
justify-content: flex-end;
}
</style>
</head>
<body>
<aside>Sidebar</aside>
<main>
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
<div class="actions"><button>Save</button><button>Share</button></div>
</div>
<div style="height: 150vh">Tall content so the page scrolls.</div>
</main>
</body>
</html>Each tool does the job it is suited for:
- Grid creates the two columns.
- Sticky keeps the sidebar visible as the page scrolls.
- The card is a positioned ancestor, so the absolute badge sits in its corner.
- Flexbox right-aligns the buttons and spaces them.
Edge cases when the tools meet
align-self: starton a sticky grid item. Grid items stretch by default, so the sidebar would be as tall as the whole row and have no room to move. Aligning it to the start lets it be only as tall as its content, so it can slide within the row. The same applies to flex items under the defaultalign-items: stretch.- A sticky item is limited by its containing block. In a grid, that is the item's grid area. A sticky header placed in its own short row cannot stick past the end of that row. Put sticky elements in a taller region, or make the row span the content.
- Absolute children of flex and grid containers. An absolutely positioned child does not take part in flex or grid layout. It gets no flexible size and does not occupy a cell. It is placed against its containing block, which needs
position: relativeon the container if you want the container to be that block. z-indexworks on flex and grid items withoutposition. Items in a flex or grid container can usez-indexeven whenpositionisstatic. Elsewhere,z-indexon a static element is ignored.- Overlap without positioning. Grid lets several items share the same cell, which is a clean way to put a caption over an image.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Grid overlap</title>
<style>
.hero { display: grid; }
.hero > * { grid-area: 1 / 1; }
.hero img { width: 100%; height: 12rem; object-fit: cover; }
.hero .caption {
align-self: end;
padding: 0.5rem 1rem;
background: rgba(0, 0, 0, 0.6);
color: white;
}
</style>
</head>
<body>
<div class="hero">
<img src="https://example.com/photo.jpg" alt="Mountain lake">
<div class="caption">Lake at dawn</div>
</div>
</body>
</html>The container sizes itself to the image, because both children are in flow. An absolutely positioned caption would also work, but the grid version keeps the caption in the layout, so the container's height still accounts for it if it grows taller than the image.
A quick selection checklist
- Does the box need to stay on screen while the page scrolls? Use
fixedorsticky. - Does it overlay a specific parent without taking space? Use
absoluteinside arelativeparent. - Are several siblings sharing space along one axis? Use Flexbox.
- Do rows and columns need to line up? Use Grid.
- Do you only need to move one box a few pixels visually? Use
relativewith an offset, ortransform.
Exercise
A product card shows an image, a title, and a price, and a small "Sale" label in the card's top-left corner. The card must grow if the title wraps to two lines. Which tools do you use for the card's contents and for the label, and why?
Show answer
Make the card position: relative and use position: absolute; top: 0; left: 0 on the label, because the label floats over the card and should not take space. Lay out the image, title, and price with normal flow or Flexbox (flex-direction: column), so the card's height follows its content and grows when the title wraps.
Exercise
Your sidebar has position: sticky; top: 1rem inside a two-column grid, but it never sticks. The main column is very tall. What is the likely cause and the fix?
Show answer
Grid items stretch to the full row height by default, so the sidebar is as tall as its containing grid area and has no room to move. Add align-self: start to the sidebar so it is only as tall as its content and can slide within the row.
Exercise
You want a caption over the bottom of an image, and the container must still be sized by the image. Name two ways to do it and one reason you might prefer the grid approach.
Show answer
Put the caption in a position: absolute; bottom: 0 box inside a position: relative wrapper, or place both image and caption in the same grid cell with grid-area: 1 / 1 and align-self: end on the caption. The grid approach keeps the caption in the layout, so the container accounts for its size instead of letting it overflow or be clipped.