Forum Moderators: not2easy

Message Too Old, No Replies

CSS hover image popup help

         

andrewsmd

1:59 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a link and I would like to display an image on hover.
lets say we have this link
<a href = "somepage.com">Some Page</a>
and an image image.jpg

I want to display image.jpg when your mouse hovers over the link to somepage.com.
Anyone know the css to do this. Thanks,

SuzyUK

2:52 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you already know the code ;)

in your previous post [webmasterworld.com] - simply replace the span with your image in the HTML and then replace "span" in the CSS with "img"

or put the image inside the span and leave the CSS as it is

an image is an inline element and can be nested inside a link

[edited by: SuzyUK at 3:05 pm (utc) on Sep. 4, 2008]

D_Blackwell

2:59 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding the image on a:hover is easy enough.

a:hover {
background-image: url(aaa.jpg);
}

This works in all the good stuff. Does not work in IE, or at least IE6. For that you must declare a background-color: or the image won't work.

Easiest fix is to simply color match to the image, which you would probably do anyway.

a:hover {
background: #fff url(aaa.jpg);
}

The trick then is how to get rid of the text. Simply changing color: to match and blend in with the image MAY work and would be a very quick solution.

Sample code provided. Depending upon the image used, this would work very well - or horribly - and we will have to get into some <span>s - hiding and showing them. It all depends upon the image and the ease of simply changing the color text to hide it when the image shows.

But, it you are going to use an image on the a:hover, why not just use on image on the <a> and take dealing with the text out of the equation.

(You may want to preload the :hover image so that it pops into place instantly - that there isn't a hesitation between the over and the rendering of the image.)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<style>
a:hover {
background: #fff url(aaa.jpg); background-color; color: #000;
}
</style>
</head>
<body>
<div>
<a href="http://www.example.com">LINK to some page</a>
<div>
<!--##########
I have a link and I would like to display an image on hover.
lets say we have this link
<a href = "somepage.com">Some Page</a>
and an image image.jpg

I want to display image.jpg when your mouse hovers over the link to somepage.com.
Anyone know the css to do this. Thanks,
-->
</body>
</html>

andrewsmd

5:10 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The thing is I have multiple images I want to display only on specific links. ie. <a href "page1.com">"displays image1.jpg"</a><a href "page2.com">"displays image2.jpg"</a>. I agree that IE sucks, but it still has like 70% of the market share so it is retarded to design a web page that does not work with 70% of the people who would be looking at it.

andrewsmd

5:32 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SuzyUK, that works in FF but once again, not in IE. Is there any way to make this cross browser compatible? Thanks here is the css that works in FF.
a.info {
position: relative;
z-index: 24;
color: white;
font-family: cursive;
border: none;

}

a.info:hover {
z-index: 25;
color: #FFFF00;
text-decoration: none;
}

a.info img {
display: none;
border: none;

}
.spanClass{
padding-left: .5em;
padding-top: .5em;
font: 17px bold;
padding-bottom: .5em;
padding-right: .5em;
font-family: cursive;
border: none;
}

a.info:hover img { /*the span will display just on :hover state*/
display: block;
position: absolute;
bottom: 2em;
left: 2em;
color: black;
border: none;

}
<a class = "info" href = "http://www.bulletformyvalentine1.com/news.php" target = "blank">Bullet For My Valentine
<img class = "spanClass" src = "bullet.jpg"></img>

</a>

SuzyUK

5:55 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



same bug [webmasterworld.com]as D_Blackwell tells you about above, it can be fixed by adding something else to a:hover.. see that link for the list of possibilities.. very easy to workaround for IE ;)

however there is another bug which that reminded me on which can be triggered when using the display:none/block method so try this code instead..

a.info {
position: relative;
color: #00f;
font-family: cursive;
border: none;

}

a.info:hover {
color: #FFFF00;
text-decoration: none;
background: #fff; /* added to get around IE bug.. there are more choices if a background: color doesn't suit your site */
}

a.info img {
border: none;
margin-left: 10px; /*adjusting this will control the space betwwen the image and the text */
position: absolute;
left: -9999px; /* hide the "popup" offscreen*/
}

a.info:hover img {
left: auto; /* bring it back on on hover */
}

andrewsmd

6:28 pm on Sep 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Works in both now for that. Thanks SuszyUK. Just an FYI I found a work around with the background thing. Basically you can set it to an image that doesn't exist and it works and shows the image as the background (which doesn't exist) and therefore you get no background.
Here is the css
a.info {
position: relative;
color: white;
font-family: cursive;
border: none;

}

a.info:hover {

text-decoration: none;
background: url("fileDoesNotExist.jpg");
z-index: 6;
}

a.info img {
border: none;
margin-left: 10px; /*adjusting this will control the space betwwen the image and the text */
position: absolute;
left: -9999px; /* hide the "popup" offscreen*/
}

a.info:hover img {
left: auto; /* bring it back on on hover */
top: -16em;
}
and here is the HTML
<a class = "info" href = "http://www.example.com" target = "blank" title = "example's Website">Coldplay<img src = "example.jpg"></img></a>
Thanks to everyone for their help

[edited by: jatar_k at 12:51 pm (utc) on Sep. 5, 2008]
[edit reason] please use example.com [/edit]

andrewsmd

1:10 am on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whooohoo another problem. Does anyone know how to decide where to put the picture so it won't go off the page? Thanks,