css background-position not working

49,385

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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;
}
Share:
49,385
qwerty
Author by

qwerty

Updated on April 11, 2020

Comments

  • qwerty
    qwerty about 4 years

    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

  • Amit G
    Amit G about 13 years
    Ditto, couldn't find an issue. I'd try ^.
  • qwerty
    qwerty about 13 years
    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)
  • John Green
    John Green about 13 years
    Yeah, you'll have to post an example. I think you'd have an answer within 2 minutes if you did.
  • John Green
    John Green about 13 years
    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.
  • Amit G
    Amit G about 13 years
    Ah ok, IE7 supports :hover, it flounders on :active though, quirksmode.org/css/contents.html#t16.
  • John Green
    John Green about 13 years
    Yeah, he didn't mention in the original code that he had '#buttons .button'. Changed the face of it entirely. : )
  • qwerty
    qwerty about 13 years
    @john, that worked. thanks. as mentioned before, being sick every thing is hazy O.o
  • Admin
    Admin over 11 years
    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!
  • Admin
    Admin about 11 years
    For clarification, its the lack of "px" that is breaking it
  • Tony Tambe
    Tony Tambe over 9 years
    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!