Transformations on CSS display properties
P粉958986070
2023-08-23 12:27:32
<p>I'm currently designing a CSS "megadropdown" menu - basically a regular pure CSS dropdown, but with different types of content. </p>
<p>Currently, <strong>CSS 3 transitions do not appear to work with the 'display' property</strong>, i.e. you cannot make any kind of transition/code> from <code>display: none< to < code>display: block</code> (or any combination). </code></p><code>
<p>Is there a way to make the second-level menu in the example above "fade in" when someone hovers over one of the top-level menu items? </p>
<p>I know you can use a transform on the <code>visibility:</code> attribute, but I can't think of a way to use it efficiently. </p>
<p>I also tried using height, but failed miserably. </p>
<p>I also know it's easy to do this using JavaScript, but I want to challenge myself to just use CSS, and I think my abilities are a bit lacking. </p></code>
You need to hide the element in some other way for this to work properly.
I achieved this effect by positioning both absolutely and setting the hidden one to - Set the
- Set the
- Place the element outside another frame with an
opacity: 0
.If you switch the
display
attribute fromnone
toblock
, the transition will not occur on other elements. 强>To resolve this issue, always allow the element to be
display: block
, but hide the element by adjusting any of the following:height
to0
.opacity
to0
.overflow:hidden
element.There may be more solutions, but if you switch the element to
display: none
, the conversion cannot be performed. For example, you could try something like this:But this doesn't work. In my experience, I've found that this doesn't do anything.
So you always need to preserve the element
display: block
- but you can get around it by doing:You can connect two or more transitions, and
visibility
will come in handy this time.