Table of Contents

About

To hide an element in CSS, you may use:

The difference is that:

  • with the display:none option, you will not see any blank space
  • while with visibility:hidden, you will (if the element is positioned - ie not absolute)

Toggle

They are used has the basic elements to implement toggling

Example:

  • The css of the element to toggle
.hidden {
  /* Not displayed at first */
  display: none;
}
  • The trigger element that will toggle the visibility of the target element
<button>Toggle</button>
  • The target element
<p class="toggle hidden">I'm not visible until you click on the button</p>
document.querySelector("button").addEventListener("click",function(){
   document.querySelector(".toggle").classList.toggle('hidden');
});
  • Result: click on the button to toggle the visibility