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

CSS3 Backgrounds

CSS3 Backgrounds


CSS3 Backgrounds

CSS3 includes several new background properties, providing greater control over background elements.

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

You will also learn how to use multiple background images.


Browser support

The numbers in the table indicate the first browser version that supports this property.

A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.

property
background-image
(with multiple backgrounds) 4.0 9.0 3.6 3.1 11.5
background-size 4.0
1.0 -webkit- 9.0 4.0
3.6 -moz- 4.1
3.0 -webkit- 10.5
10.0 -o-
background-origin 1.0 9.0 4.0 3.0 10.5
background-clip 4.0 9.0 4.0 3.0 10.5

The CSS3 background-image Property

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

Different background images are separated by commas, and the image displayed at the top of the stack 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.

In CSS3 you can specify a background image, letting you specify its size again in different environments. You can specify the size in pixels or percentages.

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

Example 1

Reset the background image:

div

{

background:

url

(

img

_

flwr

.

gif

)

;

background-size:

80

px

60

px

;

background-repeat:

no-repeat

;

}

Try it »

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 »


The CSS3 background-origin Property

The background-origin property specifies the positioning area of the background image.

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

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 an element.

Example

Set two background images on the body element:

body

{

background-image:

url

(

img

_

flwr

.

gif

),

url

(

img

_

tree

.

gif

)

;

}

Try it »


The CSS3 background-clip Property

In CSS3 the background-clip property determines the area in which the background is painted.

Example

#example1

{

border:

10

px

dotted

black

;

padding:

35

px

;

background:

yellow

;

background-clip:

content-box

;

}

Try it »


New Background Properties

Order Description CSS
background-clip Specifies the painting area of the background. 3
background-origin Specifies the positioning area of the background image. 3
background-size Specifies the size of the background image. 3