Home > Article > Web Front-end > Sharing tips on using CSS in actual combat
This time I will share with you the skills of using CSS in actual combat. What are the precautions for using CSS in actual combat? The following is a practical case, let’s take a look.
Create clipping animationFor clipping animation, use clip-path instead of width/height to avoid low performance caused by DOM rearrangement..animate { width: 200px; height: 200px; background: #000; animation: 1s clip; } @keyframes clip { 0% { clip-path: inset(0 0 0 0); } 100% { clip-path: inset(0 100% 100% 0); } }clip-path can also be used to cut other regular/irregular graphics
.clip { clip-path: polygon(0 100%, 50% 0, 100% 100%, 0 30%, 100% 30%); /* 多边形 */ clip-path: circle(30px at 35px 35px); /* 圆形 */ clip-path: ellipse(30px 25px at 35px 35px); /* 椭圆 */ }Optimize animation performanceIn addition to using transform3d to enable gpu acceleration, you can also use will-change forces gpu acceleration to optimize animation performance
.animate { width: 200px; height: 200px; background: #000; animation: 1s clip; will-change: clip-path; } @keyframes clip { 0% { clip-path: inset(0 0 0 0); } 100% { clip-path: inset(0 100% 100% 0); } }Achieve aspect ratioUse padding simulation, and then use child elements
/* 1:1 */ .container { width: 200px; } .container:after { display: block; content: ' '; padding-top: 100%; } /* 16:9 */ .container { width: 200px; } .container:after { display: block; content: ' '; padding-top: calc(100% * 9 / 16); }vertical CenterOur commonly used method:
padding up and down support,
display: table ,
position<a href="http://www.php.cn/wiki/902.html" target="_blank"> margin: auto</a>,
absolute positioning margin, etc. These are hack methods that are not commonly used and can only be used in special scenarios. They are hacks before CSS3. CSS3 You won't have to use these to achieve vertical centering later on, so I won't say more.
display: flex is a snake oil. It can be used directly in most scenarios, but there are still some special scenarios that cannot be used:
Initializing the project structure in the front-end project
##Bugs encountered when using js variable scopeThe above is the detailed content of Sharing tips on using CSS in actual combat. For more information, please follow other related articles on the PHP Chinese website!