Mastering CSS3. Multiple backgrounds
By Konstantin Kichinsky
Our journey to the world of CSS3 shadows continues. Today we will focus on another interesting feature — how to use multiple backgrounds with CSS3.
There are many reasons why you may need to create a composition of multiple images to build you background. I think the most important are the following ones:
I believe you may have other reasonable arguments. :)
So we need to build a multi-layered background by placing some images on top of others. How this problem is usually solved? It is really easy: just create a container (like a div element) for each of the images you have and add a background for it using a CSS rule. Next you insert one container into another or place them in a row and apply corresponding positioning CSS rules. I have here a simple sample:
The “fishing” class if inside of the “mermaid” class only for demo purposes.
And here we have some CSS styles:
Note, for the “fishing” class I’m using the new background positioning syntax, also defined in CSS3. But as for now it is supported only by IE9+ and Opera 11+, but does not yet work in Firefox 10 or Chrome 16. So the users of the last two cannot catch the fish :(
Let’s continue. Is it possible to simplify this composition?
This is when the multiple backgrounds come to the scene. This feature allows you to add more than one background at once and to the same element. Here is how it looks like:
To define multiple backgrounds you should use the background-image rule by enumerating your images comma-separated. You may also use other rules to set a position, repeating mode and other attributes to each of the images — just write up them also using a comma-separated list for the corresponding rule. Note the order of images: they are listed left to right starting with the uppermost one and ending with the lowest one.
The result is 100% identical:
In one rule
If you don’t need you fish to swim in an independent block the whole background can be written in one simple rule:
I’m not showing the same picture one more time but trust me — it is equal to the two images above. Looks at the styles one more time, especially on the background-repeat rule. According to the spec if a part of the list is omitted UA (browser) should repeat present list to fill the rest.
In our case it equal to the following definition:
If you remember the CSS 2.1 it is possible to describe a background image in a one short “background”-rule. What about multiple backgrounds? Actually you also can use the “background”-rule for multiple backgrounds:
But note that you can’t easily omit arguments unless the values are equal to the default ones. Also if you would like to define the color of background you should do it in the latest layer.
I have a real life sample — the dandelion theme on the Yandex website (Russian search provider, YNDX):
If you look in to the source code (pressF12 in your IE to open devtools) you will find a code like that one:
The divs with classes “b-fluff-bg”, “b-fluff__cloud” и “b-fluff__item” have the CSS rules applied adding overlaying background images. The background with cloud is scrolled from left to right, and the backgrounds with dandelion seeds are flying across the screen.
Is it possible to rewrite such composition using css3 multiple backgrounds? Actually yes, but only if 1) it is supported in all target browsers and 2) continue reading ;)
How can we make our multiple backgrounds more dynamic? Internally the browser parses every “background” rule into separate “background-*” rules for each of the attributes. It is very useful if you need to change only one of the attributes. For example you can use “background-position” rule to shift your images. But there are some penalties while dealing with multiple backgrounds: if you are going to move only one layer you still need to rewrite this rule for all layers.
To animate our sea background we can use the following js-code:
You may also use CSS3 Transitions or Animations but it is a good topic for separate discussion.
Parallax and interactivity
Finally using similar technics you can easily add some parallax effects or other interaction effects for you background:
Multiple backgrounds are useful in such scenarios and while we are talking only about backgrounds, not the content, using them is definitely a good way to not pollute the html-code with complex unnecessary elements. But as I said there are some penalties if you need to build a complex and dynamic background: you cannot access a separate layer by id, class or any other parameter. You should remember the order of layers in your code and to change an attribute for just one layer you will need to build a string describing this attribute for all the layers you have. To update one layer you need to update the whole composition:
I’m sure it is possible to build a nice and useful js-library which will virtualize all these layers and provide easy way to change attributes for a separate layer keeping clean your html-code and the DOM.
All modern browsers including IE10 and 9 support multiple backgrounds. You may also use some tools like Modernizr to provide some level of compatibility for older browsers, i.e. by providing alternate background. As Chris Coyier wrote in his article on the stacking order of multiple backgrounds you can use the following approach:
p.s. Check also this phenomenal article about the Cicada Principle by Alax Walker.
CSS properties discussed in this article are defined in the CSS3 Backgrounds and Borders module, which is currently in the Working Draft status. Meanwhile it seems to be quite stable it still can change in details.
About the Author
Konstantin Kichinsky is a developer evangelist focusing on HTML5 and CSS3 web development at Microsoft. Tweet him @kichinsky or read his blog.