Analysis of display Property Impact on visibility and opacity in CSS Transitions

Nov 21, 2025 · Programming · 10 views · 7.8

Keywords: CSS transitions | display property | visibility property | opacity property | navigation menu

Abstract: This article provides an in-depth examination of the interaction mechanisms between the display property and visibility/opacity properties in CSS transition animations. Through analyzing the implementation of transition effects from hidden to visible states in navigation menus, it reveals the non-animatable nature of the display property and its overriding effect on other animatable properties. The paper explains why using display: none alongside visibility: hidden in CSS transitions causes transition failures and offers solutions using only visibility and opacity for smooth transitions. Alternative approaches using CSS keyframe animations are also compared, providing comprehensive implementation guidance for front-end developers.

Fundamental Principles of CSS Transitions

In CSS, transitions provide a mechanism for smooth property value changes. When an element's property value changes, the browser automatically calculates intermediate states to create fluid animation effects. However, not all CSS properties support transitions, with the display property being a classic example of a non-animatable property.

Special Characteristics of the display Property

The display property defines an element's display type, such as block, inline, or none. When an element's display value is none, the element becomes not only invisible but is completely removed from the document flow, occupying no space. This "all-or-nothing" characteristic makes the display property unsuitable for smooth transitions.

Problem Analysis: Causes of Transition Failure

In the original code, the sub-navigation menu simultaneously sets display: none, visibility: hidden, and opacity: 0:

nav.main ul ul {
    position: absolute;
    list-style: none;
    display: none;
    opacity: 0;
    visibility: hidden;
    padding: 10px;
    background-color: rgba(92, 91, 87, 0.9);
    -webkit-transition: opacity 600ms, visibility 600ms;
            transition: opacity 600ms, visibility 600ms;
}

When hovering occurs, the code attempts to change display to block while modifying visibility and opacity:

nav.main ul li:hover ul {
    display: block;
    visibility: visible;
    opacity: 1;
}

The key issue here is that the switch from display: none to display: block happens instantaneously, with no intermediate states. More importantly, when an element is in display: none state, the browser ignores all other style properties of that element, including transition effects. Only when the element becomes visible does the browser begin calculating and applying transition animations.

Solution: Removing the display Property

The optimal solution involves removing the display property and using only visibility and opacity to control element visibility:

nav.main ul ul {
    position: absolute;
    list-style: none;
    opacity: 0;
    visibility: hidden;
    padding: 10px;
    background-color: rgba(92, 91, 87, 0.9);
    -webkit-transition: opacity 600ms, visibility 600ms;
            transition: opacity 600ms, visibility 600ms;
}

nav.main ul li:hover ul {
    visibility: visible;
    opacity: 1;
}

This approach offers several advantages:

Alternative Approach: Using CSS Animations

While the transition approach is optimal, CSS animations (@keyframes) can serve as an alternative in specific scenarios:

nav.main ul li:hover ul {
    display: block;
    visibility: visible;
    opacity: 1;
    animation: fade 1s;
}

@keyframes fade {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

This method, while achieving animation effects, has several limitations:

Application of the position Property

In situations where developers want hidden elements to occupy no space, the position property can be utilized:

.content-page {
    position: absolute;
    opacity: 0;
}

.content-page.active {
    position: static;
    opacity: 1;
    transition: opacity 1s linear;
}

By setting elements to position: absolute, they can be removed from the normal document flow, achieving effects similar to display: none while maintaining transition capabilities.

Practical Implementation Recommendations

In actual development, the following best practices are recommended:

  1. Prioritize using visibility and opacity combinations for show/hide transitions
  2. Avoid using display: none on elements requiring transition animations
  3. For scenarios requiring complete removal from document flow, consider using position: absolute
  4. Appropriately set transition durations and easing functions to ensure natural, fluid animation effects

Browser Compatibility Considerations

The solutions discussed in this article have good compatibility with modern browsers. For older browsers, consider:

By deeply understanding CSS property characteristics and browser rendering mechanisms, developers can better control the show/hide effects of web page elements, creating smoother and more elegant user experiences.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.