Tailwind CSS/layout
Layout & Positioning
Before reaching for flexbox or grid, you need Tailwind's core layout utilities: display, position, z-index, overflow and visibility. They decide how elements flow, stack and ove...
3 min readtailwind#tailwind#layout#positioning
Introduction
Before reaching for flexbox or grid, you need Tailwind's core layout utilities: display, position, z-index, overflow and visibility. They decide how elements flow, stack and overlap on the page.
Subtopics
- Display:
block,inline-block,hidden,flex,grid - Position:
static,relative,absolute,fixed,sticky - Offsets:
top-*,inset-*,start-* - Stacking with
z-* - Overflow and scrolling
containerutility- Floating and columns (
columns-*)
Syntax
html
<div class="hidden md:block">Visible from md up</div>
<div class="relative">
<span class="absolute top-2 right-2">Badge</span>
</div>
<header class="sticky top-0 z-50 bg-white">Sticky header</header>
<div class="fixed inset-x-0 bottom-0">Bottom bar</div>
<div class="absolute inset-0 grid place-items-center">Perfectly centered</div>
<div class="max-h-64 overflow-y-auto">Scrollable area</div>
<div class="columns-3 gap-6">Newspaper-style columns</div>
absolutepositions relative to the nearestrelativeancestor.inset-0=top:0; right:0; bottom:0; left:0.sticky top-0sticks the element when scrolling past it.z-10,z-50control stacking order (only affects positioned elements).containersetsmax-widthat each breakpoint (center it withmx-auto).
Important Properties
| Utility | Purpose | Syntax | Example |
|---|---|---|---|
block / hidden | Display value | {display} | hidden |
relative / absolute | Positioning | {position} | absolute |
inset-* | All offsets | inset-{n} | inset-0 |
z-* | Stack order | z-{n} | z-50 |
overflow-* | Overflow | overflow-{value} | overflow-hidden |
container | Responsive max-width | container mx-auto | page wrapper |
Common Use Cases
- Beginner: show or hide content with
hiddenandmd:block. - Practical UI: place a notification dot on an avatar with
relativeparent +absolutechild. - Real-world: a sticky site header with
sticky top-0 z-50 backdrop-blur.
Common Errors
- ❌
absolutechild escaping to the page corner → ✅ addrelativeto the intended parent. - ❌
z-50having no effect → ✅ the element needs a position (relative,absolute,fixed,sticky) — flex/grid children are also affected byz. - ❌
stickydoing nothing → ✅ give ittop-0(or another offset) and make sure no ancestor hasoverflow: hidden. - ❌
containernot centered → ✅ usecontainer mx-auto px-4.
Common Mistakes
- Overusing
absolutefor layouts that flex or grid solve more cleanly. - Giant z-index values such as
z-[99999]instead of a small consistent scale. - Hiding important content with
hiddenand leaving screen-reader users without it (usesr-onlyfor visually hidden text). - Forgetting mobile when using
fixedelements that can cover content.
Quick Reference
html
<div class="relative h-64 overflow-hidden rounded-xl">
<img class="absolute inset-0 size-full object-cover" src="/photo.jpg" alt="" />
<div class="absolute inset-x-0 bottom-0 bg-black/50 p-4 text-white">Caption</div>
</div>