CSS3 Backgrounds
CSS3 contains several new background properties,which allow greater control of the background element.
The following background properties:
- background-size
- background-origin
- use of multiple background images
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.
A. The background-size Property:
- The background-size property specifies the size of the background image. It will accept two parameters: the x and y widths, respectively.
- 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.
- If we wanted a particular image to take up the entire background of the body element, then the code below will take up all available space direct the 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 1:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Resize- background-image </title>
<style type="text/css">
#wrapper
{ margin:auto; width:500px;
background:url(../4oct-business/images/img1.jpg);
background-size:80px 60px;
-moz-background-size:80px 60px; /* Firefox 3.6 */
background-repeat:no-repeat;
padding-top:40px;
}
</style>
</head>
<body>
<div id="wrapper">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
</p>
<p>Original image: <img src="../4oct-business/images/img1.jpg" height="100" width="282"/></p>
</div>
</body>
</html>
Result:
Example 2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Resize- background-image- %</title>
<style type="text/css">
#wrapper
{ margin:auto; width:500px;
background:url(../4oct-business/images/img1.jpg);
background-size:100% 100%;
-moz-background-size:100% 100%; /* Firefox 3.6 */
background-repeat:no-repeat;
padding-top:40px;
}
</style>
</head>
<body>
<div id="wrapper">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
</p>
<p>Original image: <img src="../4oct-business/images/img1.jpg" height="100" width="282"/></p>
</div>
</body>
</html>
Result:
B. 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.
Syntax:
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;
}
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>background-origin </title>
<style type="text/css">
body{margin:0 auto; width:500px;}
div
{
border:1px solid black;
padding:35px;
background-image: url(../4oct-business/images/right-img7.jpg);
background-repeat:no-repeat;
background-position:left;
}
#div1
{
background-origin:border-box;
}
#div2
{
background-origin:content-box;
}
</style>
</head>
<body>
<p>background-origin:border-box:</p>
<div id="div1">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
</div>
<p>background-origin:content-box:</p>
<div id="div2">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
</div>
</body>
</html>
Result:
C. Multiple Backgrounds Images:
CSS3 allows you to use several background images for an element. You might use a texture, a gradient color, and images in the background.By using a comma as the separator, we’re referencing two separate background images. Notice how, in the first case, it’s placed in the top left position (0 0), and, in the second, the top right position (100% 0).
Syntax:
.box {
background: url(image/path.jpg) 0 0 no-repeat,
url(image2/path.jpg) 100% 0 no-repeat;
}
Example:
New Background Properties
Property | Description |
background-clip | Specifies the painting area of the background images |
background-size | Specifies the positioning area of the background images |
background-origin | Specifies the size of the background images |
No comments:
Post a Comment