CSS Hide and Seek: Choosing Between display: none and visibility: hidden
Learn the key differences between CSS display:none and visibility:hidden, and when to use each for better performance, accessibility, and layout control.
Join the DZone community and get the full member experience.
Join For FreeWhen it comes to hiding elements in CSS, developers often face a fundamental choice: should they use display: none or visibility: hidden? While both properties can make elements disappear from view, they behave very differently under the hood. Understanding these differences is crucial for creating efficient, accessible, and maintainable web applications.
The Core Difference
The primary distinction between display: none and visibility: hidden lies in how they affect the document flow and layout:
display: nonecompletely removes the element from the document flow, as if it never existedvisibility: hiddenmakes the element invisible but preserves its space in the layout
This fundamental difference cascades into several important implications that affect performance, accessibility, user experience, and development workflow.
Space Occupation and Layout Impact
Display: None Behavior
When you apply display: none to an element, it's completely removed from the render tree. This means:
- The element takes up zero space
- Surrounding elements shift to fill the gap
- The layout recalculates as if the element was never there
- Parent containers may resize to accommodate the change
.hidden-element {
display: none;
}
Visibility: Hidden Behavior
In contrast, visibility: hidden maintains the element's presence in the layout:
- The element remains in the document flow
- Its original space is preserved
- Surrounding elements stay in their positions
- The element becomes transparent but structurally intact
.invisible-element {
visibility: hidden;
}
Performance Implications
The performance characteristics of these two properties differ significantly, particularly when dealing with frequent visibility changes or animations.
Reflow vs Repaint
Display: None Performance:
- Triggers a reflow (layout recalculation)
- Browser must recalculate positions of surrounding elements
- More computationally expensive
- Can cause layout thrashing if used frequently
- Affects the entire page layout
Visibility: Hidden Performance:
- Triggers only a repaint
- No layout recalculation required
- Significantly faster operation
- Better for animations and frequent toggling
- Minimal impact on surrounding elements
Best Practices for Performance
For optimal performance:
- Use
visibility: hiddenfor temporary hiding, animations, or frequent toggling - Use
display: nonefor permanent hiding or when space collapse is desired - Avoid repeatedly toggling
display: noneon complex layouts - Consider using
opacity: 0for fade animations instead of visibility changes
Child Element Behavior and Inheritance
One of the most important differences between these properties is how they handle child elements.
display: none Inheritance
When a parent element has display: none:
- All child elements are also hidden
- Children cannot override the parent's display property
- The entire subtree is removed from the render tree
- No child elements can be made visible
visibility: hidden Inheritance
With visibility: hidden, child elements have more flexibility:
- Child elements inherit the hidden state by default
- Children can override with
visibility: visible - Individual child elements can be selectively shown
- Provides fine-grained control over element visibility
.parent {
visibility: hidden;
}
.child {
visibility: visible; /* Still visible */
}
This makes visibility: hidden useful when you want finer control over nested elements.
Accessibility Considerations
The choice between these properties significantly impacts screen readers and assistive technologies.
Screen Reader Behavior
display: none:
- Completely removes elements from the accessibility tree
- Screen readers will not announce the content
- Ideal for decorative elements or truly hidden content
- Ensures content is not confusing for screen reader users
visibility: hidden:
- May still be present in the accessibility tree
- Some screen readers might still announce the content
- Behavior can vary between different assistive technologies
- May cause confusion if important content is hidden this way
Real-World Use Cases
When to Use Display: None
- Responsive Design: Hiding elements on specific screen sizes
- Conditional Rendering: Showing/hiding content based on user state
- Modal Overlays: Completely removing modal content when closed
- Progressive Enhancement: Hiding JavaScript-dependent content
- Print Stylesheets: Removing web-specific elements from printed pages
@media (max-width: 768px) {
.desktop-only {
display: none;
}
}
When to Use Visibility: Hidden
- Smooth Animations: Preparing elements for fade-in effects
- Form Validation: Temporarily hiding error messages
- Interactive Elements: Showing/hiding tooltips or dropdowns
- Grid Layouts: Maintaining consistent spacing while hiding items
- Image Galleries: Creating smooth transitions between images
.tooltip {
visibility: hidden;
opacity: 0;
transition: opacity 0.3s ease;
}
.tooltip.show {
visibility: visible;
opacity: 1;
}
Browser Compatibility and Considerations
Both display: none and visibility: hidden have excellent browser support, dating back to early CSS implementations. However, consider these points:
- Older browsers may handle transitions differently
- Mobile browsers might optimize these properties differently
- Some accessibility tools behave inconsistently across browsers
- Performance characteristics may vary on low-powered devices
Debugging and Development Tips
Common Pitfalls
- Forgetting about space preservation with
visibility: hidden - Performance issues from frequent
display: nonetoggling - Accessibility problems from improper usage
- Animation conflicts when combining with transitions
- Responsive design issues when elements don't collapse properly
Development Best Practices
- Use browser developer tools to inspect element states
- Test with screen readers during development
- Monitor performance with frequent visibility changes
- Document your choice reasoning for team members
- Create consistent utility classes for common patterns
Conclusion
The choice between display: none and visibility: hidden should be intentional and based on your specific requirements. Use display: none when you want complete removal from the layout and document flow, and visibility: hidden when you need to preserve space and layout structure.
Understanding these differences enables you to create more efficient, accessible, and maintainable CSS. Whether you're building responsive layouts, creating animations, or managing dynamic content, choosing the right property will improve both user experience and code quality.
Remember that the best choice depends on your specific context: performance requirements, accessibility needs, layout constraints, and user experience goals. By mastering both properties and understanding their nuances, you'll be better equipped to make informed decisions in your CSS development workflow.
Opinions expressed by DZone contributors are their own.
Comments