Design Shack - Web design showcase, CSS tutorials and web standards


Sponsors

PSD to HTML
PSD to HTML
Web Hosting UK
PSD to XHTML
Dedicated Server Hosting

About

Design Shack showcases inspiring web design, alongside resources and tutorials for you to succeed in the same way. It is brought to you by David Appleyard, a freelance designer who is available for work.

Introduction to CSS3 - Part 6: Backgrounds


Written by David Appleyard, On 27th May 2008.
Filed in Articles, CSS.



For the last part of our introductory series to CSS3, we will be taking a look at the new background properties. These include background size, using more than one background for an element, and background origin (which effects the position of a background).

The new features allow greater control of the background element and will provide designers with a whole array of new possibilities. As usual, examples can be found below:

View the live examples page

Background Size

Before CSS3, background size was determined by the actual size of the image used. It will be possible with the next CSS revision to specify in terms of percentage or pixels how large a background image should be. This will allow you to re-use images in several different contexts and also expand a background to fill an area more accurately.

The following is a simple example of resizing the Design Shack logo as a background in the top left hand area of a div:

1
2
3
4
5
6
7
8
9
10
.backgroundsize {
background: url(http://www.designshack.co.uk/images/logo.gif);
-webkit-background-size: 137px 50px;
-khtml-background-size: 137px 50px;
-o-background-size: 137px 50px;
background-size: 137px 50px;
background-repeat: no-repeat;
padding: 60px 5px 5px 10px;
border: 3px solid #ddccb5;
}

CSS3 Background Resizing

Multiple Backgrounds

The new ability to use multiple backgrounds is a great time saver, allowing you to achieve effects which previously required more than one div. Whether it will be possible to combine this with background-size will be interesting to see.

The example below uses one background for the top border, one repeated vertically for the left and right borders and a third at the bottom.

1
2
3
4
5
6
7
8
.multiplebackgrounds { 
height: 150px;
width: 270px;
padding: 40px 20px 20px 20px;
background: url(top.gif) top left no-repeat,
url(bottom.gif) bottom left no-repeat,
url(middle.gif) left repeat-y;
}

CSS3 Multiple Backgrounds

Background Origin

CSS3 allows you to specify how the position of a background is calculated. The background-origin property can be set to start positioning from either the border, padding, or content. This allows for greater flexibility in terms of placing a background image.

In conclusion…

There are a whole new variety of background options available with CSS3. As with all the other modules covered in this introductory series, it leads to greater flexibility and makes it much easier to recreate previously complex effects. Whilst not supported by most browsers as yet, it shouldn’t be too long before you can get creating stunning user interfaces with these new additions.

We really hope that you have enjoyed this series, and we’ll be keeping you up to date on all the latest CSS3 developments as they happen over coming months. Here’s looking forward to the next generation of style sheets!

Other posts in the series

Tags: , , , , , ,



5 Comments


  1. An interesting series; thanks. Just one point to note; in your Background Size example it’s good practice to include the property name (without browser prefix) after you’ve listed the browser-specific ones; it future-proofs the style in the case that browsers decide the property is stable and remove the prefix in a future version.

  2. Hi Peter. Thanks for pointing that out, I have updated the tutorial to reflect it.

  3. Thanks for the good article about CSS3. I’m wondering about the IE, because only it does not support these exciting features.

  4. [...] Introduction to CSS3 - Part 6: Backgrounds [...]

  5. Alif amri says:

    Hi, thanks for your tutorial, but how about css3 browser compatiblity. Still waiting about that. Thanks


Leave a Reply