In this chapter you will learn how to add multiple background images to one element.
You will also learn about the following properties:
background-size
background-origin
background-clip
CSS Multiple Backgrounds
CSS allows you to add multiple background images for an element, through the
background-image
property.
The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.
The following example has two background images, the first image is a flower (aligned to the bottom and right) and the second image is a paper background (aligned to the top-left corner):
#example1 {
background-image: url(img_flwr.gif), url(paper.gif);
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;}
background-image: url(img_flwr.gif), url(paper.gif);
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;}
Multiple background images can be specified using either the individual background properties (as above) or the
background
shorthand property.
The following example uses the
background
shorthand property (same result as example above):
#example1 {
background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;}
background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;}
CSS Background Size
The CSS
background-size
property allows you to specify the size of background images.
The size can be specified in lengths, percentages, or by using one of the two keywords: contain or cover.
The following example resizes a background image to much smaller than the original image (using pixels):
#div1 {
background: url(img_flower.jpg);
background-size: 100px 80px;
background-repeat: no-repeat;}
background: url(img_flower.jpg);
background-size: 100px 80px;
background-repeat: no-repeat;}
Define Sizes of Multiple Background Images
The
background-size
property also accepts multiple values for background size (using a comma-separated list), when working with multiple backgrounds.
The following example has three background images specified, with different background-size value for each image:
#example1 {
background: url(img_tree.gif) left top no-repeat, url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
background-size: 50px, 130px, auto;}
background: url(img_tree.gif) left top no-repeat, url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
background-size: 50px, 130px, auto;}
Full Size Background Image
Now we want to have a background image on a website that covers the entire browser window at all times.
The requirements are as follows:
- Fill the entire page with the image (no white space)
- Scale image as needed
- Center image on page
- Do not cause scrollbars
The following example shows how to do it; Use the <html> element (the <html> element is always at least the height of the browser window). Then set a fixed and centered background on it. Then adjust its size with the background-size property:
html {
background: url(img_man.jpg) no-repeat center fixed;
background-size: cover;}
background: url(img_man.jpg) no-repeat center fixed;
background-size: cover;}
Hero Image
You could also use different background properties on a <div> to create a hero image (a large image with text), and place it where you want.
.hero-image {
background: url(img_man.jpg) no-repeat center;
background-size: cover;
height: 500px;
position: relative;}
background: url(img_man.jpg) no-repeat center;
background-size: cover;
height: 500px;
position: relative;}
CSS background-origin Property
The CSS
background-origin
property specifies where the background image is positioned.
The property takes three different values:
- border-box - the background image starts from the upper left corner of the border
- padding-box - (default) the background image starts from the upper left corner of the padding edge
- content-box - the background image starts from the upper left corner of the content
The following example illustrates the
background-origin
property:
Example :
#example1 {
border: 10px solid black;
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: content-box;}
border: 10px solid black;
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: content-box;}
0 comments:
Post a Comment