tags:

views:

108

answers:

2

This is my image:- http://www.flickr.com/photos/50525207@N02/5064283850/

CSS n html

Now the problem:-

When I hover over links the same image appears when I want different parts of the image to appear. Also the other links shift when I move mouse over one link. What I want is this:-

http://www.flickr.com/photos/50525207@N02/5063686801/

What I want is a grey colored image to appear in the background when the mouse is hovered over "Link1". A green colored image is to appear when the mouse is hovered over "Link2" and so on. What am I doing wrong? I have been trying to make it work since yesterday but in vain.

PS: erm, that's not the actual image BTW. I don't want colors in the background. It's going to be images of products. Oh, and I want that grey image to appear when no link is hovered over. How to do that?

[EDIT]

I added the following in the CSS:-

.sprite Div
{
    width: 728px;
    height: 243px;
}

.sprite a
{
    width: 728px;
    height: 243px;
}

In the HTML IK included the links inside of Div so the height gets fixed:-

<div id="SpriteDiv" class="sprite">
    My links here...
</div>
+2  A: 

First, you should set a size of your anchor element without hover, this is what's causing your other links to shift around (the dimensions shouldn't be defined on a:hover):

.sprite a {
  display: block;
  width: 728px; 
  height: 243px; 
}

Next, your image background is assigned to the anchor elements, not the span, so you need to define those positions with the selector like this:

.sp_ID1 a {
  background-position: 0px 0px; 
} 
Andrew Vit
ok lemme try this
Serenity
ok same image problem has got resolved..thnx..but the links are still shifting..changed the code like u said..even tried adding a div..plz check the added details
Serenity
The display:block should be on the non-hover selector too. Only the background-image stuff should be on the a:hover. See updated.
Andrew Vit
Plz check "Edit 2" above..I removed the div and changed code..Now the links have all moved 243 px apart =/ whats wrong now ? Why isnt the height getting fixed ?
Serenity
hello??????????
Serenity
@Happy Soul: Take it easy, sometimes it takes a day to get an answer.
dutt
Only 20 minutes between your "Plz check" and the "hello". That would make me bail out trying to help.
Arjan
Agreed, Arjan. I have to give up on this one: I'm here to show the way and explain, not spell things out and provide finished work. :(
Andrew Vit
omg..I never asked no one to type the complete code for me! wow..lol..I only asked what the problem was but anyway thnx for all the help
Serenity
@Happy, that's not how some folks feel about it. Please don't ignore that feedback.
Arjan
well...whatever
Serenity
+1  A: 

Corrected according to your comment:

Originally I put the gray background on .container, but that causes strange behavior on Chrome, so I added .sp_ID0

<style type="text/css">
    .sprite { display: block; margin-bottom: 5px; }
    .container, .sp_ID0, .sprite div { width: 600px; height: 203px; }
    .sp_ID0, .sprite:hover div {
        background-image: url(http://farm5.static.flickr.com/4106/5064283850_fc6b5fac15_b.jpg);
        background-repeat: no-repeat;
    }
    .container  { position:relative; }
    .sp_ID0 { z-index: -2; }
    .sprite div { display: none; z-index: -1; }
    .sp_ID0, .sprite:hover div { position: absolute; top: 0px; left: 0px; display: block; }
    .sp_ID1 div { background-position: 0px -203px; }
    .sp_ID2 div { background-position: 0px -406px; }
    .sp_ID3 div { background-position: 0px -609px; }
    .sp_ID4 div { background-position: 0px -812px; }
    .sp_ID5 div { background-position: 0px -203px; }
    .sp_ID6 div { background-position: 0px -406px; }
</style>

<div class="container">
<div class="sp_ID0">&nbsp;</div>
    <a href=# class="sprite sp_ID1">Link1<div>&nbsp;</div></a>
    <a href=# class="sprite sp_ID2">Link2<div>&nbsp;</div></a>
    <a href=# class="sprite sp_ID3">Link3<div>&nbsp;</div></a>
    <a href=# class="sprite sp_ID4">Link4<div>&nbsp;</div></a>
    <a href=# class="sprite sp_ID5">Link5<div>&nbsp;</div></a>
    <a href=# class="sprite sp_ID6">Link6<div>&nbsp;</div></a>
</div>

Old solution.

y34h
no no..that's what my code is doing too now :/ I don't want the links far apart..want them together like this http://www.flickr.com/photos/50525207@N02/5063686801/ ...with that image background..the image is actually a banner and that banner changes colors(images) when links are hovered over..like when one hovers on "Link1" grey colored image part should be displayed..."Link 2" hover should display Green colored image part and so on...How do I implement that now ?
Serenity
Note that `:hover` only works on anchor elements in IE6: I've always avoided putting hover on other elements for that reason. But, if you don't need to support it, then this way is fine.
Andrew Vit
@y34h :: Thanks a ton! :D ... SO is worth it because of people like you..really appreciate the help :)
Serenity