Tutorials Tailwind CSS Display Utilities
Ch 7 / 45
Chapter 7 Beginner 5 min read Tailwind CSS

Display Utilities

The display property is the most fundamental CSS layout property — it controls how an element participates in the document flow. Tailwind provides a utility class for every display value: block, inline, inline-block, flex, inline-flex, grid, inline-grid, hidden, contents, flow-root, and the full set of table utilities. Mastering these unlocks everything that comes next — flexbox, grid, and positioning.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
Every Tailwind display class and its CSS equivalent — block through hidden
Visual difference between block, inline, and inline-block behaviour
When to use flex vs inline-flex, and grid vs inline-grid
hidden — removing elements from layout entirely vs CSS visibility
Responsive show/hide patterns — desktop-only, mobile-only, and range visibility
contents — removing a wrapper from layout without removing from the DOM
flow-root — creating a block formatting context to contain floats
Table display utilities for semantic and layout table patterns

All Display Classes at a Glance

Tailwind maps every CSS display value to a utility class of the same name. These are the most commonly used — we'll go deep on each in the sections below.

block
display: block
Full-width block element. Stacks vertically. Respects width, height, margins.
inline
display: inline
Flows with text. Width and height have no effect. Vertical margin ignored.
inline-block
display: inline-block
Flows inline like text but respects width, height, padding, and all margins.
flex
display: flex
Block-level flex container. Children become flex items. Full-width by default.
inline-flex
display: inline-flex
Inline flex container — shrink-wraps to content, sits inside text flow.
grid
display: grid
Block-level grid container. Add grid-cols-* to define columns. Full-width by default.
inline-grid
display: inline-grid
Inline grid container — shrinks to content width, sits inline with other elements.
hidden
display: none
Removes the element from layout entirely — takes up no space, not accessible.
contents
display: contents
Removes the box but keeps children — they participate in the parent's layout.

block, inline & inline-block

These three values are the foundation of CSS layout. Understanding the difference between them is essential before learning flex and grid.

block — each item takes full width, stacks vertically
block item 1
block item 2
block item 3
inline — items flow with text, no width/height control
Text before inline 1 text between inline 2 text after
inline-block — flows inline but respects width, height, padding
ib item 1
ib item 2
tall item
ib item 4
block, inline, inline-block — usage patterns
<!-- BLOCK — full width, stacks vertically -->
<span class="block">Force a span to behave like a div</span>
<a    class="block w-full py-3 text-center">Full-width link button</a>
<img  class="block w-full h-auto" src="...">   <!-- removes inline gap under images -->

<!-- INLINE — flows with text, no sizing control -->
<div  class="inline">Force a div to flow inline with text</div>
<span class="inline text-cyan-500">Highlighted word</span> in a sentence.

<!-- INLINE-BLOCK — inline flow + full box model -->
<span class="inline-block px-4 py-2 bg-cyan-500 text-white rounded-lg">
  Badge with padding
</span>
<a    class="inline-block px-6 py-3 bg-blue-500 text-white rounded-xl">
  Pill button — not full-width
</a>

<!-- Practical: make image links block so they don't have gap -->
<a class="block" href="...">
  <img class="block w-full" src="..." alt="...">
</a>

flex & inline-flex

flex and inline-flex both create a flex container — children become flex items. The only difference is how the container itself participates in the surrounding layout.

flex — block-level container, stretches full width
flex item 1
flex item 2
flex item 3
inline-flex — shrinks to content, sits inline
Text before inline-flex badge 3 text after
flex vs inline-flex — when to use each
<!-- flex — full-width container, use for layout sections -->
<div class="flex items-center justify-between gap-4">
  <span class="font-bold">Logo</span>
  <nav  class="flex gap-6">Nav links</nav>
</div>

<!-- inline-flex — sits inside text flow, perfect for badges/buttons -->
<span class="inline-flex items-center gap-1.5 px-3 py-1
             bg-cyan-100 text-cyan-700 rounded-full text-sm font-medium">
  <iconify-icon icon="solar:check-circle-bold" width="14"></iconify-icon>
  Active
</span>

<!-- inline-flex for icon + text buttons that sit inside a sentence -->
<a class="inline-flex items-center gap-2 text-cyan-500 hover:text-cyan-600" href="...">
  Learn more
  <iconify-icon icon="solar:arrow-right-linear" width="14"></iconify-icon>
</a>

<!-- ── Quick rule ─────────────────────────────────────── */
<!-- Use flex for layout containers (navbars, card rows)   -->
<!-- Use inline-flex for components inside text or lists   -->

grid & inline-grid

