10 Ways to Hide Elements in CSS
Hi! In this guide, I’ll show you in detail how to hide elements with CSS. We’ll look at different methods, what sets them apart, and when to use each one.
Sometimes you need to hide an element depending on the situation—for example, show an ad banner on desktop but hide it on mobile. There are several ways to do this, and I’ll walk you through each one.
For this example, we’ll use three identical blocks and hide the one in the middle.
<div class="wrapper">
<div class="container">
<div class="left-block">1</div>
<div class="center-block">2</div>
<div class="right-block">3</div>
</div>
</div>
.wrapper {
display: flex;
align-items: center;
justify-content: center;
max-width: 1300px;
margin: 0 auto;
padding-left: 15px;
padding-right: 15px;
box-sizing: content-box;
}
.container {
display: flex;
justify-content: center;
flex-wrap: wrap;
max-width: 700px;
outline: 2px solid black;
}
[class*="-block"] {
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
margin: 15px;
font-size: 50px;
font-weight: bold;
text-align: center;
color: #ffffff;
background: red;
}
opacity and filter: opacity()
One of the simplest ways to hide an element is to adjust its opacity. The opacity property makes an element fully transparent when you set its value to 0:
.center-block {
opacity: 0;
}
The result:
You can also use the filter property with the opacity() function:
.center-block {
filter: opacity(0);
}
The result is similar.
An element hidden this way remains on the page: it stays in the DOM tree and continues to take up space among the other elements.
In modern browsers, there’s little practical difference between these two methods. However, use
filterwhen you’re applying several effects at once, such as blur, contrast, or grayscale.
visibility
The CSS visibility property controls an element’s visibility. Set it to hidden, and the element will become visually hidden while still taking up space among the other HTML elements on the page.
.center-block {
visibility: hidden;
}
The space occupied by the element remains reserved unless you use the value
collapse.
To hide an element completely, you’ll need additional CSS properties or ARIA attributes, such as
aria-hidden="true".
display
This is probably one of the properties web developers use most often. Set it to none, and the element is removed from the document flow, as if it never existed on the current HTML page.
.center-block {
display: none;
}
Although
display: noneis a popular method, it has some limitations. It can’t be animated, and it triggers a layout change that affects the position of other elements on the page.
displayalso has many possible values, such asblock,inline,table,flexbox,gridand so on. Restoring the correct value afterdisplay: none;can be tricky (althoughunsetmay help).
color and background-color
Another way to hide an HTML element is to set a color’s alpha channel to zero using rgba(0,0,0,0). This makes the element fully transparent.
.center-block {
color: rgba(0,0,0,0);
background-color: rgba(0,0,0,0);
}
This approach won’t make an element with a background image transparent unless the image is created with a linear gradient or a similar technique.
transform
Another option is to use the CSS transform.
.center-block {
transform: scale(0);
}
or
.center-block {
transform: translate(-9999px, 0);
}
transformoffers high performance and hardware acceleration because the element is efficiently moved to its own layer and can be animated in 2D or 3D. It still remains in the DOM tree.
clip-path
This property creates a clipping region that determines which parts of an element are visible. Setting it to circle(0) or polygon(0px 0px,0px 0px,0px 0px,0px 0px) will hide the element completely.
/* OPTION 1 */
.center-block {
clip-path: circle(0);
}
/* OPTION 2 */
.center-block {
clip-path: polygon(0px 0px,0px 0px,0px 0px,0px 0px);
}
clip-pathoffers plenty of possibilities for all kinds of animations, but it should only be relied on in modern browsers.
The HTML hidden attribute
You can add this attribute to any element:
<!-- OPTION 1 -->
<div class="center-block" hidden>2</div>
<!-- OPTION 2 -->
<div class="center-block" hidden="">2</div>
<!-- OPTION 3 -->
<div class="center-block" hidden="hidden">2</div>
The attribute works only if no explicit
displayproperty is set on the element. For example, I’ve setdisplay: flexon the element with thecenter-blockclass. In that case, thehiddenattribute won’t have any effect on the element.
This method has the same advantages and disadvantages as
display: none, though it can be useful with a content management system (CMS) that doesn’t let you change CSS styles.
position
The position property lets you move an element from its default static position in the page’s DOM tree using the top, bottom, left, and right properties. This lets you move an absolutely positioned element off-screen to the left, right, up, or down.
.center-block {
position: absolute;
left: -9999px;
right: -9999px;
}
Overlaying another element
You can also hide an element visually by placing another element over it in the same color as the background. This example uses the ::after pseudo-element, but you can use any child element.
For this example, add a few lines of CSS:
.center-block {
position: relative;
}
.center-block::after {
position: absolute;
content: "";
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
background-color: #ffffff;
}
Reducing the element’s size
You can also hide an element by reducing its dimensions.
.center-block {
height: 0px;
padding: 0px;
overflow: hidden;
}
The hidden element still remains among the other DOM elements on the page, even though its height is
0.
