CSS3 Background Size, Origin, Clip

CSS3 contains several new background properties,
which allow greater control of the background element.
In this chapter you will learn about the following background properties:

  • background-size
  • background-origin 
  • background-clip
You will also learn how to use multiple background images.


Browser Support

  • Firefox 3.6 and earlier does not support the background-origin property, and requires the prefix -moz- to support the background-size property.
  • Safari 4 requires the prefix -webkit- to support the new background properties.
  • Internet Explorer 9, Firefox 4, Chrome, Safari 5 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 (resize a background image)
div
{
background:url(img_flwr.gif);
-moz-background-size:80px 60px; /* Firefox 3.6 */
background-size:80px 60px;
background-repeat:no-repeat;
}


Example (stretch the background image to completely fill the content area)
div
{
background:url(img_flwr.gif);
-moz-background-size:100% 100%; /* Firefox 3.6 */
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%;
-webkit-background-origin:content-box; /* Safari */
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-image:url(img_flwr.gif),url(img_tree.gif);
}


CSS3 background-clip Property

The background-clip property is supported IE9+, Firefox 4+, Opera, and Chrome.
Safari supports an alternative, the -webkit-background-clip property. 

Syntax
background-clip: border-box|padding-box|content-box; 

Example (specify the painting area of the background) 

div
{
background-color:yellow;
background-clip:content-box;
-webkit-background-clip:content-box; /* Safari */
}

Leave a Reply