CSS3 Backgrounds
CSS3 Backgrounds
CSS3 contains several new background properties,
which allow greater control of the background element.
which allow greater control of the background element.
In this chapter you will learn about the following background properties:
- background-size
- background-origin
You will also learn how to use multiple background images.
Browser Support
Property | Browser Support | ||||
---|---|---|---|---|---|
background-size | |||||
background-origin |
Internet Explorer 9+, Firefox, Chrome, Safari, and Opera support the new background properties.
CSS3 The background-size Property
The background-size property specifies the size of the background image.
Before CSS3, the background image size was determined by the actual size of the image. In CSS3 it is possible to specify the size of the background image, which allows us to re-use background images in different contexts.
You can specify the size in pixels or in percentages. If you specify the size as a percentage, the size is relative to the width and height of the parent element.
Example 1
Resize a background image:
div
{
background:url(img_flwr.gif);
background-size:80px 60px;
background-repeat:no-repeat;
}
{
background:url(img_flwr.gif);
background-size:80px 60px;
background-repeat:no-repeat;
}
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;
}
{
background:url(img_flwr.gif);
background-size:100% 100%;
background-repeat:no-repeat;
}
CSS3 The background-origin Property
The background-origin property specifies the positioning area of the background images.
The background image can be placed within the content-box, padding-box, or border-box area.
Example
Position the background image within the content-box:
div
{
background:url(img_flwr.gif);
background-repeat:no-repeat;
background-size:100% 100%;
background-origin:content-box;
}
{
background:url(img_flwr.gif);
background-repeat:no-repeat;
background-size:100% 100%;
background-origin:content-box;
}
CSS3 Multiple Background Images | |
CSS3 allows you to use several background images for an element. |
Example
Set two background images for the body element:
body
{
background:url(img_tree.gif),url(img_flwr.gif);
background-size:100% 100%;
background-repeat:no-repeat;
}
{
background:url(img_tree.gif),url(img_flwr.gif);
background-size:100% 100%;
background-repeat:no-repeat;
}
CSS3 Background Properties
Property | Description | CSS |
---|---|---|
background-clip | Specifies the painting area of the background images | 3 |
background-origin | Specifies the positioning area of the background images | 3 |
background-size | Specifies the size of the background images | 3 |