← Back to CSS Course | Chapter 5: Layout & Display | Lesson 2 of 14

CSS Hide Elements

Hiding something on a webpage is not just one switch. Sometimes you want it gone as if it never existed, sometimes you want it invisible but still holding its spot in line, and sometimes you want it faded but still clickable.
Syntax
css
selector {
  display: none;          /* removed from layout */
  visibility: hidden;     /* space is kept */
}

display: none Removes From Flow

Setting display: none takes an element completely out of the page. The browser does not render it, does not reserve any space for it, and screen readers skip it entirely. Everything around the hidden element reflows to fill the gap, exactly as if the element had been deleted from the HTML.

Note:
  • Accepted values:
  • display: none — removes the element from the layout
  • visibility: hidden — hides it but keeps its space
  • opacity: 0 — invisible but still clickable
  • hidden attribute — HTML way to hide an element
  • position: absolute with left: -9999px — off-screen but readable by screen readers

Example: display: none Removes From Flow

css
<style>
.hidden {
  display: none;
}
</style>
<p>Before</p>
<p class="hidden">Completely removed, no space reserved</p>
<p>After (moves up to fill the gap)</p>

visibility: hidden Keeps the Space

visibility: hidden makes an element invisible but leaves its box exactly where it was, so surrounding content does not reflow. This is the key difference from display: none, and it matters whenever you want to hide something without the layout jumping.

Note:
  • Accepted values:
  • hidden — invisible but still takes space
  • visible — shown normally (initial value)
  • collapse — removes table rows or columns without changing layout; acts as hidden elsewhere
  • opacity — 0 to 1, also makes it invisible but stays clickable
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: visibility: hidden Keeps the Space

css
<style>
.hidden {
  visibility: hidden;
}
</style>
<p>Before</p>
<p class="hidden">Invisible but space still reserved</p>
<p>After (does not move up)</p>

opacity: 0 Stays Interactive

opacity: 0 makes an element fully transparent while keeping it in the layout AND keeping it clickable and focusable, unlike the other two methods. This makes it useful for custom-styled form controls where you visually hide the native input but still want it to receive clicks and keyboard focus.

Note:
  • Accepted values:
  • opacity: <number> — 0 (transparent) to 1 (opaque); a percentage also works
  • visibility: hidden — invisible, takes space, not clickable
  • display: none — removed from layout entirely
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: opacity: 0 Stays Interactive

css
<style>
.transparent {
  opacity: 0;
}
</style>
<button class="transparent">Invisible but still clickable and focusable</button>

Screen Reader and SEO Differences

display: none and visibility: hidden are both skipped by screen readers, so their content is invisible to assistive technology. opacity: 0 content is still announced by screen readers even though sighted users cannot see it, which is a common accessibility mistake when developers assume opacity: 0 behaves like the other two.

Example: Screen Reader and SEO Differences

css
<style>
.gone { display: none; }
.invisible { visibility: hidden; }
.transparent { opacity: 0; }
</style>
<p class="gone">Skipped by screen readers</p>
<p class="invisible">Skipped by screen readers</p>
<p class="transparent">Still announced by screen readers</p>

Choosing the Right Method

Use display: none when you want an element gone with no trace and no layout gap, such as toggling a mobile menu closed.

Use visibility: hidden when you need the layout to stay stable, such as a table cell that sometimes has no icon. Use opacity: 0 only when you specifically need the element to remain interactive or animatable.

Example: Choosing the Right Method

css
<style>
.menu-closed { display: none; }
.icon-slot { visibility: hidden; }
.custom-input { opacity: 0; }
</style>
<nav class="menu-closed">Mobile menu, fully gone</nav>
<span class="icon-slot">Icon slot, layout stays stable</span>
<input class="custom-input" type="checkbox">
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using display: none on an element that screen readers need, when it also hides the element from them.
  2. Using visibility: hidden and expecting the space to disappear, when it keeps its space.
  3. Using opacity: 0 and forgetting the element can still be clicked.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.