28

I have an element:

elem
   transform translateY(5px) scale(1.2) 

Now on hover I want it to move down an additional 5px

elem:hover
   transform translateY(5px)

Obviously this would overwrite the previous transform. Is there anyway to set it to move an additional 5 without knowing what the previous transform state is?

Thanks.

2
  • 3
    It would be cool to have something like #elem:hover { transform: add translateY(5px); }. Unfortunately, such a thing doesn't exist yet. Commented Oct 9, 2015 at 20:38
  • 1
    Since Chrome 104 there are individual transform properties such as scale: 1.5; rotate: 45deg; translate: -50%; etc. But, as of 17.03.2023 they have only 88% support on CanIUse Commented Mar 17, 2023 at 8:15

5 Answers 5

10

CSS custom properties aka CSS variables are the only answer to this (outside of using Javascript).

To add to a previous value:

div {
  --translateX: 140;
  --trans: calc(var(--translateX) * 1px);
  transform: translateX(var(--trans)) scale(1.5);
}

div:hover {
  
 --translatemore: calc(var(--translateX) + 25);
 --trans: calc(var(--translatemore) * 1px);
}

div {
  transition: .2s transform;
  width: 50px;
  height: 50px;
  background: salmon;
}

All browsers now support the ability to set transform properties individually.

Sign up to request clarification or add additional context in comments.

Comments

4

Use the WebKitCSSMatrix object or MozCSSMatrix (I think...) to set new values trough the original object without knowing the initial transform.

http://jsfiddle.net/Cx9hH/

In this case I have an initial translate of 100px on witch I add an extra 100px:

box.style.webkitTransform = matrix.translate(100, 0);

1 Comment

Nice, this doesn't cover all the cases but +upvote for being potentially useful for somebody.
1

You just have to create another div or span wrapper and set transform to that intead

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
@GhostOps this is a legit answer dude.
0

CSS variables! https://jsfiddle.net/91q0s5h0/4/

div {
  --translateX: 10px;
  transform: translateX(var(--translateX)) scale(1.5);
}

div:hover {
  --translateX: 5px;
}

2 Comments

as far as I can tell this does not work, it replaces the 10px with the 5px instead of adding onto it.
Also, these variables are still set in stone, and won't change dynamically when changing the translation through javascript.
-1

You could use nested elements, and increase the translation on each on interdependently for each variable.

Although, that would get ugly pretty quick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.