CSS3 Background


CSS3 background

CSS3 includes several new background properties that provide greater control over background elements.

In this chapter you will learn about the following background properties:

  • background-image

  • background-size

  • background-origin

  • background-clip

You will also learn how to use multiple background images.


Browser support

The numbers in the table represent the first browser version number that supports the property.

The number immediately preceding -webkit-, -ms- or -moz- is the version number of the first browser that supports the prefixed attribute.

Attributesimage.pngimage.pngimage.pngimage.pngimage.png
background-image
(with multiple backgrounds)
4.09.03.63.111.5
background-size4.0
1.0 -webkit-
9.04.0
3.6 -moz-
4.1
3.0 -webkit-
10.5
10.0 -o-
background-origin1.09.04.03.010.5
background-clip4.09.04.03.010.5

CSS3 background-image property

In CSS3, you can add background images through the background-image property.

Different background images and images are separated by commas, and the one displayed at the top of all images is the first one.

Example

#example1 { 
    background-image: url(img_flwr.gif), url(paper.gif); 
    background-position: right bottom, left top; 
    background-repeat: no-repeat, repeat; 
}

Try It! 


You can set multiple different properties for different images

Example

#example1 {
    background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
}

Try It! 


CSS3 background-size property

background-size specifies the size of the background image. Before CSS3, the background image size was determined by the actual size of the image.

The background image can be specified in CSS3, let us re-specify the size of the background image in different environments. You can specify pixel or percentage size.

The size you specify is a percentage of the width and height of the parent element.

OperaSafariChromeFirefoxInternet Explorer

Example 1

Reset the background image:

div
{
    background:url(img_flwr.gif);
    background-size:80px 60px;
    background-repeat:no-repeat;
}

Try It! 

OperaSafariChromeFirefoxInternet Explorer

Example 2

Stretch the background image to completely fill the content area:

div
{
    background:url(img_flwr.gif);
    background-size:100% 100%;
    background-repeat:no-repeat;
}

Try It! 


CSS3 background-origin property

The background-origin property specifies the location area for the background image.

Background images can be placed in the content-box, padding-box, and border-box areas.



OperaSafariChromeFirefoxInternet Explorer

Example

Position the background image in the content-box:

div
{
    background:url(img_flwr.gif);
    background-repeat:no-repeat;
    background-size:100% 100%;
    background-origin:content-box;
}

Try It! 


CSS3 multiple background images


CSS3 allows you to add multiple background images to elements.

OperaSafariChromeFirefoxInternet Explorer

Example

Set two background images in the body element:

body{ 
    background-image:url(img_flwr.gif),url(img_tree.gif);
}

Try It! 


CSS3 background-clip property

The background clipping property of background-clip in CSS3 starts drawing from the specified position.

Example

#example1 { 
    border: 10px dotted black; 
    padding: 35px; 
    background: yellow; 
    background-clip: content-box; 
}

Try It! 



New background property

PropertyDescriptionCSS
background-clipSpecifies the drawing area for the background.3
background-originSpecifies the positioning area for the background image.3
background-sizeSpecifies the size of the background image.3