Monday 26 December 2011

CSS3 Backgrounds resize Property





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

 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.

Syntax: 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 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:

No comments:

Post a Comment