Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Backgrounds

CSS Backgrounds

CSS background properties are used to define the background of HTML elements.

CSS properties define background effects:

Background Color

The background-color property defines the background color of an element.

The background color of the page is set in the body selector:

Example

body {background-color:#b0c4de;}

Try it »

In CSS, color values are usually defined in the following ways:

In the following example, the h1, p, and div elements have different background colors:

Example

h1 {background-color:#6495ed;}

p {background-color:#e0ffff;}

div {background-color:#b0c4de;}

Try it »


Background Image

The background-image property describes the background image of an element.

By default, background images are tiled and repeated to cover the entire element.

Example of setting a page background image:

Example

body {background-image:url('paper.gif');}

Try it »

The following is an example of a poor combination of text and background image. The text is hard to read:

Example

body {background-image:url('bgdesert.jpg');}

Try it »


Background Image - Horizontal or Vertical Tiling

By default, the background-image property tiles the image horizontally or vertically on the page.

Some images look inconsistent when tiled both horizontally and vertically, as shown below: 

Example

body

{

background-image:url('gradient2.png');

}

Try it »

If the image is tiled only horizontally (repeat-x), the page background will look better:

Example

body

{

background-image:url('gradient2.png');

background-repeat:repeat-x;

}

Try it »


Background Image - Setting Position and No Repeat

Remark Prevent the background image from affecting text layout

If you do not want the image to be tiled, you can use the background-repeat property:

Example

body

{

background-image:url('img_tree.png');

background-repeat:no-repeat;

}

Try it »

In the example above, the background image and text are displayed at the same position. To make the page layout more reasonable and avoid affecting text readability, we can change the position of the image.

You can use the background-position property to change the position of the image in the background:

Example

body

{

background-image:url('img_tree.png');

background-repeat:no-repeat;

background-position:right top;

}

Try it »


Background - Shorthand Property

In the examples above, we can see that the page background color is controlled by many properties.

To simplify the code for these properties, we can combine them into a single property.

The shorthand property for background colors is "background":

Example

body {background:#ffffff url('img_tree.png') no-repeat right top;}

Try it »

When using the shorthand property, the order of the property values is:

You do not need to use all of the above properties; you can use them according to the actual needs of the page.

This example uses the CSS introduced earlier; you can view the corresponding example: CSS example


More examples

How to Set a Fixed Background Image
This example demonstrates how to set a fixed background image. The image will not scroll with the rest of the page.


CSS Background Properties

Property Description
background Shorthand property for setting all background properties in a single declaration.
background-attachment Specifies whether a background image is fixed or scrolls with the rest of the page.
background-color Sets the background color of an element.
background-image Sets an image as the background.
background-position Sets the starting position of a background image.
background-repeat Sets whether and how a background image is repeated.