Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Layout with Flexbox and Grid

Display, Position and Overflow

Learn how Tailwind v4 controls display types, relative, absolute, fixed and sticky positioning, inset and z-index, overflow, visibility and screen-reader-only text.

Before you reach for flexbox and grid, you need three basic tools that every layout relies on: display (how an element takes part in the flow), position (whether it is placed by the flow or by coordinates) and overflow (what happens when content does not fit). They are plain CSS properties, so if you have used them before, this lesson is mostly about the Tailwind class names. If any behavior surprises you, see the CSS display and position lessons.

Display

Class CSS
block display: block
inline display: inline
inline-block display: inline-block
flex, inline-flex flex container (next lesson)
grid, inline-grid grid container
hidden display: none
contents the element's box disappears, its children remain
flow-root creates a new block formatting context (contains floats, stops margin collapse)
table, table-cell, table-row table layout types

hidden removes the element from layout entirely, and block brings it back. Together with variants you get the most common responsive trick, showing something only at some sizes:

HTML
<nav class="hidden md:block">Desktop menu</nav>
<button class="md:hidden">Menu</button>

For visually hiding but keeping in layout, use invisible (visibility: hidden), and use opacity-0 for transparent-but-clickable. For content that screen readers need but sighted users do not, use sr-only:

HTML
<button>
  <svg aria-hidden="true" class="size-5">...</svg>
  <span class="sr-only">Close dialog</span>
</button>

sr-only hides text visually while leaving it available to assistive technology. Its opposite, not-sr-only, reverses it, for instance on focus to build skip links.

Position

The position property decides how an element is placed:

flowchart TB
  S["static<br/>normal flow (default)"]
  R["relative<br/>normal flow, can be nudged,<br/>anchors absolute children"]
  A["absolute<br/>removed from flow, placed<br/>against nearest positioned ancestor"]
  F["fixed<br/>placed against the viewport"]
  T["sticky<br/>flow, then sticks while scrolling"]

The five positions as utilities: static, relative, absolute, fixed and sticky.

You place a positioned element with the inset utilities: top-0, right-4, bottom-2, left-1/2, or shorthands inset-0 (all sides), inset-x-0, inset-y-0. They use the spacing scale and accept fractions, negatives (-top-2) and auto.

The pattern you will use all the time is "relative parent, absolute child":

Badge on a card (relative + absolute)

relative on the card makes it the anchor. The badge is placed relative to the card, up and right. If you forget relative, the badge jumps to the page corner, a very common mistake.

Fixed and sticky

  • fixed top-0 inset-x-0 pins a bar to the top of the viewport; remember that it leaves the flow, so add padding to the content below it.
  • sticky top-0 keeps an element in the normal flow, but once it would scroll out of view it sticks to the edge of its scroll container. It stops working if any ancestor has overflow: hidden, or if the parent is exactly as tall as the sticky child.

z-index

Stacking order uses z-0, z-10, z-20, z-30, z-40, z-50, plus z-auto and arbitrary values (z-[100]). It only affects positioned elements (and flex/grid children). Keep a small set of layers for the whole project, for example: header z-30, dropdown z-40, modal z-50.

Overflow

Overflow decides what happens to content larger than its box:

Class Behavior
overflow-visible content spills out (default)
overflow-hidden clipped, no scrollbar
overflow-auto scrollbars only when needed
overflow-scroll scrollbars always
overflow-clip clipped, no scrolling at all
overflow-x-auto, overflow-y-auto one axis

Typical uses: overflow-hidden rounded-xl clips an image to the card's rounded corners; overflow-x-auto makes a wide table scroll sideways on a phone; max-h-64 overflow-y-auto creates a scrolling panel. truncate (overflow-hidden text-ellipsis whitespace-nowrap) cuts one long line with "...".

Sticky header in a scroll area

The header stays visible because the scrolling container is the max-h-60 overflow-y-auto box and the sticky element lives inside it. z-10 keeps list items from scrolling over it.

Other everyday layout helpers

  • container sets width: 100% and a max-width equal to the current breakpoint. In v4, it is no longer centered or padded by default; combine with mx-auto px-4, or customize it with @utility container (chapter 7).
  • aspect-video, aspect-square, aspect-[4/3] reserve a ratio for images and embeds before they load.
  • object-cover, object-contain, object-center control how an image or video fits its box.
  • float-left, clear-both exist for wrapping text around images; use flexbox or grid for layout instead.
  • columns-2, columns-3 make newspaper-style text columns.

Common mistakes

  • Absolute child without a relative parent.
  • Sticky inside an overflow-hidden ancestor.
  • Using h-screen with fixed content and then wondering why the page scrolls under it: give the content padding for the bar.
  • Large z-[9999] values: pick a scale instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do you need on the parent so an absolute child is positioned relative to it?

    An absolutely positioned element is placed against the nearest positioned ancestor; relative makes the parent that anchor.
  2. 2. Which class hides text visually but keeps it for screen readers?

    hidden and invisible hide content from assistive technology too. sr-only is the accessible visually-hidden pattern.
  3. 3. Which class clips an image to its card's rounded corners?

    Rounded corners only clip children when the parent has overflow-hidden.
  4. 4. Why might sticky top-0 not stick?

    An ancestor with overflow other than visible becomes the scroll container, so the element sticks within it, or fails to stick at all.

Summary

  • block, inline-block, hidden, contents, flex and grid set display; sr-only hides text for sighted users only.
  • relative, absolute, fixed and sticky plus inset-* place elements; z-* orders layers.
  • overflow-* clips or scrolls; truncate handles long single lines.
  • aspect-*, object-* and container support everyday layouts.

Next: Flexbox