Gradient effect of CSS display properties
P粉608647033
2023-08-21 11:08:32
<p>I'm currently designing a CSS "super dropdown" - basically a regular pure CSS dropdown, but with different types of content in it. </p>
<p>Currently, <strong> it seems that CSS 3 transition effects do not apply to the "display" attribute</strong>, that is, there is no way to transition from <code>display: none</code> to < code>display: block</code> (or any combination). </p>
<p>In the example above, is there a way to make the second level menu "fade in" when someone hovers over the top menu item? </p>
<p>I know you can use transition effects on the <code>visibility:</code> attribute, but I can't figure out a way to use it effectively. </p>
<p>I also tried using height, but the results were terrible. </p>
<p>I also know that this can be easily achieved using JavaScript, but I wanted to challenge myself to just use CSS, but I feel like I'm a little out of my depth. </p>
You need to hide the element in other ways to make it work properly.
I achieved the effect by setting both
<div>
to absolute positioning, and the hidden one toopacity: 0
.If you switch the
display
attribute fromnone
toblock
, the transition effect for other elements will not occur.To solve this problem, always allow elements to be
display: block
, but hide the element by any of the following means:height
to0
.opacity
to0
.overflow: hidden
.There may be more solutions, but if you switch the element to
display: none
, the transition cannot be performed. For example, you might try something similar to:But this will notwork. In my experience, I've found that this doesn't have any effect.
So you always need to keep the element's
display: block
- but you can get around this by:You can connect two or more transition effects, and
visibility
comes in handy at this point.