开发者

css background-position not working

开发者 https://www.devze.com 2023-03-07 02:09 出处:网络
I have 3 a tags disguised as \"roll over buttons\". <div id=\"buttons\"> <a class=\'button\' id=\'but1\' href=\'\'></a>

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 bac开发者_如何学Gokground 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


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.


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.


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.


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;
}


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 ;)


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号