css background-position not working
Asked Answered
P

7

19

I have 3 a tags disguised as "roll over buttons".

<div id="buttons">
    <a class='button' id='but1' href=''></a>
    <a class='button' id='but2' href=''></a>
    <a class='button' id='but3' href=''></a>
</div>

Each button is getting its initial image from the CSS as follows:

.button{
    background:url(theimage.jpg);
    width;height; etc...
}

Now, when i try to assign initial background position for each specific element as such:

#but1{
    background-position:0 0;
}
#but1:hover{
    background-position:0 -50px;
}

#but2{
    background-position:0 -100px;
}
#but2:hover{
    background-position:0 -150px;
}

#but3{
    background-position:0 -200px;
}
#but3:hover{
    background-position:0 -250px;
}

The Issue: each button defaults to position 0 0

Note that the hover positions work as expected.

I'm kind of sick right now so this is probably an oversight but I've been stairing at this for an hour now and can't figure it out.

Any thoughts?

Thanks

EDIT pastebin love http://pastebin.com/SeZkjmHa

Privatdocent answered 18/5, 2011 at 0:35 Comment(3)
It's been a while since I've used sprites...but shouldn't the values be positive and not negative?Squatter
@jcoc611 Since I'm going from bottom up I'm using negativesPrivatdocent
Right...but I still think it's worth trying...also...a link to a website or jsfiddle would be awesome.Squatter
R
43

I'm not reproducing your issue. Which browser?

Initial thought (without seeing an error case) is to change your initial background definition from a full 'background:' to a background-image declaration:

.button{
  background-image:url(theimage.jpg);
  width;height; etc...
}

By setting background, which is a container for background-position, some browsers may have issues with specificity issues there.

Rosabella answered 18/5, 2011 at 0:42 Comment(8)
Ditto, couldn't find an issue. I'd try ^.Splasher
Thanks for the reply. Tried that but no joy. Driving me nuts because I'm not getting any CSS errors reported by the web dev plugin (firefox)Privatdocent
Yeah, you'll have to post an example. I think you'd have an answer within 2 minutes if you did.Rosabella
This is exactly the specificity issue I was talking about before. Change 'background' in the #buttons .button to 'background-image:url(blah);background-repeat:no repeat' and it works.Rosabella
Yeah, he didn't mention in the original code that he had '#buttons .button'. Changed the face of it entirely. : )Rosabella
@john, that worked. thanks. as mentioned before, being sick every thing is hazy O.oPrivatdocent
Ran into the same issue with chrome. background:URL(image.png) was defined inline on the a tag. It was somehow overriding the a:hover. Changing it to background-image worked perfectly!Mobcap
The regular background:url worked perfectly for me for 2 years. All of a sudden it was broken. Changing it to background-image did the trick!Puffy
D
12

Split up the "background" shorthand property.

If you omit one of the entries in a shorthand property, the omitted entry is reset to the default rather than simply being left alone.

So what's happening is more-or-less the equivalent of this:

#someElement {
    background-position:0 -100px;
    background:url(image.png) no-repeat;
    /* ^--- omitted background-position gets reset to the default */
}

Break the shorthand into different entries:

#someElement {
    background-image:url(image.png);
    background-repeat:no-repeat;
}

EDIT: In your code, the background-position values come after the background values... But I'm pretty sure that the #buttons .button selector is more specific than the #retain-our-firm and similar selectors, meaning that its rule takes precedence over the others even though the others come after.

Dashtikavir answered 18/5, 2011 at 1:36 Comment(0)
F
5

I know this was asked ages ago, but I think I have the fix.

I had the exact same problem, the positioning was working in Chrome etc but not Firefox.

Took so long to figure out the silly answer,

background-position: 7 4;

Will work in chrome, but not Firefox..

background-position: 7px 4px;

Will work in both.

Forgery answered 11/4, 2013 at 5:25 Comment(1)
For clarification, its the lack of "px" that is breaking itForgery
H
2

If you came here because background-position-<axis> is not working and your problem is nothing to do with incomplete background shorthand properties, double check you're not using the contain sizing property:

background-size: contain;

This doesn't seem to work in conjunction with background-position-<aixis> on Chrome. Haven't tested on other browsers.

Hilversum answered 21/4, 2023 at 12:56 Comment(1)
I have the very same issue with cover: background-size: cover; makes background-position-y not working on opera (112.0.5197.39).Baun
S
1

Works if you split up the background properties, http://jsfiddle.net/kTYyU/.

#buttons .button{
    display:block;
    position:relative;
    float:left;
    width:250px;
    height:80px;
    padding-left:20px;
    background-image:url(http://www.websitesforlawyers.us/images/valid_xhtml_code_icon.png);
    background-repeat:no-repeat;
}
Splasher answered 18/5, 2011 at 1:6 Comment(0)
G
0

Have you considered getting it into <ul> <li> </li> <ul>? It is much easier this way. like:

<ul class="buttons">
    <li  href=''></a>
    <li  href=''></a>
    <li  href=''></a>
</ul>

for the css part

.buttons li{
    background:url(theimage.jpg);
    width;height; etc...
}

.buttons li:hover{
    background-position:0 0;
 }

see if that works ;)

Guardhouse answered 18/5, 2011 at 0:58 Comment(1)
Ah ok, IE7 supports :hover, it flounders on :active though, quirksmode.org/css/contents.html#t16.Splasher
H
0

I think the problem stems from declaring an incomplete shorthand property on your .button class.

A fixed example can be seen at jsFiddle: http://jsfiddle.net/rRVBL/

Hallux answered 18/5, 2011 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.