We have already touched upon the capabilities of the CSS3 Backgrounds and Borders module, considering the work with shadows ( box-shadow ). Today we will talk a little about another interesting possibility - the use of multiple images in the background.
Background composition
There are many reasons why you may need to compose several images in the background at all, among them the most important are:
saving traffic on the size of the images, if the individual images in the total weigh less than the image with the flattened layers, and
the need for independent behavior of individual layers, for example, when realizing the effects of parallax.
There may be other reasonable reasons :)
Classic approach
So, we need to place several background images one above the other. How is this problem usually solved? Very simple: a block is created for each background image, which is assigned a corresponding background image. Blocks are either invested in each other, or placed in a row with the appropriate positioning rules. Here is a simple example:
In this example, three nested backgrounds and one block with fish, located next to the “background” blocks. In theory, fish can be moved, for example, using JavaScript or CSS3 Transitions / Animations.
Currently, it is supported in IE9 + and Opera 11+, but not supported in Firefox 10 and Chrome 16. So, users of the last two browsers won't be able to catch the fish yet.
Let's go further. How to simplify this design?
Multiple Backgrounds
A new option added in CSS3 comes to the rescue - the ability to define several background images for one element at once. It looks like this:
To define multiple images, you must use the background-image rule, listing individual images separated by commas. Additional rules, also a list, you can set the positioning, repetitions and other parameters for each of the images. Note the order in which the images are listed: the layers are listed from left to right from the topmost to the bottommost.
The result is the same:
One rule
If the fish do not need to be allocated in a separate block for subsequent manipulations, the whole picture can be rewritten with one simple rule:
<divclass="sample3"><divclass="sea"></div></div>
Styles:
.sample3.sea { height:300px; width:480px; position: relative; background-image: url("media/fishing.svg"), url("media/mermaid.svg"), url("media/fish.svg"), url("media/sea.png"); background-position: top right 10px, bottom left, 30px90px, top left; background-repeat: no-repeat, repeat-x ; }
I will not give a picture with the result - believe me, it coincides with the two pictures above. But pay attention to the styles once again, especially on “background-repeat” - according to the specification, if part of the list is skipped at the end, the browser must repeat the specified list the necessary number of times to match the number of images in the list.
In this case, it is equivalent to this description:
If you remember CSS 2.1, it defines the ability to describe background images in concise form. How about multiple images? This is also possible:
.sample4.sea { height:300px; width:480px; position: relative; background: url("media/fishing.svg") top right 10px no-repeat, url("media/mermaid.svg") bottom left repeat-x, url("media/fish.svg") 30px90px no-repeat, url("media/sea.png") repeat-x; }
But note that now you can’t just skip the values ​​(unless they coincide with the default value). By the way, if you want to set the color of the background image, it must be done in the very last layer.
Dynamic images
If the composition is static or dynamic no more than depending on the size of the container, then multiple backgrounds obviously simplify the page design. And what to do if you need to work with separate elements of the composition independently from javascript (move, scroll, etc.)?
By the way, here is an example from life - a topic with a dandelion in Yandex: If you get into the code, you will see something like the following:
Blocks with the classes “b-fluff-bg”, “b-fluff__cloud” and “b-fluff__item” contain background images that overlap each other. And the background with the clouds constantly scrolls, and dandelions fly across the screen.
Can this be rewritten using multiple backgrounds? In principle, yes, but provided 1) support for this feature in the target browsers and ... 2) read on;)
How to add speakers to multiple backgrounds? In such a situation, it turns out to be convenient that, in the internal view, the browser scatters the individual parameters of the background images according to the appropriate rules. For example, for positioning there is a “background-position”, and for shifts it is enough to change only it. However, there is also a charge for the use of multiple images - in this rule (and any similar), you must list the position for all backgrounds specified for your block, and you cannot do this selectively.
To add animation to our fish background, you can use code like this:
And, by the way, animations can also be done through CSS3 Transitions / Animations, but this is a topic for a separate discussion.
Paralax and interactive
Finally, with similar maneuvers you can easily add the effects of parallax or interactive interaction with the background:
Multiple background images are convenient in such scenarios, as while we are talking only about the background (and not the content), using them allows us to avoid littering the html-code and the DOM. But you have to pay for everything: I can’t access individual elements of the composition by name, id, class or some other parameter. I must clearly remember the order of the elements in the code and for every change of any parameter of any element I actually have to glue a string describing the values ​​of this parameter for all elements and update it for the whole composition.
I am sure that this can be wrapped in a convenient javascript code that will take over the virtualization of relationships with individual layers, while leaving the html code of the page as clean as possible.
What's wrong with compatibility?
All modern versions of popular browsers, including IE9 +, support multiple images (you can check, for example, with saniuse ).
If you are confused by the use of JS for providing backward compatibility, you can simply declare the background twice, though it also has its drawbacks in the form of a possible double load of resources (this depends on the implementation of css processing in a specific browser):
If you’ve already begun to think about Windows 8, keep in mind that you can use multiple backgrounds when developing metro style applications, since the same engine is used internally as in IE10.
ps In the subject: I can not remember a phenomenal article about the principle of cicada .