Page is a not externally linkable
- Code, Content, and Presentation
-- CSS
---- CSS Crib Sheet, cheatsheets, shortcut lists, syntax lists, & others


SuzyUK - 11:03 am on Sep 13, 2004 (gmt 0)


Tips 'n'Tricks: Part 4

2. Shorthand Properties continued..

Background Shorthand

background [w3.org] is a shorthand property for:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

    background {<background-color> <background-image> <background-repeat> <background-attachment> <background-position>;}

    the individual properties are not seperated by commas, all parts are optional and can be used in any order but if one or more of the choices are not declared they are still understood to be there and each "unset" property will be assumed to have been set (implicitly set) to the individual default instead.

    Example:
    p {
    background: #fff url(mybgimage.gif) no-repeat fixed 0% 50%;
    }

    is the equivalent of:
    p {
    background-color: #fff;
    background-image: url(mybgimage.gif);
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: 0% 50%;
    }

    Example 2:
    p {
    background: url{mybgimage.gif);
    }

    is the equivalent of:
    p {
    background-color: transparent;
    background-image: url(mybgimage.gif);
    background-repeat: repeat;
    background-attachment: scroll;
    background-position: 0% 0%;
    }

    Note: setting a background using a shorthand property later in the cascade will override more explicit settings from earlier on. e.g. the second example if used after the first will reset values (back to default) from the first even though they're not declared.

    If you only require to override one part of a setting the has been set using shorthand earlier in the cascade you should use the long hand backgrund property as this will only affect that particular property.

    Example:
    p {
    background: #fff url(mybgimage.gif) no-repeat fixed 0% 50%;
    }

    p.repeat {background-repeat: repeat;}

    <p class="repeat">this now has a repeating background image</p>

    The p with the class of repeat will still be take on the properties specified in the first rule and the second rule just targets the background-repeat property.

    Note: Quotes around background-image names should not be used, they are not required and older browsers will choke (especially on single quotes)

    Note: the path to a background-image should be relative to your stylesheet, watch this while testing with an embedded stylesheet then transferring the CSS to an external file within it's own folder. Relative paths within the stylesheet may need to adjusted or it may be better to use absolute paths depending on your site structure.

    background-position

    normally this needs 2 x values set and these can be percentages, lengths or keywords. The default when no values are set is 0% 0% which equates to top left of the element.

    e.g.
    background-position: 50% 50%;
    background-position: 14px 54px;
    background-position: top center;

    the percentage and length values are applied in the order:
    horizontal(x) ~ from the left,
    vertical(y) ~ from the top

    the keywords ~ the two values have three choices:
    <top ¦ center ¦ bottom> <left ¦ center ¦ right>
    ~ can be applied in any order. e. g.
    top left = left top

    if one value only is given:
    percentages and lengths will take it that the horizontal (from the left) position has been set and the vertical (from the top) will default to 50% (note: not the 0% original default!)

    keywords
    if either top, right, bottom, left, center is set the other will default to center (again not the 0% original).

    You can use combinations of keyword, length and percentage values
    Examples: {50% 20px} or {center 20px} or {center 10%}.

    Note: when using combinations of keyword and non-keyword values, 'left' and 'right' can only be used as the first value, and 'top' and 'bottom' can only be used as the second value, keeping the {x, y} order in place as required by the length / percentage values.

    ... more to follow ....


    Thread source:: http://www.webmasterworld.com/css/4514.htm
    Brought to you by WebmasterWorld: http://www.webmasterworld.com