Sub topic 13
When to Use Positioning, Flexbox, or Grid
This chapter assumes you know HTML and CSS selectors, the box model, and the five position values (static, relative, absolute, fixed, sticky). It helps you pick the right layout tool for a task.
Three tools, three jobs
Each tool answers a different question.
- Positioning answers: where does this one element go relative to something else, or to the viewport?
- Flexbox answers: how do I arrange items along one line (a row or a column) and share the space between them?
- Grid answers: how do I arrange items in rows and columns at the same time?
Flexbox and Grid keep their items in the flow. The container sizes itself around them, and neighbors respond when an item grows. An absolutely positioned element is removed from the flow. Its siblings ignore it, and the parent does not grow to contain it.
Deciding by the task
| Task | Best tool | Reason |
|---|---|---|
| Nav bar with a logo left and links right | Flexbox | One-dimensional, items share space |
| Page with header, sidebar, content, footer | Grid | Two-dimensional |
| Card gallery with equal columns | Grid | Rows and columns line up |
| "New" badge on a card corner | Absolute positioning | Overlaps another element, no flow space needed |
| Header that stays visible while scrolling | Sticky | Depends on scroll position |
| Modal and backdrop | Fixed | Tied to the viewport |
| Centering one item in a box | Flexbox or Grid | Simpler than offsets and transforms |
Why not use positioning for everything?
You can build a whole page with absolute positioning, but the result is fragile. Because absolute elements do not take up space, you must set sizes and offsets by hand. If text wraps to an extra line, the next element does not move down. It overlaps.
Compare two ways to center a box. The first uses the offset-and-transform technique from the modal pattern:
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}The second uses Grid, and the child stays in the flow:
.stage {
display: grid;
place-items: center;
min-height: 100vh;
}For a modal, fixed positioning is still right, because the modal must sit above the page and ignore scrolling. For centering content inside a normal section, Grid is shorter and easier to maintain.
Combining the tools
Real pages mix all three. Grid or Flexbox lays out the structure, and positioning handles the exceptions that overlap or pin.
<div class="cards">
<article class="card">
<span class="badge">New</span>
<h2>Title</h2>
<p>Card text</p>
</article>
<article class="card">
<h2>Another title</h2>
<p>More text</p>
</article>
</div>
<style>
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
.card {
position: relative;
padding: 1rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>Grid places the cards. Each card is position: relative, which makes it the containing block for its badge. The badge is absolute, so it overlaps the corner without disturbing the text.
Common mistakes
- Using offsets to nudge things into place. If you reach for
topandleftto line up siblings, trygap,align-items, orjustify-contentfirst. - Forgetting that absolute elements leave the flow. The parent collapses around them, or later content slides underneath.
- Using
position: relativeand offsets to make space. The element moves visually, but its original space stays reserved. Use margin orgapto change spacing. - Putting positioning and layout on the same element without a plan. A flex or grid item that is also absolute stops taking part in the container's layout. Its offsets are then relative to its containing block.
Exercises
Exercise
A product page needs a row of five thumbnails that wrap onto new lines on narrow screens, plus a "Sale" label in the corner of each thumbnail. Which tool handles each part?
Show answer
Use Flexbox (with flex-wrap) or Grid for the row of thumbnails, since they are laid out in the flow. Use position: relative on each thumbnail and position: absolute on the "Sale" label, because the label overlaps the thumbnail and needs no space of its own.
Exercise
A developer centers a paragraph inside a section with position: absolute, top: 50%, left: 50%, and a transform. Afterward the section collapses to zero height. Why, and what is a simpler fix?
Show answer
The absolute paragraph is removed from the flow, so the section has no in-flow content to give it height. A simpler fix is to make the section a Grid container with display: grid and place-items: center, and to give it a height or min-height. The paragraph then stays in the flow.