Various properties that existed in CSS have been made extremely easy to implement in CSS3. In this tutorial, we would discuss two of these important properties: Background image size and multiple backgrounds.
The background size property is an amazing thing to have on your tool belt when you’re creating a liquid layout. An example of this could be when you have a background image for a container that is for the sidebar. The classic way of doing this would require tweaking to have the image repeat along the y-axis, but with CSS3 it’s like adding another background property. The code required in classic way was also long and cumbersome. Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<style type="text/css"> .box { position: relative; overflow: hidden; } .box img { position: absolute; top: 0; left: 0; width: 50%; height: 50%; z-index: 100; } .box .content { position: absolute; z-index: 200; } </style> |
1 2 3 4 5 6 |
<div class="box"> <div class="content"> <!--CONTENT--> </div> <img src="http://www.starcomsystems.net/images/logo.png" alt="" /> </div> |
Short and simple code used in CSS3 is as follows:
1 2 3 4 5 6 7 8 9 10 |
<style type="text/css"> .box { background: #ccc url(http://www.etechy101.com/wp-content/uploads/2010/07/Wallpaper-1080p-44.jpg) no-repeat; -webkit-background-size: 50%; -o-background-size: 50%; -khtml-background-size: 50%; } </style> |
1 2 3 |
<div class="box"> <!--CONTENT--> </div> |
Now let’s talk a bit about multiple backgrounds. This is quite a powerful property and can be used in many web applications. Let’s see how this important property was implemented in classic way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<style type="text/css"> .box { width: 200px; height: 150px; background: url(images/bg.png) repeat-x; } .box2 { width: 100%; height: 100%; background: url(images/text.png) center center no-repeat; } </style> |
1 2 3 4 5 |
<div class="box"> <div class="box2"> <!--CONTENT--> </div> </div> |
While same thing is implemented in much simpler and elegant manner in CSS3:
1 2 3 4 5 6 7 8 9 |
<style type="text/css"> .box { width: 200px; height: 150px; background: url(images/text.png) center center no-repeat, url(images/bg.png) repeat-x; } </style> |
1 2 3 |
<div class="box"> <!--CONTENT--> </div> |
The syntax is easy and the code is short. So basically CSS3 has pretty much eliminated the need for unnecessary code and has made the life of programmers and web developers much easier.