How to Hide an Element in CSS
Use CSS hiding techniques when content should stay in the HTML but not remain visible in the layout. The best method depends on whether the element should disappear completely, keep its layout space, or only hide on certain screen sizes.
What you’ll build or solve
You’ll learn how to hide an element in CSS using the most common techniques. You’ll also know when to use display: none, visibility: hidden, and responsive hiding rules.
Learn CSS on Mimo
When this approach works best
This approach is the right choice when part of the UI should be temporarily hidden without deleting the HTML.
Common real-world scenarios include:
- Mobile-only navigation changes
- Hiding banners after dismissal
- Removing helper text on smaller screens
- Feature-flagged UI blocks
- Temporarily hiding test components
This is a bad idea when the content should remain accessible to screen readers or still be interactive in the page flow.
Prerequisites
You only need:
- A basic HTML file
- A text editor
- Basic HTML and CSS knowledge
Step-by-step instructions
Step 1: Choose the right hiding method for the behavior you need
Use display: none when the element should disappear completely.
HTML
<div class="promo-banner">USA pricing launch</div>
<style>
.promo-banner {
display: none;
}
</style>
Use visibility: hidden when the element should stay in the layout but not remain visible.
HTML
<div class="helper-text">France rollout details</div>
<style>
.helper-text {
visibility: hidden;
}
</style>
For responsive hiding, combine it with a media query.
HTML
<div class="desktop-only">UK dashboard panel</div>
<style>
@media (max-width: 768px) {
.desktop-only {
display: none;
}
}
</style>
What to look for:
display: noneremoves the element from layout flowvisibility: hiddenkeeps the layout space- Media queries work best for screen-specific hiding
- Use the method that matches the layout behavior you want
- Avoid hiding important content users still need to access
Examples you can copy
Hide a promo banner
HTML
<div class="banner">USA spring launch</div>
<style>
.banner {
display: none;
}
</style>
Hide helper text but keep spacing
HTML
<p class="note">France beta details</p>
<style>
.note {
visibility: hidden;
}
</style>
Hide desktop block on mobile
HTML
<div class="desktop-panel">UK analytics</div>
<style>
@media (max-width: 768px) {
.desktop-panel {
display: none;
}
}
</style>
Common mistakes and how to fix them
Mistake 1: Using visibility: hidden when layout space should collapse
What the reader might do:
HTML
.banner {
visibility: hidden;
}
Why it breaks: the empty space still stays in the layout.
Corrected approach:
HTML
.banner {
display: none;
}
Mistake 2: Using display: none for screen-reader-only content
What the reader might do:
HTML
.helper-text {
display: none;
}
Why it breaks: the content disappears completely for both visual users and assistive tools.
Corrected approach:
Use an accessibility-focused visually hidden utility class instead of removing it entirely.
Mistake 3: Forgetting breakpoint logic in responsive hiding
What the reader might do:
HTML
@media (min-width: 768px) {
.mobile-only {
display: none;
}
}
Why it breaks: the logic may hide the wrong version on the wrong screen.
Corrected approach:
Double-check whether the rule should target smaller or larger screens.
Troubleshooting
If empty space still remains, switch from visibility: hidden to display: none.
If the wrong version hides on mobile, recheck the media query breakpoint direction.
If the hidden element still affects screen readers, use an accessibility-specific utility pattern.
If layouts jump unexpectedly, confirm whether removing the element from flow is actually the right choice.
Quick recap
- Use
display: noneto fully remove an element - Use
visibility: hiddento keep layout space - Use media queries for responsive hiding
- Match the hiding method to layout behavior
- Avoid hiding important accessible content the wrong way
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot