Sometimes you want to hide parts of a webpage or control if something is visible. This helps keep the page clean or show things only when needed.
Hidden and visibility control in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
hidden visible invisible
hidden completely hides an element and removes it from the page layout.
invisible hides the element but keeps its space on the page.
<div class="hidden">This text is hidden and takes no space.</div><div class="invisible">This text is invisible but space is reserved.</div><div class="visible">This text is visible.</div>This page shows three examples: a hidden paragraph that disappears completely, an invisible paragraph that keeps space but is not seen, and a visible paragraph. There's also a button that toggles a hidden message on and off.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Hidden and Visibility Control</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6 font-sans"> <h1 class="text-2xl mb-4">Hidden and Visibility Control Example</h1> <div class="mb-2 bg-blue-100 p-3"> <p class="hidden">This paragraph is hidden and takes no space.</p> <p class="visible">This paragraph is visible and shows normally.</p> </div> <div class="mb-2 bg-green-100 p-3"> <p class="invisible">This paragraph is invisible but space is reserved.</p> <p>This paragraph is below the invisible one.</p> </div> <button id="toggleBtn" class="mt-4 px-4 py-2 bg-indigo-600 text-white rounded">Toggle Hidden Text</button> <p id="toggleText" class="hidden mt-2 bg-yellow-100 p-2">You toggled me visible!</p> <script> const btn = document.getElementById('toggleBtn'); const text = document.getElementById('toggleText'); btn.addEventListener('click', () => { text.classList.toggle('hidden'); }); </script> </body> </html>
Use hidden when you want to remove an element completely from view and layout.
Use invisible when you want to hide something but keep the space it occupies, so the page layout doesn't jump.
Toggle visibility with JavaScript by adding or removing the hidden class for interactive effects.
hidden hides and removes element space.
invisible hides but keeps space.
Use these classes to control what users see and keep your page tidy.
Practice
hidden do to an element?Solution
Step 1: Understand the
Thehiddenclass effecthiddenclass in Tailwind CSS setsdisplay: none;, which hides the element completely.Step 2: Effect on layout space
Because the element is removed from the layout flow, its space is also removed, so no empty gap remains.Final Answer:
It hides the element and removes its space from the layout. -> Option BQuick Check:
hidden= hides + removes space [OK]
- Thinking hidden keeps space like invisible
- Confusing hidden with opacity or visibility
- Assuming hidden only makes element transparent
Solution
Step 1: Identify the class that hides but keeps space
Theinvisibleclass setsvisibility: hidden;, which hides the element but keeps its space.Step 2: Compare with other options
hiddenremoves element and space,opacity-0makes element transparent but still interactive,sr-onlyhides visually but accessible to screen readers.Final Answer:
invisible -> Option DQuick Check:
invisible= hide but keep space [OK]
- Choosing hidden instead of invisible
- Confusing opacity-0 with invisible
- Thinking sr-only hides visually and removes space
<div class="w-24 h-24 bg-blue-500 hidden">Box</div> <div class="w-24 h-24 bg-red-500 invisible">Box</div>
Solution
Step 1: Analyze the blue box with
The blue box hashiddenhidden, so it is not visible and its space is removed from layout.Step 2: Analyze the red box with
The red box is invisible, so it is hidden but its space remains reserved in the layout, leaving an empty gap.invisibleFinal Answer:
Only the red box space is reserved but not visible; blue box is hidden and removed. -> Option AQuick Check:
hiddenremoves element;invisiblehides but keeps space [OK]
- Thinking invisible shows a transparent box
- Assuming hidden keeps space
- Believing both boxes are visible
Solution
Step 1: Understand accessibility needs
To hide visually but keep accessible, the element must be off-screen but still in the accessibility tree.Step 2: Identify the correct Tailwind class
sr-onlyhides the element visually but keeps it readable by screen readers, unlikehiddenorinvisible.Final Answer:
sr-only -> Option AQuick Check:
sr-only= hide visually, keep screen reader access [OK]
- Using hidden which removes from accessibility tree
- Using invisible which hides visually but not accessible
- Using opacity-0 which hides visually but still interactive
Solution
Step 1: Understand responsive classes
hiddenhides element by default (mobile),md:blockshows element as block on medium screens and larger.Step 2: Check other options
invisible md:hiddenhides but keeps space on mobile and hides on medium, so menu never visible.block md:hiddenshows on mobile but hides on medium, opposite of requirement.hidden md:hiddenhides always.Final Answer:
hidden md:block -> Option CQuick Check:
Hide mobile, show medium+ = hidden md:block [OK]
- Using md:hidden hides on medium screens instead of showing
- Using invisible which keeps space and confuses layout
- Using hidden md:hidden hides always
