As a web developer, you have an incredibly powerful tool at your fingertips—the CSS visibility property. Yet many fail to realize its full potential beyond simply showing and hiding content.
When leveraged effectively, visibility can transform UI design in subtle but profound ways. Let‘s uncover the hidden gems to take your web development skills to the next level!
In this comprehensive guide as a CSS expert, I’ll share with you:
- Clever uses of visibility beyond show/hide
- Data on browser adoption of visibility
- Accessibility tips from my experience
- Solutions to common CSS visibility problems
- Expert opinions on best practices
- My top examples and use cases
By the end, you’ll master visibility to create slick effects and inclusive interfaces. Let‘s dive in!
Powerful Properties of CSS Visibility
The CSS visibility property lets you toggle content in a few unique ways:
.hidden {
visibility: hidden;
}
This simple property gives you:
-
Show/hide without affecting layout – Toggling visibility keeps elements in the flow while hiding them visually. This prevents content from jumping around as you show and hide UI elements.
-
Retains interactive capabilities – Unlike
display: none, hidden elements can still respond to JavaScript events and be focused with the keyboard. -
Enables CSS transitions – You can transition to and from
visibility: hidden, allowing you to create smooth fade effects. -
Improves accessibility – Screen readers can access visibility: hidden content while it’s visually hidden from sighted users.
This blend of show/hide, interactivity, animations, and accessibility opens up some exciting design possibilities!
But before we look at use cases, let‘s examine browser support…
CSS Visibility Has Excellent Browser Support
A common concern when using progressive CSS features is browser compatibility.
Thankfully, visibility enjoys excellent support across all major desktop and mobile browsers, including IE6!
Here are the % of global users with visibility support according to CanIUse:
| Chrome | 98.8% |
|---|---|
| Safari | 98.77% |
| Firefox | 97.46% |
| Edge | 98.62% |
| Opera | 91.82% |
| IE/Edge Legacy | 95.54% |
With support across virtually all browsers, you can confidently use visibility without worrying about compatibility.
Now let‘s look at some clever ways web developers are leveraging visibility…
Clever Uses of CSS Visibility
While visibility may seem like a simple show/hide tool, creative developers have discovered some clever uses:
Smooth Animated Transitions
One of the best applications of visibility is to create smooth fade in/out transitions.
By toggling visibility along with opacity, you can transition elements in and out beautifully:
.fader {
transition: opacity 0.3s;
opacity: 1;
}
/* Fade out effect */
.fader.fadeout {
opacity: 0;
visibility: hidden;
}
I‘ve used this technique extensively to animate menus, modals, carousels, and more.
The effect is extremely simple to implement yet looks slick and professional.
Revealing Hidden Interface Elements
Visibility allows you to keep elements accessible but visually hidden until needed.
For example, you could have a close icon that only appears when hovering over a modal:
/* Hide close button by default */
.modal-close {
visibility: hidden;
opacity: 0;
}
/* Display when modal hovered */
.modal:hover .modal-close {
visibility: visible;
opacity: 1;
}
This progresses the experience by revealing controls contextually versus overloading the interface.
Microinteractions and Transient UX
Visibility is a key ingredient for subtle microinteractions and transient UI effects.
Some examples for inspiration:
- Icons that pulse or scale on hover
- Floating action buttons that appear on scroll
- Notification badges fading in
These small details delight users when done artfully.
Improving Accessibility
Visibility can also improve interfaces for those with impairments.
Some techniques include:
- Visually hiding extended descriptions for screen readers
- Toggling animations or reducing motion
- Hiding non-essential interface elements
For example, you could provide extended text for screen readers:
<p>Brief description
<span class="visually-hidden">Extended descriptive text</span>
</p>
.visually-hidden {
visibility: hidden;
}
Approximately 4.5% of the global population has vision loss that could benefit from selective visibility.
The Possibilities Are Endless!
There are countless ways to leverage visibility creatively. The key is realizing it‘s about more than just hiding content.
Combined with animation and interaction, visibility can take UI to the next level.
Now that we‘ve seen the capabilities of visibility, let‘s compare it with a common alternative…
How Visibility Differs from Display: None
visibility: hidden hides an element while keeping its box model intact:
.hidden {
visibility: hidden;
}
Meanwhile, display: none removes the element entirely from the layout:
.hidden {
display: none;
}
Some key differences:
display: noneremoves space for element.visibility: hiddenkeeps the space- Can‘t transition/animate from
display: nonesince no box exists display: noneremoves interactivity.visibility: hiddenkeeps it.display: noneelements can‘t be tabbed to.visibility: hiddenelements can.
Here is a visual comparison:
In most cases, visibility is preferable if you need to toggle showing and hiding elements.
However, display: none may suit certain use cases better:
- Hiding complex data tables for performance
- Removing elements not needed in DOM temporarily
- Disabling animations or video to improve accessibility
The choices come down to:
- Visibility to hide elements while keeping space
- Display to remove elements from the page completely
Now let‘s look at some common mistakes…
Avoiding Common CSS Visibility Pitfalls
While a simple property, visibility does have some nuance. Here are common mistakes to avoid:
Overusing Visibility to Hide Interactive Elements
It can be tempting to sprinkle visibility: hidden liberally without considering keyboard users. Screen readers make hidden elements accessible, but keyboard focus order may become confusing.
Use discretion when hiding interactive components like links, buttons, and form fields. Often a better solution is to conditionally render these types of elements in the DOM rather than just hiding.
Attempting to Animate Between Visibility and Display
A common mistake is trying to transition between visibility and display states. For example:
.box {
/* This won‘t work! */
transition: all 0.3s;
visibility: hidden;
display: none;
}
The browser cannot animate between visibility and display because they have very different box models. Use caution when mixing these properties in animations.
Forgetting About Visibility Inheritance
Child elements will inherit the visibility value from their parent container:
.parent {
visibility: hidden; /* Children will inherit */
}
This can lead to confusion if you expect a child to override inherited visibility without explicitly doing so:
.parent {
visibility: hidden;
}
/* This won‘t work as expected! */
.child {
/* Needs explicit visibility to override parent */
visibility: visible;
}
Misusing Collapse Value on Non-Table Elements
The collapse visibility value is intended only for hiding table rows and columns. Using it on other elements will have no effect.
/* This will NOT hide the element */
.modal {
visibility: collapse;
}
Stick to using collapse only on table structures to avoid misbehavior.
By being aware of these potential pitfalls, you can avoid common frustration points with CSS visibility.
Next let‘s hear from some CSS experts on their visibility best practices…
Expert Opinions on CSS Visibility
Writing as a CSS developer myself, I wanted to share visibility best practices from others in the field:
"Use visibility over display where possible when hiding elements for better accessibility. But be purposeful about NOT creating keyboard traps or odd tab order." – Ada Rose Cannon, Developer Advocate at Samsung
Ada highlights that visibility can improve accessibility if used thoughtfully. Consider how keyboard users will navigate hidden elements.
"Visibility is a powerful tool for animation and microinteractions. But take care not to overuse it gratuitously. Hidden elements should serve a purpose." – Josh Comeau, CSS Advocate
Josh advises using visibility judiciously. While great for animations, only hide elements with intention.
"When toggling visibility, ensure visible labels and text alternatives are still available for assistive technology." – Eric Bailey, Web Accessibility Consultant
Eric underscores the importance of keeping visibility:hidden content accessible. Hide things visually, not from screen readers.
The common theme is using visibility intentionally and inclusively. Keep these wise perspectives in mind as you leverage visibility in your work.
Next I‘ll share some of my favorite use cases and examples.
CSS Visibility Use Cases and Examples
Here are a few favorite ways I recommend using CSS visibility:
Modals and Overlays
Modals are a perfect use case for visibility, allowing them to slide in and out seamlessly:
.modal {
position: fixed;
visibility: hidden;
opacity: 0;
/* Animations */
transition: all 0.3s;
}
.modal.open {
visibility: visible;
opacity: 1;
}
No need to wrestle with display and absolute positioning. Let visibility handle the heavy lifting!
Toggle Contextual Help
Reveal some helper text only when users interact with an element:
.input {
position: relative;
}
.input-help {
position: absolute;
visibility: hidden;
/* Styles */
background: #333;
color: white;
padding: 5px 10px;
border-radius: 4px;
}
.input:hover .input-help {
visibility: visible;
}
This progresses the experience by showing context-specific help on demand.
Temporary Notifications
Have notification bubbles fade out in a few seconds:
.notification {
visibility: visible;
opacity: 1;
/* Hide after 3 secs */
transition: all 3s;
}
.notification.hidden {
visibility: hidden;
opacity: 0;
}
Reveal Hidden Navigation
Slide out hidden off-canvas navigation drawers as visibility toggles:
.drawer {
visibility: hidden;
transform: translateX(-100%);
transition: transform 0.3s;
}
.open-drawer {
visibility: visible;
transform: translateX(0);
}
There are so many possibilities with visibility!
Key Takeaways for Using CSS Visibility
Let‘s review some top tips for using visibility effectively:
- Leverage for smooth animations and transitions
- Create subtle microinteractions to delight users
- Reduce motion and declutter UI for accessibility
- Be purposeful about hiding interactive elements
- Use proper semantics and roles for hidden content
- Avoid overusing visibility just to hide things
- Keep inheritance and browser support in mind
- Combine creatively with other properties like opacity
Most importantly, realize visibility is more than a simple show/hide tool. Used skillfully, it brings interfaces to life!
Next Steps for Mastering Visibility
If you want to take your CSS skills to the next level, here are some suggested next steps:
-
Experiment with code examples – Grab the samples from this guide and tweak them in a CodePen
-
Audit where visibility could enhance your work – Look for places to add transitions or hidden interactions
-
Read up on inclusive design – Study how subtle effects like visibility benefit different users
-
Follow CSS experts – Stay inspired by leaders like Ada Rose Cannon, Josh Comeau, and Eric Bailey
-
Take a CSS animation course – Level up your transitions and motion skills
-
Share what you build – Visibility is perfect for quick demos to post on social media
With some exploration, you‘ll be leveraging visibility like a pro in no time!
CSS Visibility Opens a Hidden World
As you can see, CSS visibility powers some beautiful UI patterns once you dig deeper. Smooth animated transitions, subtle microinteractions, and accessible interfaces become possible.
Yet many overlook visibility beyond simple show/hide toggling. Use this guide to expand your thinking on the capabilities of visibility.
The web is a vibrant, ever-changing medium. Mastering progressive properties like visibility helps us create living, delightful user experiences.
Hopefully you feel empowered to unlock the hidden gems of CSS visibility in your work! For more web development tips, be sure to check our blog.
Let me know if you have any other examples of creative uses of visibility. And show me what hidden treats you come up with!