grid creates a block-level grid container. inline-grid creates an inline grid that shrinks to its content. Both need grid-cols-* to define columns — without it they behave like a single-column block.

grid grid-cols-3 — block-level, full width, 3 equal columns
grid item 1
grid item 2
grid item 3
grid item 4
grid item 5
grid item 6
grid vs inline-grid
<!-- grid — block-level, stretches full parent width -->
<div class="grid grid-cols-3 gap-6">
  <div>Card 1</div>
  <div>Card 2</div>
  <div>Card 3</div>
</div>

<!-- Responsive grid — 1 col → 2 → 3 -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
  ...
</div>

<!-- inline-grid — shrinks to content, rare but useful -->
<span class="inline-grid grid-cols-2 gap-1 p-2 bg-gray-100 rounded-lg">
  <span class="text-xs bg-white rounded p-1 text-center">A</span>
  <span class="text-xs bg-white rounded p-1 text-center">B</span>
</span>

<!-- grid without cols — single column stack (same as block) -->
<div class="grid gap-4">
  <div>Stacked item 1</div>
  <div>Stacked item 2 — gap-4 adds spacing between</div>
</div>

hidden — Removing Elements from Layout

hidden sets display: none, which completely removes the element from the document layout — it takes up no space and screen readers cannot access it. This is different from invisible (opacity 0 — still takes space) and sr-only (visually hidden but accessible to screen readers).

Class CSS Visible? Takes Space? Screen Reader
hidden display: none ✗ No ✗ No ✗ Not accessible
invisible visibility: hidden ✗ No ✓ Yes ✗ Not accessible
opacity-0 opacity: 0 ✗ No ✓ Yes ✓ Accessible
sr-only position:absolute; clip… ✗ No ✗ No (visual) ✓ Accessible
not-sr-only Reverses sr-only ✓ Yes ✓ Yes ✓ Accessible
hidden — usage and gotchas
<!-- Basic hide -->
<div class="hidden">This element takes no space</div>

<!-- Toggle visibility with JS -->
<div id="menu" class="hidden">Dropdown menu content</div>
<script>
  // Toggle between hidden and block
  document.getElementById('menuBtn').addEventListener('click', () => {
    document.getElementById('menu').classList.toggle('hidden');
  });
</script>

<!-- Accessible text for screen readers only -->
<button>
  <iconify-icon icon="solar:close-linear" width="16"></iconify-icon>
  <span class="sr-only">Close dialog</span>   <!-- visible to screen readers -->
</button>

<!-- Invisible but keeps layout space -->
<div class="invisible group-hover:visible">Tooltip</div>

Responsive Show & Hide Patterns

Combining hidden with responsive prefixes is how you show or hide elements at specific breakpoints. Tailwind is mobile-first, so the pattern involves hiding on mobile and showing on larger screens, or vice versa.

hidden md:block
Hidden on mobile, visible on md+ (≥768px)
block md:hidden
Visible on mobile, hidden on md+ (≥768px)
hidden lg:flex
Hidden until lg breakpoint, then flex
flex lg:hidden
Flex on mobile/tablet, hidden on lg+
hidden sm:block lg:hidden
Visible only on sm–md range (tablet only)
hidden xl:grid
Grid layout only on xl+ screens
Responsive show/hide — real-world examples
<!-- Desktop navbar — hidden on mobile, flex on md+ -->
<nav class="hidden md:flex items-center gap-6">
  <a href="...">Home</a>
  <a href="...">About</a>
</nav>

<!-- Hamburger button — visible on mobile, hidden on md+ -->
<button class="block md:hidden" aria-label="Open menu">
  <iconify-icon icon="solar:list-bold" width="24"></iconify-icon>
</button>

<!-- Sidebar — hidden on mobile, block on lg+ -->
<aside class="hidden lg:block w-64 flex-none">
  Sidebar content
</aside>

<!-- Mobile-only card — visible on mobile only -->
<div class="block sm:hidden bg-cyan-50 rounded-xl p-4">
  Mobile-only notice
</div>

<!-- Tablet only — sm to lg range -->
<div class="hidden sm:block lg:hidden">
  Tablet-only element
</div>

<!-- Dark mode display control -->
<div class="block dark:hidden">Light mode icon</div>
<div class="hidden dark:block">Dark mode icon</div>
Mobile-first reminder Tailwind is mobile-first — unprefixed classes apply from 0px up. The pattern hidden md:block means "hidden on mobile, block from 768px up." The reverse block md:hidden means "block on mobile, hidden from 768px up."

contents & flow-root

Two less-commonly used but powerful display values that solve specific layout problems.

