Transform
Transform utility classes.
Intro #
The transform utility classes take advantage of CSS custom properties: you can combine multiple transformations by applying multiple classes (i.e., different transform classes won't overwite each other).
[class^="flip"], [class*=" flip"],
[class^="-rotate"], [class*=" -rotate"],
[class^="rotate"], [class*=" rotate"],
[class^="-translate"], [class*=" -translate"],
[class^="translate"], [class*=" translate"],
[class^="-scale"], [class*=" -scale"],
[class^="scale"], [class*=" scale"],
[class^="-skew"], [class*=" -skew"]
[class^="skew"], [class*=" skew"] {
--translate: 0;
--rotate: 0;
--skew: 0;
--scale: 1;
transform: translate3d(var(--translate-x, var(--translate)), var(--translate-y, var(--translate)), var(--translate-z, 0)) rotateX(var(--rotate-x, 0)) rotateY(var(--rotate-y, 0)) rotateZ(var(--rotate-z, var(--rotate))) skewX(var(--skew-x, var(--skew))) skewY(var(--skew-y, 0)) scaleX(var(--scale-x, var(--scale))) scaleY(var(--scale-y, var(--scale)));
}
To create a new class, update the relative custom property value:
.rotate-45 {
--rotate: 45deg;
}
.hover\:scale-2:hover {
--scale: 2;
}
.-translate-y-100\% {
--translate-y: -100%;
}
Scale #
Scale utility classes:
CSS Class | Description |
---|---|
f{p}lip |
--scale: -1; → flip element horizontally and vertically |
f{p}lip-x |
--scale-x: -1; → flip element horizontally |
f{p}lip-y |
--scale-y: -1; → flip element vertically |
Translate #
Translate utility classes:
CSS Class | Description |
---|---|
-{p}translate-50% |
--translate: -50%; |
-{p}translate-x-50% |
--translate-x: -50%; |
-{p}translate-y-50% |
--translate-y: -50%; |
t{p}ranslate-50% |
--translate: 50%; |
t{p}ranslate-x-50% |
--translate-x: 50%; |
t{p}ranslate-y-50% |
--translate-y: 50%; |
How to center an absolutely positioned element using the position and transform classes:
<!-- how to center element -->
<div class="position-relative">
<div class="position-absolute top-50% left-50% -translate-50%">
Centered content!
</div>
</div>
<!-- how to horizontally center element -->
<div class="position-relative">
<div class="position-absolute left-50% -translate-x-50%">
Centered content!
</div>
</div>
<!-- how to vertically center element -->
<div class="position-relative">
<div class="position-absolute top-50% -translate-y-50%">
Centered content!
</div>
</div>
Rotate #
Rotate utility classes:
CSS Class | Description |
---|---|
r{p}otate-90 |
--rotate: 90deg; |
r{p}otate-180 |
--rotate: 180deg; |
r{p}otate-270 |
--rotate: 270deg; |
Transform origin #
Transform-origin utility classes:
CSS Class | Description |
---|---|
o{p}rigin-center |
set transform-origin in the center |
o{p}rigin-top |
set transform-origin in the top center |
o{p}rigin-right |
set transform-origin in the center right |
o{p}rigin-bottom |
set transform-origin in the bottom center |
o{p}rigin-left |
set transform-origin in the center left |
o{p}rigin-top-left |
set transform-origin in the top left |
o{p}rigin-top-right |
set transform-origin in the top right |
o{p}rigin-bottom-left |
set transform-origin in the bottom left |
o{p}rigin-bottom-right |
set transform-origin in the bottom right |