In-depth understanding of CSS Transform: Solving UI Layout and Dynamic Progress
In modern web development, CSS
transform
Properties are the preferred tool for handling the position, size and rotation of elements. Different from changes
top
、
left
or
width
Waiting for layout properties,
transform
Composition Layer), which means it does not cause page re-alignment, thus providing extremely fluid visual experience. This paper will explore
transform
How to address the challenges of development in practice.
1. Addressing the challenge of absolute centering of elements
Where the width is known or unknown, the perfect placement of an element in a container has been a pain for the front end developer. Traditional
margin: auto
Or percentage deviations often require negative margins.
Solutions:
Use
translate
。 Through Settings
top: 50%; left: 50%; transform: translate(-50%, -50%);
。 Here.
-50%
It is calculated at a width relative to the element itself, so it can remain at the geometric centre regardless of the change in the element content. This is one of the most robust middle-class programmes available.
2. Upgrading animation performance and silk slide
When you need to achieve a mouse-sliding effect or a complex transect animation, if
left
property, the browser must recalculate the layout of the entire page on each frame, which will cause a visible Carton on the lower end device.
Best practices:
Always first.
translate3d
or
translate
。 Because the change is in GPU. , it moves pixels directly on the screen, by bypassing the complex layout calculation phase. You can apply directly to the parameters that you adjust with this generator. CSS Transtion or Animation ensures that the animation meets the 60FPS slurb.
3. Creating a highly deep 3D visual level
transform
Not just 2D plane. Complement
perspective
(Perceive) Properties, you can make elements move on Z axis or rotate around X/Y axis, thereby creating a real 3D effect.
Application scene:
Common 3D flip cards, cube menus, or space-sensitive picture walls are all combined by
rotateY
、
translateZ
equals the properties that are achieved. This brings home page design with a sense of precision and interactive depth similar to that of the original App.
4. Use of vigilance
-
Stack order:
It's applied.
transformThe element will automatically create a new Stacking Context, which may affectz-indexShow. -
Properties order:
Yes.
transformin a string, the order of the attribute will influence the final result. For example, rotation before smoothing and lateralization over rotation have a completely different effect. This tool is based on generictranslate → rotate → scale → skewThe sequenced generation of codes ensures predictability of results. -
Text blurry:
In some browsers, very small multiples
scaleOr non-integer.translateThis may lead to ambiguity of text. It is recommended that even pixels be used or added to the variable as much as possiblebackface-visibility: hidden;。