2015-08-08

Normal

0

false

false

false

EN-US

X-NONE

X-NONE

CSS Background

CSS background properties are used to define the background effects of an element.

CSS properties used for background effects:

background-color

background-image

background-repeat

background-attachment

background-position

Background Color
The background-colorproperty specifies the background color of an element.
The background color of a page is set like this:

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

background-color: #b0c4de;

}

</style>

</head>

<body>

<h1>My CSS web page!</h1>

<p>Hello world! This is a W3Schools.com example.</p>

</body>

</html>

With CSS, a color is most often specified by:

a HEX value - like "#ff0000"

an RGB value - like "rgb(255,0,0)"

a color name - like "red"

In the example below, the <h1>, <p>, and <div> elements have different background colors:

Example

<!DOCTYPE html>

<html>

<head>

<style>

h1 {

background-color: #6495ed;

}

p {

background-color: #e0ffff;

}

div {

background-color: #b0c4de;

}

</style>

</head>

<body>

<h1>CSS background-color example!</h1>

<div>

This is a text inside a div element.

<p>This paragraph has its own background color.</p>

We are still in the div element.

</div>

</body>

</html>

Background Image

The background-imageproperty specifies an image to use as the background of an element.

By default, the image is repeated so it covers the entire element.

The background image for a page can be set like this:

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

background-image: url("paper.gif");

}

</style>

</head>

<body>

<h1>Hello World!</h1>

</body>

</html>

Below is an example of a bad combination of text and background image. The text is almost not readable:

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

background-image: url("bgdesert.jpg");

}

</style>

</head>

<body>

<h1>Hello World!</h1>

<p>This text is not easy to read on this background image.</p>

</body>

</html>

Background Image - Repeat Horizontally or Vertically

By default, the background-image property repeats an image both horizontally and vertically.

Some images should be repeated only horizontally or vertically, or they will look strange, like this:

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

background-image: url("gradient_bg.png");

}

</style>

</head>

<body>

<h1>Hello World!</h1>

</body>

</html>

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

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

background-image: url("gradient_bg.png");

background-repeat: repeat-x;

}

</style>

</head>

<body>

<h1>Hello World!</h1>

</body>

</html>

Showing the image only once is specified by the background-repeat property:

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

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

background-repeat: no-repeat;

}

</style>

</head>

<body>

<h1>Hello World!</h1>

<p>W3Schools background image example.</p>

<p>The background image is only showing once, but it is disturbing the reader!</p>

</body>

</html>

In the example above, the background image is shown in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much.

The position of the image is specified by the background-position property:

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

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

background-repeat: no-repeat;

background-position: right top;

margin-right: 200px;

}

</style>

</head>

<body>

<h1>Hello World!</h1>

<p>W3Schools background no-repeat, set position example.</p>

<p>Now the background image is only shown once, and positioned away from the text.</p>

<p>In this example we have also added a margin on the right side, so the background image will never disturb the text.</p>

</body>

</html>

Background - Shorthand property

As you can see from the examples above, there are many properties to consider when dealing with backgrounds.

To shorten the code, it is also possible to specify all the properties in one single property. This is called a shorthand property.

The shorthand property for background is simply "background":

Example

<!DOCTYPE html>

<html>

<head>

<style>

body {

background: #ffffff url("img_tree.png") no-repeat right top;

margin-right: 200px;

}

</style>

</head>

<body>

<h1>Hello World!</h1>

<p>Now the background image is only shown once, and it is also positioned away from the text.</p>

<p>In this example we have also added a margin on the right side, so that the background image will not disturb the text.</p>

</body>

</html>

Show more