Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

translateX and translateY on same element?

Is it possible to apply a CC translate X and Y on the same element?

If I try this the translateX is overridden by the translateY:

.something { 
        transform: translateX(-50%);
        transform: translateY(-50%);
}
like image 881
Evanss Avatar asked Apr 15 '15 11:04

Evanss


3 Answers

You can do something like this

transform:translate(-50%,-50%);
like image 196
Akshay Avatar answered Oct 15 '22 20:10

Akshay


In your case, you can apply both X and Y translations with the translate property :

transform: translate(tx[, ty]) /* one or two <translation-value> values */

[source: MDN]

for your example, it would look like this :

.something { 
  transform: translate(-50%,-50%);
}

DEMO:

div{
  position:absolute;
  top:50%; left:50%;
  width:100px; height:100px;
  transform: translate(-50%,-50%);
  background:tomato;
}
<div></div>

As stated by this answer How to apply multiple transforms in CSS3? you can apply several transforms on the same element by specifying them on the same declaration :

.something { 
  transform: translateX(-50%) translateY(-50%);
}
like image 45
web-tiki Avatar answered Oct 15 '22 21:10

web-tiki


You can combine X and Y translates into a single expression:

transform: translate(10px, 20px); /* translate X by 10px, y by 20px */

And, in general, several transforms into a single rule:

transform: translateX(10px) translateY(20px) scale(1.5) rotate(45deg);
like image 37
joews Avatar answered Oct 15 '22 20:10

joews