CSS visibility Property
The CSS visibility property controls whether an element is visible while generally preserving the space it occupies in the document layout.
Unlike display: none, which removes an element from the layout, visibility: hidden hides the element without removing its layout space. The property also supports collapse, which has special behavior for table rows, columns, and certain flex and ruby layout elements.
visibility Syntax
The visibility property accepts three keyword values: visible, hidden, and collapse.
Basic Syntax
.element {
visibility: visible;
}
Hide an Element
.element {
visibility: hidden;
}
Collapse an Element
.element {
visibility: collapse;
}
CSS-Wide Keywords
.element {
visibility: initial;
}
.element {
visibility: inherit;
}
.element {
visibility: unset;
}
.element {
visibility: revert;
}
.element {
visibility: revert-layer;
}
Invalid Values
.element {
visibility: none;
}
.element {
visibility: invisible;
}
.element {
visibility: 0;
}
Values such as none, invisible, and 0 are not valid for the visibility property.
Property Values
| Value | Description |
|---|---|
visible |
The element is visible. This is the initial value. |
hidden |
The element is not painted, but its layout space is preserved. |
collapse |
Collapses certain table, flex, and ruby layout elements. For other elements, it generally behaves like hidden. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | visible |
| Inherited | Yes |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Discrete, with special interpolation behavior for visible |
| Percentages | Not applicable |
| Shorthand | None |
| Specification | CSS Display Module Level 3 |
How It Works
The visibility property determines whether an element and its content are painted on the screen.
When an element uses visibility: hidden, it remains part of the layout. Surrounding elements continue to occupy their original positions.
HTML Example
<div class="box first">First Box</div>
<div class="box second">Second Box</div>
<div class="box third">Third Box</div>
CSS Example
.box {
padding: 15px;
margin-bottom: 10px;
background-color: #e5eef7;
}
.second {
visibility: hidden;
}
Result
The first and third boxes remain visible. The second box is hidden, but the space between the other boxes remains.
Layout Behavior
Changing an element from visible to hidden generally does not cause surrounding elements to move because the hidden element continues to participate in layout.
Painting Behavior
The hidden element and its normally inherited hidden descendants are not painted. A descendant can become visible again by explicitly setting visibility: visible.
Interaction Behavior
Hidden elements cannot be directly targeted by pointer interactions and are removed from normal keyboard focus navigation.
The visible Value
The visible value makes an element visible, provided other CSS properties or document conditions do not prevent it from being displayed.
Syntax
.element {
visibility: visible;
}
Default Behavior
The initial value of visibility is visible.
Restore Visibility
.element {
visibility: hidden;
}
.element.active {
visibility: visible;
}
When the element receives the active class, it becomes visible again.
Visible Child Inside a Hidden Parent
.parent {
visibility: hidden;
}
.parent .child {
visibility: visible;
}
Because visibility is inherited but can be overridden, the child can be visible even when its parent is hidden.
Important Consideration
Setting visibility: visible does not override display: none. An element removed from layout by display: none remains undisplayed.
The hidden Value
The hidden value prevents an element from being painted while preserving the space it occupies in the layout.
Syntax
.element {
visibility: hidden;
}
Hidden Paragraph
.hidden-paragraph {
visibility: hidden;
}
Hidden Button
.hidden-button {
visibility: hidden;
}
The button remains part of the layout but cannot be activated through normal pointer or keyboard interaction while hidden.
Hidden Image
.hidden-image {
visibility: hidden;
}
The image is not painted, but its layout dimensions remain reserved.
Hidden Container
.hidden-container {
visibility: hidden;
}
The container and its descendants are normally hidden unless a descendant explicitly overrides the inherited visibility value.
Important Consideration
Use visibility: hidden when you need to hide an element without causing surrounding content to shift.
The collapse Value
The collapse value has special behavior for certain layout structures, particularly table rows and columns.
Basic Syntax
.element {
visibility: collapse;
}
Collapse a Table Row
table tr.collapsed {
visibility: collapse;
}
For table rows, collapse is intended to remove the row's occupied space while preserving the table's overall column sizing calculations.
Collapse a Table Column
table col.collapsed {
visibility: collapse;
}
For table columns, the value is intended to remove the column's occupied space while preserving relevant table sizing behavior.
HTML Table Example
<table>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
<tr class="collapsed">
<td>Example A</td>
<td>Hidden</td>
</tr>
<tr>
<td>Example B</td>
<td>Visible</td>
</tr>
</table>
CSS Example
tr.collapsed {
visibility: collapse;
}
Flex Items
.flex-item.collapsed {
visibility: collapse;
}
For flex items, the specification defines a collapsed item that leaves a strut preserving the flex line's cross size. However, browser implementations vary, and some browsers treat this value like hidden.
Other Elements
For ordinary block and inline elements, collapse generally behaves like hidden.
Browser Differences
Support for the special collapsing behavior is inconsistent. Some browsers preserve space where others collapse it.
Practical Recommendation
When predictable removal from layout is more important than the special table or flex behavior, use display: none.
visibility vs. display
The visibility and display properties can both hide elements, but they affect layout differently.
Using visibility: hidden
.element {
visibility: hidden;
}
The element is hidden, but its layout space remains.
Using display: none
.element {
display: none;
}
The element and its descendants are removed from the layout, and surrounding content can move into the space.
Comparison Table
| Behavior | visibility: hidden |
display: none |
|---|---|---|
| Element is painted | No | No |
| Layout space preserved | Yes | No |
| Normal pointer interaction | No | No |
| Normal keyboard focus | No | No |
| Visible descendant override | Possible | No |
| Typical use | Hide while preserving layout | Remove from layout |
When to Use visibility
Use visibility: hidden when surrounding elements should remain in their original positions.
When to Use display
Use display: none when the hidden element should not occupy layout space.
visibility vs. opacity
The visibility and opacity properties can both make an element invisible, but they differ in interaction and accessibility behavior.
Using visibility: hidden
.element {
visibility: hidden;
}
The element is hidden and cannot receive normal pointer or keyboard interaction.
Using opacity: 0
.element {
opacity: 0;
}
The element becomes fully transparent but remains part of the layout and can still receive pointer events and keyboard focus.
Comparison Table
| Behavior | visibility: hidden |
opacity: 0 |
|---|---|---|
| Element is visible | No | No |
| Layout space preserved | Yes | Yes |
| Pointer events | Not normally targeted | Still possible |
| Keyboard focus | Not normally focusable | Still possible |
| Opacity animation | Requires combining properties | Supported directly |
Combining visibility and opacity
.panel {
opacity: 0;
visibility: hidden;
transition: opacity 300ms ease, visibility 300ms;
}
.panel.active {
opacity: 1;
visibility: visible;
}
This pattern allows the panel to fade while becoming non-interactive after it is hidden.
Important Consideration
Using opacity: 0 alone does not make an interactive element inaccessible to keyboard users.
Inheritance
The visibility property is inherited. Child elements receive their parent's computed visibility value unless they specify another value.
Hidden Parent
.parent {
visibility: hidden;
}
The parent and its descendants are normally hidden.
Visible Child
.parent {
visibility: hidden;
}
.parent .child {
visibility: visible;
}
The child explicitly overrides the inherited value and can be painted even though the parent is hidden.
Explicit Inheritance
.child {
visibility: inherit;
}
The child uses its parent's computed visibility value.
Restore the Initial Value
.element {
visibility: initial;
}
This restores the initial value of visible.
Using unset
.element {
visibility: unset;
}
Because visibility is inherited, unset behaves like inherit.
Important Consideration
Unlike display: none, a hidden parent's visibility can be overridden by a descendant using visibility: visible.
Animating visibility
The visibility property can participate in CSS transitions and animations, but it does not interpolate continuously like opacity.
Discrete Animation
When transitioning between visible and a non-visible value, the element remains visible during the intermediate portion of the transition.
Basic Transition
.panel {
visibility: hidden;
transition: visibility 300ms;
}
.panel.active {
visibility: visible;
}
The visibility change follows discrete transition behavior rather than gradually fading.
Fade With opacity
.panel {
opacity: 0;
visibility: hidden;
transition: opacity 300ms ease, visibility 300ms;
}
.panel.active {
opacity: 1;
visibility: visible;
}
Combining the properties produces a fade effect while ensuring the panel is hidden and non-interactive when inactive.
Keyframe Animation
@keyframes show-panel {
from {
opacity: 0;
visibility: hidden;
}
to {
opacity: 1;
visibility: visible;
}
}
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.panel {
transition: none;
}
}
Important Consideration
Use opacity for gradual fading and visibility to control whether the element is painted and interactive.
Practical Examples
Hide a Paragraph
.hidden-text {
visibility: hidden;
}
Show an Element
.visible-element {
visibility: visible;
}
Hide a Button
.hidden-button {
visibility: hidden;
}
Hide an Image Without Moving Content
.image-placeholder img {
visibility: hidden;
}
Show a Tooltip on Hover
.tooltip {
visibility: hidden;
}
.tooltip-container:hover .tooltip,
.tooltip-container:focus-within .tooltip {
visibility: visible;
}
The tooltip becomes visible when the container is hovered or receives keyboard focus.
Hide a Panel
.panel {
visibility: hidden;
}
.panel.active {
visibility: visible;
}
Collapse a Table Row
tr.collapsed {
visibility: collapse;
}
Visible Child Inside Hidden Parent
.parent {
visibility: hidden;
}
.parent .child {
visibility: visible;
}
Fade an Element In and Out
.fade-element {
opacity: 0;
visibility: hidden;
transition: opacity 300ms ease, visibility 300ms;
}
.fade-element.active {
opacity: 1;
visibility: visible;
}
visibility Example
This interactive example demonstrates how visibility: hidden hides an element without removing its layout space.
Click the button to hide or show the middle box. Notice that the first and third boxes remain in the same positions.
HTML Example
<div class="visibility-demo">
<button type="button" class="visibility-button">Hide Middle Box</button>
<div class="visibility-box">First Box</div>
<div class="visibility-box middle-box">Second Box</div>
<div class="visibility-box">Third Box</div>
</div>
CSS Example
.visibility-button {
margin-bottom: 15px;
padding: 10px 20px;
color: #fff;
background-color: #0066cc;
border: 0;
cursor: pointer;
}
.visibility-box {
padding: 15px;
margin-bottom: 10px;
background-color: #e5eef7;
border: 2px solid #0066cc;
border-radius: 6px;
text-align: center;
}
.middle-box.hidden {
visibility: hidden;
}
JavaScript Example
const demo = document.querySelector(".visibility-demo");
const button = demo.querySelector(".visibility-button");
const middleBox = demo.querySelector(".middle-box");
button.addEventListener("click", () => {
const hidden = middleBox.classList.toggle("hidden");
button.textContent = hidden ? "Show Middle Box" : "Hide Middle Box";
});
Understanding the Results
Initially, all three boxes are visible.
Hiding the Middle Box
Clicking the button adds the hidden class to the second box, applying visibility: hidden.
Preserving Layout Space
The middle box disappears, but its original space remains between the first and third boxes.
Showing the Box Again
Clicking the button again removes the hidden class and restores the middle box.
Experiment With display
.middle-box.hidden {
display: none;
}
With display: none, the middle box is removed from layout and the third box moves upward.
Experiment With opacity
.middle-box.hidden {
opacity: 0;
}
The middle box becomes transparent but remains in the layout and can still receive pointer interactions.
Experiment With collapse
.middle-box.hidden {
visibility: collapse;
}
For an ordinary block element, collapse generally behaves like hidden, preserving its layout space.
Accessibility Considerations
The visibility property affects more than visual appearance. Hidden elements are generally excluded from the accessibility tree and normal keyboard focus navigation.
Hidden Content Is Not Accessible
Content using visibility: hidden is generally unavailable to screen readers and other assistive technologies.
Avoid Hiding Important Information
Do not hide essential instructions, labels, or error messages that users need to complete a task.
Keyboard Focus
Interactive elements hidden with visibility: hidden are removed from normal keyboard focus navigation.
Use Appropriate Disclosure Controls
For expandable sections, menus, and dialogs, ensure that the visible state and relevant accessibility attributes are updated consistently.
Do Not Use opacity Alone to Hide Controls
An element with opacity: 0 can still receive keyboard focus. Use an appropriate hiding mechanism when an interactive control should be unavailable.
Respect Reduced Motion Preferences
When combining visibility with opacity transitions, provide a reduced-motion alternative.
@media (prefers-reduced-motion: reduce) {
.panel {
transition: none;
}
}
Test With Assistive Technology
Verify that hidden content is excluded appropriately and that revealed content becomes accessible when shown.
Common Mistakes
Using visibility: none
.element {
visibility: none;
}
The value none is invalid. Use hidden or display: none, depending on the desired layout behavior.
Expecting Hidden Elements to Release Space
The hidden value preserves layout space. Use display: none when the element should be removed from layout.
Assuming opacity Disables Interaction
Fully transparent elements can still receive pointer events and keyboard focus.
Assuming Hidden Parents Always Hide Every Descendant
A descendant can override inherited visibility by explicitly setting visibility: visible.
Using collapse for Ordinary Elements
The collapse value generally behaves like hidden for ordinary block and inline elements.
Expecting Identical collapse Behavior Across Browsers
Special collapsing behavior for table and flex elements is not implemented consistently in every browser.
Trying to Override display: none
.element {
display: none;
visibility: visible;
}
The element remains undisplayed because visibility: visible does not override display: none.
Expecting a Smooth Fade From visibility Alone
The property uses discrete animation behavior. Combine it with opacity for gradual fading.
Hiding Required Information
Content hidden with visibility: hidden is generally unavailable to assistive technologies.
Important Considerations
Keep the following points in mind when using visibility:
- The property controls whether elements are painted.
- Its initial value is
visible. - The property is inherited.
- It accepts
visible,hidden, andcollapse. - The
hiddenvalue generally preserves layout space. - The
visiblevalue restores visibility when other conditions allow the element to be displayed. - The
collapsevalue has special behavior for certain table, flex, and ruby layout elements. - Special collapse behavior varies across browsers.
- For ordinary elements,
collapsegenerally behaves likehidden. - Hidden elements remain part of the layout.
- Hidden elements are not normally available for pointer interaction.
- Hidden interactive elements are removed from normal keyboard focus navigation.
- Hidden content is generally excluded from the accessibility tree.
- Child elements inherit their parent's visibility value.
- A descendant can override inherited hidden visibility with
visible. - The
visibilityproperty does not overridedisplay: none. - The
display: nonedeclaration removes an element from layout. - The
opacity: 0declaration makes an element transparent without disabling interaction. - The
visibilityproperty uses discrete animation behavior. - Combining
visibilityandopacityallows fade effects with appropriate interaction behavior. - Use
visibility: hiddenwhen preserving layout space is important. - Use
display: nonewhen removing layout space is important. - Use appropriate accessibility attributes and focus management for interactive disclosure components.
- Respect reduced-motion preferences when adding animated visibility changes.
- Test special
collapsebehavior in target browsers.
Browser Support
The CSS visibility property is widely available across modern browsers and has been supported for many years.
The visible and hidden values have broad compatibility. Special behavior for collapse, particularly with table columns and flex items, may vary by browser.
For current compatibility information, visit Can I Use? and search for visibility.
Standard Declaration
.element {
visibility: hidden;
}
CSS Feature Detection
@supports (visibility: hidden) {
.element {
visibility: hidden;
}
}
Feature detection is generally unnecessary for the widely supported visible and hidden values.
Vendor Prefixes
The visibility property does not require vendor prefixes in modern browsers.
Testing Recommendations
Test collapse with tables and flex layouts when relying on its special layout behavior. Also verify accessibility and focus behavior when showing or hiding interactive content.
Specifications
The visibility property is defined in CSS Display Module Level 3.
CSS Display Module Level 3 - Visibility
The specification defines the property's visibility values and their effects on painting and layout.
For additional documentation and examples, consult MDN Web Docs.
