Move link image 5px up on hover
Asked Answered
A

6

22

How would I go about acheiving an effect similar to that on this site's portfolio page Solid Giant, with CSS and HTML?

I had thought that just putting something like this would work:

a img{
    margin-top: 5px;
}

a img:hover{
    margin-top: 0px;
}

But it did not work, even if I put the :hover on the link instead of the img. I scoured his code and css but I could not for the life of me figure this out. Help please :)

Arium answered 27/3, 2011 at 11:52 Comment(0)
A
43

position: relative would work:

a img:hover{ position: relative; 
             top: -5px;} 

note that position: relative reserves the space in the document flow as if the element were not moved. But I think in this case, that is not an issue.

Ampereturn answered 27/3, 2011 at 11:54 Comment(3)
+1 for the information on position: relative. Didn't know that. – Havre
add padding-bottom: 5px to get rid of 'flicker' – Ashwin
You should definitely go with the translate solution, as it is hardware-accelerated via the GPU. – Patrica
K
18

Also see translate():

http://www.w3schools.com/css/css3_2dtransforms.asp

img:hover {
    -moz-transform: translate(-2px, -2px);
    -ms-transform: translate(-2px, -2px);
    -o-transform: translate(-2px, -2px);
    -webkit-transform: translate(-2px, -2px);
    transform: translate(-2px, -2px)
}

See a similar working example:
http://jsfiddle.net/rimian/7aPvS/1/

Katharynkathe answered 10/3, 2014 at 10:50 Comment(0)
K
0

You could also use CSS/HTML5 animations: http://slides.html5rocks.com/#css-animation

you could also wrap it in another parentdiv that has position:relative set:

<div class="parent">
  <img class="image" />
</div>

.parent { 
    position:relative; 
}
.image { 
    position:absolute; 
    top:0px; 
    left:0px; 
}
.image:hover { 
    top:-15px; 
}
Klara answered 27/3, 2011 at 11:59 Comment(0)
E
-1

you can use id

in HTMLπŸ‘‡

<div id="move"> </div>

in CSSπŸ‘‡

#move {
position: relative;
top: 0;
transition: top ease 1s;
}
#move:hover {
top: -5px;
}

note: transition didn't work when I tried and if you don't use js in your code there's really no point in using id.

Exercise answered 21/8, 2021 at 5:26 Comment(0)
T
-2

Give the image an id:

<img id="imgHover" src="" />


 #imgHover:hover { margin-top: -5px; }
Tolliver answered 27/3, 2011 at 12:2 Comment(0)
P
-2

Make sure you have this in your html so IE knows how to work properly

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Professor answered 6/1, 2012 at 23:29 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.