Beyond Visibility: Exploring Alternatives for Hiding Elements in CSS


  • Use Cases
    • You can hide elements temporarily for better user experience, like hiding tooltips until hovered over.
    • You can control the visibility of elements based on certain conditions using media queries or JavaScript.
    • You can use collapse to hide borders of empty tables or to hide empty elements without affecting layout.
  • Values
    • visible (default): This makes the element fully visible.
    • hidden: This hides the element, but it still occupies space in the document flow (meaning other elements will act as if it's there).
    • collapse: This hides the element and removes it from the document flow (other elements will render as if it doesn't exist).
.hidden-element {
  visibility: hidden;
}

.tooltip {
  visibility: hidden;
  position: absolute; /* Ensures tooltip appears near its target */
}

.tooltip:hover {
  visibility: visible;
}

In this example, the .hidden-element class hides the element, while the .tooltip class hides the tooltip by default and shows it only when hovered over.

  • visibility doesn't affect whether the element is printed. You can use the display property for that.


Hiding an element

<p id="secret-message">This is a secret message!</p>
#secret-message {
  visibility: hidden;
}

This code hides the paragraph with the ID "secret-message". It's still there in the code, but not visible on the webpage.

Toggle visibility with hover

<button>Show Image</button>
<img id="hidden-image" src="image.jpg" alt="Hidden Image" style="visibility: hidden;">
button:hover + #hidden-image {
  visibility: visible;
}

This code hides an image initially. When you hover over the button, the + selector targets the next sibling element (the image) and changes its visibility to visible.

Using visibility: collapse for tables

<table>
  <tr class="empty-row">
    <td></td>
  </tr>
  <tr>
    <td>Data in this row</td>
  </tr>
</table>
.empty-row {
  visibility: collapse;
}

This code hides the empty table row with the class "empty-row" and removes it from the layout. This ensures the table doesn't have unnecessary empty space.

<div class="mobile-menu">Mobile Menu Options</div>
.mobile-menu {
  visibility: hidden;
}

@media screen and (max-width: 768px) {
  .mobile-menu {
    visibility: visible;
  }
}


display: none

  • Downside
    Can cause layout issues if the hidden element affects the positioning of other elements.
  • Use Case
    This is the most common alternative for completely hiding elements you don't want displayed on the page at all.
  • Effect
    Completely hides the element and removes it from the document flow (similar to visibility: collapse).

opacity: 0

  • Downside
    The element remains clickable and can still interfere with user interactions.
  • Use Case
    Useful for creating fade-in/fade-out animations or partially transparent elements.
  • Effect
    Makes the element fully transparent, essentially hiding it visually but keeping it in the document flow.

position: absolute; with negative values

  • Downsides
    Requires additional calculations to position the element off-screen and might be less performant than other options.
  • Use Case
    Can be useful for hiding elements that need to be accessed later with JavaScript or for complex layouts.
  • Effect
    Positions the element off-screen, effectively hiding it.

clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%);

  • Downsides
    Can be complex to set up and might not be supported in older browsers.
  • Use Case
    Similar to display: none but offers more flexibility for creating custom shapes that hide parts of the element.
  • Effect
    Creates a clipping path that hides the entire element.
  • Downsides
    Less flexible than CSS properties and offers limited control.
  • Use Case
    Primarily for forms, where hidden elements can still submit data.
  • Effect
    Works similarly to visibility: hidden, hides the element but keeps it in the document flow.
AlternativeEffectUse CaseDownsides
display: noneCompletely hides and removes from document flowCompletely hiding elementsLayout issues if element affects positioning
opacity: 0Makes element transparent (visually hidden)Fade-in/out animations, partially transparent elementsElement remains clickable
position: absolute; negative valuesPositions off-screenHiding elements for later access with JavaScriptRequires calculations, less performant
clip-pathCreates clipping path to hide elementCustom hiding with shapesComplex setup, older browser compatibility
hidden attr.Similar to visibility: hiddenForms where hidden elements submit dataLess flexible, limited control