2

CSS beginner here

img {
    top: 0;
    transition: all 0.5s ease-out;
}
img:hover {
    position: relative;
    top: -5px;
}

On hover, this transition works fine. But when you mouse away again there is no effect, the 'reverse' transition is instant. How to achieve 0.5s on reverse transition too?

2
  • that should work. Might be something else breaking it. Commented Nov 10, 2015 at 19:34
  • Thank you for your answer. Will look further into it. Could be classes breaking it Commented Nov 10, 2015 at 19:37

3 Answers 3

4

Simply move position: relative from the :hover rule to the img rule. When not hovered, the img element isn't being positioned relatively. The top property doesn't do anything on a statically positioned element.

img {
    position: relative;
    top: 0;
    transition: all 0.5s ease-out;
}
img:hover {
    top: -5px;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for answering. This is a solution to my problem! :)
Sorry, new to this site. Done. Thanks again.
0
img {
    position: relative;
    top: 0;
    transition: all 0.5s ease-out;
}
img:hover {
    top: -5px;
}

Comments

-2

This issue has been corrected. Please take a look.

img {
  width: 136px;
  height: 23px;
  -webkit-transition: width 2s;
  /* For Safari 3.1 to 6.0 */
  transition: width 2s;
}
img:hover {
  width: 300px;
}
<div style="background-color:red;">
  <img src="http://jsfiddle.net/img/logo.png">
</div>

View on JSFiddle

4 Comments

How does it relates to question?
I have just set a jsfiddle link to help with demo.
Your answer should contain all relevant code and an explanation as to what your solution is. Don't put a link to an offsite resource in the comments as part of it. Then there's also the issue of how your code is different from that in question.
1. I understand it. The code there is not related to the question. 2. You have to include code as a snippet into you answer.

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.