Show or Hide: Mastering CSS Visibility and Display

Master the art of hiding and showing elements. Learn the critical difference between visibility: hidden, display: none, and opacity: 0.

Lesson ProgressStep 1 of 8
0 EXP

Hello! Let's explore how to hide elements. We have three boxes. We'll hide the middle one.

The Vanishing Act: `display: none`

This is the most absolute way to hide an element. It tells the browser to act as if the element doesn't exist.

  • The element is removed from the document flow.
  • It takes up no space.
  • Other elements reflow to fill the void.
  • It's hidden from screen readers and cannot be interacted with.
.element-to-vanish {
  display: none;
}

System Check

What happens to an element's space when you use `display: none`?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🪄
The Vanisher

Correctly use `display: none` to remove an element.

👻
Ghost Master

Correctly use `visibility: hidden` to hide an element.

🛡️
Layout Guardian

Explain the layout difference between `display` and `visibility`.

Mission: The Vanisher and The Ghost

You have two elements: `.box-1` and `.box-2`. Write the CSS to make `.box-1` disappear completely (reclaiming its space) and make `.box-2` invisible (but *keep* its space).

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Match the Effect

Drag the descriptions from the left to the correct CSS property on the right.

Preserves layout space
Removes element from layout
Still interactive
`display: none`
`visibility: hidden`
`opacity: 0`

Challenge: Complete the Syntax

Fill in the missing properties and values to hide the elements as described.

.remove-me {
: none;
}
.hide-me {
: ;
}

Consult A.D.A.

Community Holo-Net

The Vanishing Act: `display`, `visibility`, and `opacity`

In CSS, making an element "disappear" is more complex than it sounds. You have three primary tools, and each behaves in a fundamentally different way. Choosing the wrong one can lead to broken layouts, poor accessibility, and frustrating bugs. Let's explore the critical differences between display: none, visibility: hidden, and opacity: 0.

1. `display: none` — The Vanishing Act

This is the most absolute way to hide an element. When you apply display: none;, you are telling the browser to act as if the element never existed in the HTML.

  • Layout Impact: The element is completely removed from the document flow. It takes up zero space, and any elements around it will collapse or "reflow" to fill the void. This is a major layout change and can be performant-intensive.
  • Accessibility Impact: The element and all its children are removed from the accessibility tree. A screen reader will not detect it or announce it. It is completely inaccessible.
  • Interactivity: The element cannot be interacted with (e.t., clicked or hovered) because, as far as the browser is concerned, it doesn't exist.

Best for: Collapsible accordions, mobile navigation menus, and tabs. Use it when you want the layout to change and reclaim the space.

2. `visibility: hidden` — The Invisibility Cloak

This property makes the element invisible, but it's still "there." Think of it as painting the element with invisible paint.

  • Layout Impact: The element still occupies its full space in the layout. It leaves an "empty" box where it used to be, and other elements will not move to fill it. This avoids a layout reflow.
  • Accessibility Impact: The element is also hidden from screen readers. While it's still technically in the document, it's not exposed to assistive technology.
  • Interactivity: The element is not interactive. You cannot click, hover, or focus on it.
  • A Peculiar Quirk: Child elements can override this! If you set a child to visibility: visible;, the child will reappear, seemingly floating in the empty space of its invisible parent.

Best for: Hiding content while preserving the layout, or for certain animations where you want an element to "pop" in without moving anything else.

3. `opacity: 0` — The See-Through Ghost

This is the third, and most subtle, option. It *only* affects the element's transparency.

  • Layout Impact: The element still occupies its full space, just like visibility: hidden.
  • Accessibility Impact: The element is still fully accessible to screen readers. It will be read out as if it were visible.
  • Interactivity: The element is still fully interactive. A user can click, hover, and focus on the invisible element. This can be a huge "gotcha" if you're not expecting it!

Best for: Fading animations (animating opacity from 0 to 1). You almost always want to pair it with pointer-events: none; to prevent users from clicking the invisible element.

At-a-Glance Comparison

PropertyImpact on LayoutAccessible (Screen Reader)Interactive (Click/Hover)
display: none;Removed (Space is reclaimed)NoNo
visibility: hidden;Kept (Space is preserved)NoNo
opacity: 0;Kept (Space is preserved)YesYes
Key Takeaway: Use display: none to remove an element and change the layout. Use visibility: hidden to hide an element but preserve the layout. Use opacity: 0 for animations, but be sure to manage its interactivity.

CSS Visibility & Layout Glossary

display: none
A CSS property value that completely removes an element from the document flow. The element takes up no space, and the layout "reflows" as if the element never existed. It is also hidden from screen readers.
visibility: hidden
A CSS property value that makes an element invisible but preserves its space in the layout. The element still occupies its full dimensions, leaving a "ghost" or blank space. It is hidden from screen readers and is not interactive.
opacity: 0
Makes an element 100% transparent (invisible). Like visibility: hidden, it preserves its space in the layout. However, the element remains interactive (clickable) and is still accessible to screen readers, which can be confusing.
Document Flow
The default layout of elements on a webpage. display: none removes an element from this flow, while visibility: hidden and opacity: 0 do not.
Reflow
The browser process of recalculating the positions and geometries of elements on the page. This is triggered by display: none and can be computationally expensive as it may affect the entire layout.
Accessibility Tree (A11y)
A data structure used by assistive technologies (like screen readers) to understand the page. Elements with display: none or visibility: hidden are removed from this tree. Elements with opacity: 0 are not.
Interactivity
Refers to whether an element can be interacted with (e.g., clicked, hovered, focused). An element with opacity: 0 is still interactive, while the others are not.
pointer-events: none
A CSS property often paired with opacity: 0 to make the transparent element non-interactive, preventing accidental clicks on the invisible "ghost" element.
Visually Hidden
A CSS technique used to hide an element visually from the screen but keep it fully accessible to screen readers. This is the best Practice for "skip links" or labels for screen reader users only.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!