contents
Sets display: contents. The element's box is removed from layout but its children remain and participate in the parent's layout context. Useful when you need a semantic wrapper (e.g. a <label> or <li>) to not create a new layout context.

Use case: you have a div inside a flex or grid container that shouldn't be its own flex/grid item — its children should be the direct items instead.
flow-root
Sets display: flow-root. Creates a new block formatting context (BFC) — this contains any floated children and prevents margin collapsing with adjacent elements.

Use case: wrapping floated images in article content where the container collapses. flow-root is the modern alternative to the old clearfix hack.
contents and flow-root — practical examples
<!-- ── contents ─────────────────────────────────────── -->
<!-- Problem: wrapper div creates an extra flex item -->
<div class="flex gap-4">
  <button>Button 1</button>

  <!-- This wrapper would be a flex item, not the children -->
  <div class="contents">  <!-- box removed — children go directly into flex row -->
    <button>Button 2</button>   <!-- now a direct flex item -->
    <button>Button 3</button>   <!-- now a direct flex item -->
  </div>

  <button>Button 4</button>
</div>
<!-- Result: all 4 buttons are direct flex children with gap-4 -->

<!-- ── flow-root ─────────────────────────────────────── -->
<!-- Contains floated children — modern clearfix -->
<div class="flow-root">
  <img class="float-left mr-4 mb-2 w-48 rounded-xl" src="..." alt="...">
  <p>Article text wraps around the floated image...</p>
</div>
<!-- The parent div now contains the float — no collapse -->

Table Display Utilities

Tailwind exposes all CSS table display values as utility classes. These are rarely used for layout (use Grid or Flexbox instead) but are valuable when building semantic HTML tables or when working with elements that need CSS table behaviour for specific alignment effects.

Class CSS value Use
table display: table Like a <table> element — full-width block
table-caption display: table-caption Like a <caption> element
table-cell display: table-cell Like a <td> — enables vertical-align on non-table elements
table-column display: table-column Like a <col> element
table-column-group display: table-column-group Like a <colgroup> element
table-footer-group display: table-footer-group Like a <tfoot> element
table-header-group display: table-header-group Like a <thead> element
table-row-group display: table-row-group Like a <tbody> element
table-row display: table-row Like a <tr> element
table-cell — vertical centering without flex
<!-- table + table-cell: old-school vertical centering trick -->
<!-- (Use flex/grid for new projects — this is for legacy compat) -->
<div class="table w-full h-screen">
  <div class="table-cell align-middle text-center">
    Vertically and horizontally centred
  </div>
</div>

<!-- Modern equivalent — use flex instead: -->
<div class="flex items-center justify-center h-screen">
  Vertically and horizontally centred
</div>

<!-- Responsive table with overflow scroll -->
<div class="overflow-x-auto">
  <table class="table w-full text-sm">
    <thead class="table-header-group bg-gray-50 dark:bg-gray-800">
      <tr class="table-row">
        <th class="table-cell px-4 py-3 text-left font-semibold">Name</th>
        <th class="table-cell px-4 py-3 text-left font-semibold">Status</th>
      </tr>
    </thead>
  </table>
</div>

list-item & subgrid

Two additional display values that Tailwind exposes — list-item for non-list elements that should behave like list items (with bullets/numbers), and subgrid for the modern CSS subgrid specification.

list-item and subgrid
<!-- list-item — makes non-list elements show bullet/marker -->
<div class="list-item list-disc ml-5">This div has a bullet marker</div>
<div class="list-item list-decimal ml-5">This div has a number</div>

<!-- Typical use — custom styled list without ul/li -->
<div class="space-y-2 ml-6">
  <div class="list-item list-disc text-gray-700 dark:text-gray-300">First item</div>
  <div class="list-item list-disc text-gray-700 dark:text-gray-300">Second item</div>
</div>

<!-- subgrid — v3.4+ · child participates in parent's grid tracks -->
<div class="grid grid-cols-3 gap-4">
  <!-- Card that spans 2 columns and uses subgrid internally -->
  <div class="col-span-2 grid subgrid gap-4">
    <div>Aligned to parent grid track 1</div>
    <div>Aligned to parent grid track 2</div>
  </div>
