A login form has a spot for its error message. Hide that message the wrong way, and the button below it jumps up and down every time the message appears and disappears. Tailwind has two ways to hide an element: one way makes the button jump, the other doesn't.
Display decides how an element takes up space: a whole line to itself, a spot in the line next to its neighbours, or no spot at all. Visibility decides whether the element can be seen. In Tailwind, each value is one class: block, inline-block, inline, hidden, invisible. Think of a class photo, where each kid in the row is one element. If one kid steps out of the row, the others move closer and there's no gap: that's hidden. If the kid stays in the row but wears an invisibility cloak, nobody sees them, but the gap is still in the photo: that's invisible.
Three ways to take up space
These are the same three boxes as in the Lab. All three are <div>s, and each box gets a width of w-20 (5rem = 80px) and a height of h-12 (3rem = 48px):
<div id="visible" class="inline-block w-20 h-12 bg-amber-200">Visible</div>
<div id="ghost" class="inline-block w-20 h-12 bg-sky-200">Ghost</div>
<div class="inline-block w-20 h-12 bg-emerald-200">Friend</div>
The display class on the Visible box can change, and the result changes with it:
| Class | CSS it generates | Starts a new line? | Follows w-20 and h-12? |
|---|---|---|---|
block |
display: block; |
yes, Ghost and Friend move below it | yes |
inline-block |
display: inline-block; |
no, in line with Ghost and Friend | yes |
inline |
display: inline; |
no, in line | no, only as wide as its text |
- A
<div>isblockby default, which is why every plain div starts on a new line. A<span>isinlineby default. inline-blockis the mix people often want: it stays in line like text, but its width and height can be set. In the exercise,#visiblegetsinline-block.inlineignoresw-*andh-*. If your box suddenly shrinks to the width of its text, check whether the box isinline.
hidden or invisible
Both make the Ghost box disappear, but in different ways:
| Ghost uses | CSS it generates | What the row shows |
|---|---|---|
| (nothing) | Visible, Ghost, Friend | |
hidden |
display: none; |
Visible, Friend (Friend moves closer) |
invisible |
visibility: hidden; |
Visible, an 80px gap, Friend |
hiddentakes the element out of the layout. The element can't be seen, it takes no space, and its neighbours move closer.invisibleonly makes the element unseen. Its space is kept, so the Friend box doesn't move.
In the exercise, the Ghost box has to be invisible:
<div id="visible" class="inline-block">Visible</div>
<div id="ghost" class="invisible">Ghost</div>
invisible helps when the layout must not jump, for example the spot for an error message that hasn't appeared yet. hidden is for an element that must take no space, for example a dropdown menu that's closed.
hidden and invisible also hide the element from screen readers (programs that read the page aloud). If the text must still be read aloud, there is a third option:
| Class | On screen | Read aloud? | When to pick it |
|---|---|---|---|
hidden |
gone, its neighbours close the gap | no | a closed menu |
invisible |
gone, the gap stays | no | the spot for an error message that hasn't appeared yet |
sr-only |
an invisible 1px box | yes | the label of a button that only holds an icon |
For example, a close button that only holds the ✕ icon:
<button>
<span aria-hidden="true">✕</span>
<span class="sr-only">Close menu</span>
</button>
The screen shows only ✕. A screen reader reads the sr-only text, so it calls this button "Close menu". It skips the ✕, because aria-hidden="true" hides the ✕ from screen readers.
📌 opacity-0: see-through, but still there
opacity-50 generates opacity: 50% (half see-through), and opacity-0 generates opacity: 0%. With opacity-0 the element can't be seen and its space is kept, much like invisible. The difference: an opacity-0 element can still be clicked and focused. So a button with opacity-0 is an invisible button that still catches clicks. Use opacity-* for fading effects, and invisible or hidden when the element really has to go.
⚠️ Picking the wrong display class
- Mixing up
hiddenandinvisible. The exercise checksvisibility: hiddenon#ghost, and that value comes frominvisible.hiddengeneratesdisplay: none, so it doesn't pass the check. inlinefor a box that needs a size.inlineignores width and height. The exercise checksdisplay: inline-blockon#visible, so writeinline-blockin full.- Two display classes at once.
class="inline-block hidden"gives two rules that both setdisplay. Which one applies is decided by the order of the rules in the CSS Tailwind generates, not the order you write them in. Pick just one. visibleisn't the opposite ofhidden.visiblegeneratesvisibility: visible, so its opposite isinvisible. To show ahiddenelement again, replacehiddenwith another display class, such asblock.
TL;DR: Display classes set how an element takes up space. block starts a new line and follows width and height (w-*, h-*), inline stays in line but ignores width and height, inline-block stays in line and still follows width and height. hidden (display: none) takes the element out of the layout, so its neighbours move closer. invisible (visibility: hidden) only hides it, and its space stays. opacity-0 (fully see-through) keeps its space too, and the element can still be clicked. In the exercise: #visible uses inline-block, #ghost uses invisible.
⚡ In line, with a size
Write:
<div id="visible" class="inline-block w-20 h-12 bg-amber-200">Visible</div>
<div id="ghost" class="inline-block w-20 h-12 bg-sky-200">Ghost</div>
<div class="inline-block w-20 h-12 bg-emerald-200">Friend</div>
Meaning: inline-block keeps the boxes in one line, but their w-20 (80px wide) and h-12 (48px tall) still work. block starts a new line, inline stays in line but ignores width and height.
⚡ hidden or invisible
Write:
<div id="visible" class="inline-block">Visible</div>
<div id="ghost" class="invisible">Ghost</div>
Guess first: hidden generates display: none. If the Ghost box uses hidden, does the Friend box from the last step move?
Yes, it moves closer to the Visible box. With invisible, the 80px gap stays.
Meaning: hidden = display: none: not seen and taking no space. invisible = visibility: hidden: not seen, but its space is kept.
The exercise checks for visibility: hidden on #ghost, so use invisible, not hidden.
⚡ opacity-0 can still be clicked
Meaning: opacity-0 (fully see-through) also makes an element impossible to see and keeps its space, but the element can still be clicked and focused. Use opacity-* for fading, not for removing an element.