How to optimize css with pseudo elements and transparency
This article mainly shows you "css how to use pseudo elements and transparency for optimization", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "css how to use pseudo elements and transparency to optimize" this article.
Optimize with pseudo elements and transparency
Optimized with pseudo elements and transparency, we add a before pseudo element to the above element, which is the same size as the parent div, and add the required final box shadow state to the element in advance, but the transparency of the element is 0.
Div {position: relative; width: 100px; height: 100px; box-shadow: 0 2px 4px rgba (0,0,0,0.3);} div::before {content: "; position: absolute; top: 0; left: 0; width: 100%; height: 100%; box-shadow: 0 5px 15px rgba (0,0,0,0.3); opacity: 0;}
Then, in hover, we just need to set the transparency of the pseudo element from 0 to 1.
Div:hover::before {opacity: 1;}
The advantage of this is that the actual shadow change is only a change in transparency, instead of constantly redrawing the shadow, which effectively improves the fluency of the shadow animation and makes it look more silky.
Why is it better to animate transparency opacity than to animate box-shadow? You can take a look at this table and list the effects of different attribute transformations on page rearrangement and redrawing:
The above is all the content of the article "how to optimize css with pseudo elements and transparency". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!