brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the CSS translate, rotate and scale properties change one transform without losing the others

Css
~/tips/tip-css-individual-transform-properties

transition: scale 0.2s;

A hover scale no longer wipes out the translate that positions the element.

With transform, a hover rule that adds scale() replaces the whole list, so an element centered with translate() jumps away. The separate properties fix that.

.badge {
  position: absolute;
  left: 50%;
  translate: -50% 0;
  transition: scale 0.2s;
}
.badge:hover {
  scale: 1.1;
}

translate, rotate and scale are their own properties, so changing one leaves the others alone. The hover rule above only touches the scale, and the badge stays centered. It also makes transitions simpler, because I can transition just scale instead of the whole transform.

The syntax is a little different: values are separated by spaces instead of commas, like translate: 10px 20px and scale: 1.2 0.8.

One thing that can surprise you is the order. The browser always applies translate first, then rotate, then scale, and then whatever is in transform. With transform you choose the order yourself, and a different order gives a different result. If you need a rotate before a translate, stick to transform for that element.

Chrome 104, Firefox 72 and Safari 14.1 support them, so they are safe to use now.

Further reading: MDN: translate, MDN: scale

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link