spacer

Backgrounds

 
spacer
The CSS properties in the table below can be used to define background colors and images on various components on your web page. These properties are typically applied to block elements but you can also apply them to some form elements. These include the following:



CSS Properties List 4 - Backgrounds
Property NameSome Possible ValuesWhat It Does
background(All background properties at once)Sets background
background-color(Any color)Background color
background-imageurl('example.jpg')Background image
background-repeatrepeat-x | repeat-y | no-repeatTile background?
background-positiontop left | 50% 100%Position?
background-attachmentfixed | scrollFixed or scroll?


NOTES:
  • Background ~ This is CSS shorthand property that allows you to set five different background related properties at once. The shorthand syntax for background is written as follows:

    background: color image repeat position attachment;

    In the above:

    1. color = Any named color or hexadecimal color code.
      Possible values: red | green | navy | #ffffcc | #000000
    2. image = Path to any background image file.
      Syntax example: url("background.jpg")
    3. repeat = A value that defines how the background image is repeated.
      Possible values: repeat-x | repeat-y | no-repeat
    4. position = A pair of values that set the background image's position.
      Possible values: left top | center center | right bottom
    5. attachment = Any value(s) applicable to background-attachment.
      Possible values: fixed | scroll
    Hence, the background property is declared and then followed by a space-separated list of values pertaining to any one or more of the above.

    Here's an example:

    background: orange url("test.jpg") no-repeat center center fixed;

    In the above example, the background color is orange and the background image is 'test.jpg'. The background image is only displayed once in the center of the web page and is fixed in place such that when the user scrolls, it always remains within the browser viewport.

    All the values set using the background shorthand property can be set individually using the following CSS properties:

    • Background-color ~ This CSS property defines the background color. Any named color or hexadecimal color code may be used, e.g., red, green, blue, #ffffcc, etc. (Please see font color for more about what colors you can use for any HTML or CSS code.)

      Example: background-color: silver;
    • Background-image ~ This CSS property defines the background image. The syntax to use is url("path to any image file") where path to any image file equals any relative or absolute path to the image file you wish to display as a background.

      Examples:

      background-image: url("mycoolpic.gif");
      background-image: url("pics/mycoolpic.gif");
      background-image: url("http://www.example.com/mycoolpic.gif");
    • Background-repeat ~ By default, background images (when declared) are repeated horizontally and vertically until the entire element is filled. You can use the background-repeat property to control this behaviour. The value can be any one of the following:

      • repeat-x - The background image is only repeated horizontally.
      • repeat-y - The background image is only repeated vertically.
      • no-repeat - The background image is only displayed once.
      Example: background-repeat: repeat-x;
    • Background-position ~ If a background image is declared and it is set to repeat only horizontally or vertically or not at all (see background-repeat) then you can use background-position to define exactly where the background image is displayed on your web page.

      Typically, two values are used — the first defining the horizontal position and the second defining the vertical position. Some of the values you may use are named positions, percentages or pixels (px).

      NAMED POSITIONS

      Named positions for horizontal positioning are: left | center | right
      Named positions for vertical positioning are: top | center | bottom


      PERCENTAGES AND PIXELS

      When using percentages or pixels, the first value defines the horizontal distance from the left border while the second value defines the vertical distance from the top border.

      Examples:

      background-position: 0% 50%;
      (The background image is displayed flush with the left border and in the vertical center.)

      background-position: 50% 100%;
      (The background image is displayed in the horizontal center and flush with the bottom border.)

      background-position: 20px 40px;
      (The background image is displayed 20 pixels from the left border and 40 pixels from the top border.)

      background-position: right bottom;
      (The background image is displayed in the bottom right corner.)

    • Background-attachment ~ If you're starting to become concerned about cross-browser compatibility and you've discovered that using bgproperties="fixed" in the <body> tag doesn't work in Firefox (or any Gecko-based browser for that matter) then here comes the CSS property background-attachment to the rescue. This can be used to make the background image remain fixed in place while the rest of the web page content scrolls over it.

      Possible values are: fixed | scroll

      Example:

      <body style="background-attachment: fixed;"   background="example.jpg">
MORE EXAMPLES:

Below is an example of an inline style applied to a paragraph tag using background shorthand property described above (height and padding are also defined for demonstrational purposes):


Example 1 - INLINE STYLE

<p style='background: orange url("sunset.jpg") no-repeat 0px 20px; height: 100px; padding: 20px;'>It was a beautiful sunset...</p>


Note that in the inline style above, we're obliged to nest one set of quotation marks within another to declare the path to the background image within the style attribute. If we used double quotation marks for both style="..." and url("..."), the web page would get messed up because the style attribute would end prematurely. Hence we'll opt to use single quotation marks for the style attribute [ style=' ' ] and double quotation marks for the path to the background image [ url(" ") ].

The same inline style above could be declared as an embedded stylesheet in the document head:


Example 2 - EMBEDDED STYLE SHEET

<style type="text/css">
<!--
p {
   background: orange url("sunset.jpg") no-repeat 0px 20px;
   height: 100px;
   padding: 20px;

}
-->
</style>



...and in the document body, you would simply put this:


<p>It was a beautiful sunset...</p>



Both Example 1 and Example 2 will produce the following effect:

It was a beautiful sunset...

And the lowdown on what's happening here is:
  • The height of the paragraph is 100 pixels.
  • The padding on all sides of the paragraph is 20 pixels.
  • The background color is orange.
  • The background image is "sunset.jpg" and is:
    • Not repeated (only displayed once).
    • Set flush with the left border (0 pixels).
    • Set 20 pixels from the top border.

*   *   *


Next we'll cover how to apply relative and absolute positioning to your web page elements...


MORE CSS PROPERTIES:
SEE ALSO: