Tailwind Show and Hide
Responsive pages often show different content on different screens. A phone displays a compact menu button while a desktop displays the full menu. Tailwind offers several ways to hide an element, and each way behaves differently. Choosing the right one avoids layout bugs and keeps pages accessible.
Four Ways to Hide Content
Picture four methods a theater could use to hide an actor. A curtain removes the actor and the space they occupy. Invisible ink keeps the space but hides the actor. A faint spotlight dims the actor to nothing. A whisper through headphones reaches only listeners who cannot see the stage. These four cases map to four Tailwind classes.
Class Takes space Visible Screen reader reads it
──────────────────────────────────────────────────────────────
hidden no no no
invisible yes no no
opacity-0 yes no yes
sr-only no no yes
The Hidden Class
The class hidden sets display: none. The element disappears and its space collapses. Other elements move up to fill the gap.
Before hidden After hidden on the middle box
[ A ] [ A ]
[ B ] [ C ]
[ C ]
The Invisible Class
The class invisible hides the element and keeps its space. The gap stays in the layout.
Before invisible After invisible on the middle box
[ A ] [ A ]
[ B ] [ ] ← empty space remains
[ C ] [ C ]
Screen Reader Only
The class sr-only hides content visually while leaving it available to assistive technology. Use it for labels on icon buttons.
<button>
<svg aria-hidden="true">...</svg>
<span class="sr-only">Open menu</span>
</button>
The class not-sr-only reverses the effect when a larger screen has room to show the text.
Showing at a Breakpoint
Tailwind builds pages mobile first. A class without a prefix applies to every screen. A prefixed class applies at that width and above. Combine hidden with a display class to show an element only on larger screens.
hidden md:block → hidden on phones, visible from 768px up
md:hidden → visible on phones, hidden from 768px up
Width: 0 ────────── 768px ────────────►
hidden md:block [hidden] [visible ──────►]
md:hidden [visible] [hidden ───────►]
Hiding Within a Range
The max- prefix targets screens smaller than a breakpoint. Stack a minimum and a maximum to target one band.
max-md:hidden → hidden below 768px
md:max-lg:hidden → hidden only between 768px and 1024px
Menu Example
<nav class="flex items-center justify-between p-4">
<a href="/" class="font-bold">Brand</a>
<ul class="hidden gap-6 md:flex">
<li>Home</li><li>Courses</li><li>Contact</li>
</ul>
<button class="md:hidden">Menu</button>
</nav>
Phone: Brand ............ [Menu]
Desktop: Brand .... Home Courses Contact
The list uses md:flex instead of md:block because the list needs flex layout once it appears. A hidden class sets display to none, and the prefixed class must restore the display type you want.
Print and Other Conditions
Other variants control visibility under special conditions.
print:hidden → hidden when the page is printed
hidden dark:block → shown only in dark mode
group-hover:block → shown when a parent is hovered
Planning What Each Screen Needs
Hiding content is a design decision, not just a technical one. Start by listing what a visitor must do on each device. A phone visitor usually wants to read, tap, and leave. A desktop visitor may compare options side by side. Show the essential items everywhere and reserve extras, such as decorative images or long descriptions, for larger screens.
Avoid duplicating whole sections in the markup, one for phones and one for desktops. Duplicates double the page weight and confuse screen readers, which may read both versions. Prefer one set of content with layout classes that rearrange it. Use the show and hide pair only for controls that truly differ, such as a menu button against a full menu bar.
Testing Visibility
Resize the browser from narrow to wide and watch each element appear or vanish at its breakpoint. Press the Tab key through the page at every width. Focus should never land on an element you cannot see. If it does, switch from opacity-0 to hidden or invisible.
Common Mistakes
- Using
opacity-0to hide a link leaves it clickable and focusable. Visitors may activate something they cannot see. - Hiding important content on phones removes information for most visitors. Rearrange or shorten content instead.
- Forgetting the display class after
hiddenleaves the element invisible at every width.
Visibility classes shape the page for each screen. Choose the method that matches the goal: a collapsed gap, a preserved gap, or a screen-reader-only label.