</div>
Featured Course
Master Tailwind CSS — From Zero to Production (React Course)
Go beyond display utilities — build complete production UIs. Our structured course covers the full flexbox and grid system, responsive layouts, dark mode, animations, and five real-world projects including a SaaS landing page and admin dashboard.
45 Chapters 10+ Hours 5 Real Projects Certificate Live Mentoring
🖥️
⭐ 4.9 · 1,200+ students
Tailwind Display Utilities Cheat Sheet
Download our free cheat sheet — every Tailwind display class with its CSS value, the block/inline/inline-block comparison, responsive show/hide patterns, hidden vs invisible vs sr-only, and contents/flow-root use cases.
block inline flex grid hidden contents Responsive
Download Free Cheat Sheet
Chapter Summary
  • block — full-width, stacks vertically, respects all box model properties. Use to turn inline elements (span, a, img) into block-level elements.
  • inline — flows with text, no width/height control, vertical margins ignored. Use sparingly — inline-block or inline-flex are more useful.
  • inline-block — flows inline but respects full box model. Perfect for badges, pill buttons, and small interactive elements that live inside text.
  • flex vs inline-flex — both create flex containers. flex is block-level (full width). inline-flex shrinks to content and sits inline — ideal for badges and icon+text combos.
  • grid vs inline-grid — same pattern as flex/inline-flex. Always add grid-cols-* to define columns, otherwise grid behaves like a single-column block container.
  • hidden = display: none — removes element from layout and accessibility tree. Use sr-only to hide visually but keep accessible, invisible to hide but preserve layout space.
  • Responsive show/hide — combine hidden with responsive prefixes: hidden md:flex (desktop-only), flex md:hidden (mobile-only).
  • contents — removes the wrapper's box from layout while keeping its children as participants in the parent's flex/grid. flow-root — creates a block formatting context to contain floats (modern clearfix).

Quick Check — Questions & Answers

Click each question to reveal the detailed answer.

All three make an element invisible, but they behave very differently:

hidden (display: none) — the element is completely removed from the document flow. It takes up no space, cannot be interacted with, and is not accessible to screen readers.

invisible (visibility: hidden) — the element is invisible but still occupies its layout space. Other elements don't shift to fill the gap. Not accessible to screen readers.

opacity-0 (opacity: 0) — the element is transparent but fully present in the layout and accessible to screen readers and pointer events. Adding pointer-events-none prevents interaction.

Choose based on need: toggling visibility → hidden. Preserving layout space → invisible. Fade animation → opacity-0 transition-opacity.
Use inline-flex when the flex container should sit inside text or other inline content rather than taking the full width of its parent.

Common use cases for inline-flex:
• Badge/pill components: inline-flex items-center gap-1.5 px-3 py-1
• Icon + text buttons that don't need to be full-width
• Menu items in a horizontal navigation that sits inside a flex row
• Status indicators next to text

Use flex when: the container should be block-level and stretch the full available width — navbars, card bodies, section layouts.
Use the pattern hidden lg:block (or hidden lg:flex if it needs to be a flex container):

<nav class="hidden lg:flex items-center gap-6">

Breakdown:
• hidden — starts hidden on all screens (mobile-first default)
• lg:flex — becomes a flex container at ≥1024px

For a desktop-only sidebar: hidden lg:block w-64
For a mobile-only hamburger: block lg:hidden

Remember: Tailwind is mobile-first — unprefixed hidden applies from 0px, then responsive prefixes override it at their breakpoint.
contents removes the element's own box from the layout while leaving its children to participate directly in the parent's layout — as if the wrapper element didn't exist.

Use case: you have a flex or grid container and you want to wrap some children in a semantic element (for CSS selectors, JavaScript, or accessibility grouping) but that wrapper should NOT create a new flex/grid item. With contents, the wrapper's children become direct flex/grid items of the grandparent.

Important caveats: elements with display: contents lose their own visual properties (background, border, padding). They also have some accessibility issues in certain browsers — screen readers may still see the element or ignore it inconsistently. Use with care and test with a screen reader.
Images are inline by default, which means they sit on the text baseline. This creates a small gap below the image (typically 3–5px depending on the font's baseline) because the browser reserves space for descenders (letters like g, y, p).

Adding block to an image (<img class="block">) removes it from inline flow, eliminating the gap. This is why you'll see block w-full h-auto on most images in Tailwind layouts.

Tailwind's Preflight reset already sets all images to display: block automatically — so if you're using Preflight (the default), you don't need to add block to images manually. If you've disabled Preflight, you need to add it yourself.

MCQ Quiz — Test Yourself

Select the best answer. The correct option is revealed after you click.

Question 1 of 4
You want a badge inside a paragraph that shows an icon next to text, both vertically aligned. The badge should only be as wide as its content. Which class should you use?
Question 2 of 4
An element with hidden vs one with invisible — what is the key difference?
Question 3 of 4
Which class combination shows an element only on mobile screens (below md breakpoint)?
Question 4 of 4
You add grid grid-cols-3 to a parent. Inside you have a wrapper div that groups two children. You want those two children to each be a grid item, not the wrapper. Which class goes on the wrapper div?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel