It seems a bit counter intuitive to have properties, which on their own are key:value pairs, be grouped together. Especially since most of them are quite different and can still be used simultaneously as long as you know how to write it. In case it's not clear what I'm talking about, my question is this. Why is the following:
transform: rotate(40deg) scaleX(1,5) translate(-10px, 20px);
Not written like so:
rotation: 40deg;
scaleX: 1.5;
translate: -10px 20px;
This way each property can be manipulated on their own, without having to keep track of the sibling values. There must be a good reason the W3 choose this approach, so does anyone know it?
In the example below, we'll use multiple values of the transform property. It is possible to add multiple values applied one after another. The values must be separated by space.
The transform CSS property lets you rotate, scale, skew, or translate an element. It modifies the coordinate space of the CSS visual formatting model.
The transform property applies a 2D or 3D transformation to an element. This property allows you to rotate, scale, move, skew, etc., elements.
Abstract. CSS transforms allows elements styled with CSS to be transformed in two-dimensional or three-dimensional space. This spec adds new transform functions and properties for three-dimensional transforms, and convenience functions for simple transforms.
That's because transforms are not commutative. Therefore, the order matters.
For example, if you use a translation after a rotation, the translation direction will be rotated too.
.first::after {
transform: rotate(180deg) translateX(50px);
}
.second::after {
transform: translateX(50px) rotate(180deg);
}
body {
display: flex;
flex-direction: space-around;
}
div {
height: 100px;
width: 100px;
border: 5px solid;
margin: 25px auto;
}
div::after {
content: 'Hello';
display: block;
height: 100%;
width: 100%;
background: yellow;
opacity: .5;
}
.first::after {
transform: rotate(180deg) translateX(50px);
}
.second::after {
transform: translateX(50px) rotate(180deg);
}
<div class="first"></div>
<div class="second"></div>
With different CSS properties, you couldn't choose the order you want. That's the limitation of CSS Transforms level 2 that BoltClock mentioned, the spec defines an order and you can't alter it.
The CSS transform
property originated from SVG transforms, where a space-delimited list of transform functions is provided as a value for the SVG transform
attribute. The CSS transform
property is most likely a direct port of that.
Of course, hindsight has shown this to be a terrible mistake, and the transform functions will be promoted to their own CSS properties in CSS Transforms level 2, with almost the exact syntax that you have proposed (there aren't individual scaleX
/Y
/Z
properties yet). Their interaction with the transform
property is accounted for, although the draft notes that the transformation matrix will be changed to accommodate how the new properties will interact with respect to the cascade.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With