DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

6 ways to hide an element in CSS

Here are six different ways to hide an element using CSS:

display: none;

.hidden-element {
  display: none;
}
Enter fullscreen mode Exit fullscreen mode

This completely removes the element from the document flow. It won't occupy any space on the webpage.

visibility: hidden;

.hidden-element {
  visibility: hidden;
}
Enter fullscreen mode Exit fullscreen mode

This hides the element, but it still occupies space on the webpage. The element is not visible, but the space it takes up remains.

opacity: 0;

.hidden-element {
  opacity: 0;
}
Enter fullscreen mode Exit fullscreen mode

This hides the element by making it fully transparent. It still occupies space and retains its dimensions but is visually hidden.

height: 0; width: 0; overflow: hidden;

.hidden-element {
  height: 0;
  width: 0;
  overflow: hidden;
}
Enter fullscreen mode Exit fullscreen mode

This hides the element by setting both its height and width to zero. Using overflow: hidden prevents any content from being visible.

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

.hidden-element {
  clip-path: polygon(0 0, 0 0, 0 0, 0 0);
}
Enter fullscreen mode Exit fullscreen mode

This method uses the clip-path property to hide the element by defining a polygon shape with zero area.

clip-path: circle(0);

.hidden-element {
  clip-path: circle(0);
}
Enter fullscreen mode Exit fullscreen mode

This will hide the element by clipping it to a circle with no visible area, essentially making the element invisible.

Top comments (3)

Collapse
 
ynn1k profile image
Yannik Hewlik

This is just a 1:1 answer from ChatGPT, where it somehow managed to list the clip-path solution twice...
I got the position: absolute; left:-9999px as the 5th solution.

Collapse
 
olivia578 profile image
Olivia Anderson

Great tips on hiding elements in CSS! The practical examples make it easy for both beginners and seasoned coders to choose the right method for their specific needs.

Collapse
 
hoj20 profile image
J.Has12

Thank'u for sharing