Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to transform in both X & Y direction with CSS Only [duplicate]

Tags:

html

css

How can I rotate a card in X & Y direction at a time?

I have an image and I want to rotate that in both X & Y direction!

this is HTML,

<div class="card"></div>

and this is CSS,

.card {
background-image: url("https://i.stack.imgur.com/FW7wN.png");
height: 100px;
margin: 50px auto;
position: relative;
width: 100px;
-webkit-transition: .5s linear;
-webkit-transform-style: preserve-3d;
}


.card:hover {
-webkit-transform: rotateX(60deg);
}

Fiddle Project is Here

if I add Y transition to hover it is taking the only 2nd transition and ignoring 1st transition Like This

 .card:hover {
-webkit-transform: rotateX(60deg);
-webkit-transform: rotateY(60deg);
}

How can I translate that Card both in X&Y diagonals? Please help me!

like image 443
rakcode Avatar asked Nov 29 '16 14:11

rakcode


People also ask

How do you transform both X and Y in CSS?

You can use translateX() and translateY() to only move your element along the x and y axis respectively.

Can you have two transform CSS?

There are many ways of applying multiple transforms in CSS. In this snippet, we'll demonstrate how to achieve this using multiple values of the transform property, as well as using nested classes. In the example below, we'll use multiple values of the transform property.

How do you write multiple transformations in CSS?

Multiple transforms can be applied to an element in one property like this: transform: rotate(15deg) translateX(200px); This will rotate the element 15 degrees clockwise and then translate it 200px to the right.

What does transform translateY do?

translateY() Transform function for a 2d translation which moves an element on the y-axis by the given value. Note that the y-axis increases vertically downwards: positive lengths shift the element down, while negative lengths shift it up.


1 Answers

Use rotateX(), rotateY() or rotateZ() combined, like:

.card:hover {
  transform: rotateX(60deg) rotateY(60deg) rotateZ(60deg);
}

Have a look at this snippet below:

.card {
    background-image: url("https://i.stack.imgur.com/FW7wN.png");
    height: 100px;
    margin: 50px auto;
    position: relative;
    width: 100px;
    -webkit-transition: .5s linear;
    -webkit-transform-style: preserve-3d;
}
    
.card:hover {
    -webkit-transform: rotateX(60deg) rotateY(60deg) rotateZ(60deg);
}
<div class="card"></div>

Hope this helps!

like image 195
Saurav Rastogi Avatar answered Nov 15 '22 02:11

Saurav Rastogi