Results 1 to 6 of 6

Thread: preference or reason

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default preference or reason

    Is specifying background-image: or -color: better then just using background: and specifying in here? I found background with the -image a while back didn't work on one browser i had (forget which one). Didn't know if there was a preference going towards being more specific and if that helped when testing on different browsers.

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    background is better supported than background-image.

  3. The Following User Says Thank You to Snookerman For This Useful Post:

    bluewalrus (12-28-2008)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    oo okay thanks

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    What? background is just shorthand for background-{image,color,position,repeat}. One should not be supported without the other.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Since css cascades, at least in some browsers (perhaps all), regardless of whether or not all are specified, a later declaration of background will override earlier ones for background-color, -image, -position, -repeat.

    So for example:

    Code:
    div {
    background-image: url(some.gif);
    }
    
    div {
    background: #f00;
    }
    will result in a red background, no image.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    I had this question myself a while ago and did some researching and most of the articles that said something about the difference, said that background is better supported. Now I suspect this might not be the case with modern browsers and that the two are equally supported and background is just shorthand as you say. I don't remember which sources I found since this was some time ago and this was the only article I could quickly google myself to that talks about the support:
    http://htmlhelp.com/reference/css/color-background/background-image.html

    Like I said, I don't think support is a big issue (if any at all), however, there is one thing that should be kept in mind: when using the shorhand background you set all the value to default (white none repeat scroll 0% 0%). So if you use for example background: blue; it will actually mean background: blue none repeat scroll 0% 0%;.

    This is important to think about, e.g. say you want all div containers on you page to have a green background color so you have this piece of code:
    Code:
    div {
    background-color: green;
    }
    Then say you want to give a nested container a background image:
    Code:
    div div#special {
    background: url(foo.jpg) no-repeat;
    }
    Since you don't mention the color for the div#special, this will be set to default (white).
    If you would use this:
    Code:
    div div#special {
    background-image: url(foo.jpg);
    background-repeat: no-repeat;
    }
    the div#special would have a green background color because of the first rule.
    You can read more about this here:
    http://www.w3.org/TR/CSS21/colors.html

    Edit: I see John beat me to it, I knew I should have refreshed before replying

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •