Today we will work on background images, which are set using the background property and its additional values. Let's consider a couple practical examples in the implementation of setting several backgrounds to the same element.

This can be useful in many cases and moments. Especially the use of pseudo elements in this case, since they are very flexible in parameters.

Lots of background images

In order not to create a block within a block, the easiest way is to add one line of rules to the main element and thus get the desired result. We can consider this a laconic option, especially since it eliminates the need to once again go into source. Everything will be done using CSS alone.

Blockimg( background: url("img/img2.png"),/*topmost background and then sequentially*/ url("img/img3.png"), url("img/img1.jpg"); background-position :370px center, 120px 150px, center center;/*position of images*/ background-repeat: no-repeat;/*repeat picture*/ background-color: #444;/*if background color is needed*/ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); margin: 100px auto 15px; box-sizing: border-box; padding: 25px; width:700px; min-height: 300px; ) /*shortened version*/ .blockimg ( background: url("img/img2.png") no-repeat 370px center, url("img/img3.png") no-repeat 120px 150px, url("img/img1.jpg") no-repeat center center ; margin: 100px auto 15px; box-sizing: border-box; padding: 25px; width:700px; min-height: 300px; )

Explanation. We give the element a background image, indicating the path to its location. Separated by commas, we are given the opportunity to enter many more backgrounds, as can be seen in the code above. The order of their numbers determines which image will be on top of the others. That is, the first background is higher than all the others, and then the sequence follows the principle of a regular graphic editor.

Next, additional parameters are specified through individual properties: position, repetition, size, and, if necessary, color. Please also note that all parameters are written separated by commas, in the same order as the number of the picture.

And one last detail. The entire code can be shortened by using just one generic property, background . There is a second option in the code example that shows how this is done.

Background image via pseudo element

Also don't forget about alternative options as such are the before and after pseudo-elements. There is a positive advantage in their use - the image can be moved beyond the edge of the element, so that it does not disappear at the border, but is on top of it. This technique will come in handy if you need to create something like a 3D effect.

Blockimg( background: url("img/img1.jpg") no-repeat;/*element background*/ position:relative;/*positioning area*/ margin: 200px auto 15px; box-sizing: border-box; padding: 25px; width:700px; min-height: 300px; ) .blockimg::before( background: url("img/img1.png") no-repeat center center; bottom: 0; content: ""; height: 295px; left: 0; position: absolute;/*absolute positioning*/ right: 0; top: -150px; )

Explanation. In fact, everything is very simple. We set the background to the main element in the usual way. Next comes the key property position: relative; , which defines the area for moving another element that is in the main element and has the property position:absolute; .

Instead of another element, although formally it goes as a separate area, we use a pseudo-element. We give it an absolute position and position it in the place we need.

Task

Cross-browser add two background images for the block.

Solution

Background pictures are quite actively used to create blocks, since with their help the most bizarre designs are formed. In particular, you can add decorative corners, vertical and horizontal lines, and much more to the element.

Cross-browser (that is, for all browsers, including older versions) you cannot add two backgrounds to one element, so you have to use a trick and nest one element inside another. In this case, for each nested element, its own background image is created, due to the overlay of blocks, and the effect is created that there is only one element and it contains several background images. The important thing here is not to add padding to external elements, otherwise the effect will be lost.

For example, consider creating vertical decorative lines to the left and right of the block. To do this, first prepare images that should be repeated vertically without joints. In Fig. Figure 1 shows the background image that will be used for the first element; it will form the border on the left, and in Fig. 2 background for a nested element that adds a border to the right.

Rice. 1. Background image for the border on the left

Rice. 2. Background image for the border on the right

As block element to which the background is added, the tag is usually used

due to its convenience and versatility. The background image is set via the background style property, as shown in Example 1.

Example 1: Two background images

HTML5 CSS 2.1 IE Cr Op Sa Fx

Two background images

During 11 months of duty, radio operators conducted 8,642 communication sessions with a total volume of 300,625 groups. These are only meteorological and aerial telegrams. Received from the radio station Cape Chelyuskin 7450 groups.


Result this example shown in Fig. 3.

Rice. 3. View of a block with two background images

In this example, so that the background is added only to the desired tag

, and not all similar elements on the page, a class called block is used, to which all style properties are applied. To style only the nested
the example specifies a context selector (the .block div construct), it defines the style only for the tag
located inside
.

When we talked about the background-image property at the beginning of this section, we didn't mention one feature: in CSS3, you can add multiple backgrounds to a single element by simply listing them separated by commas. This is especially necessary when an element with a background has a variable width or height, and the background must adapt to its size.

How to Set Multiple Backgrounds in CSS

We will show you an example that may well be useful in practice. Let's imagine that we need to place a block of text in a frame. The frame represents graphic file in PNG format:


In this problem, the height of the text is unknown to us - we do not know whether the text will fit completely within the frame or go beyond it. Because of this unknown quantity, we can't risk using the original frame design as a background. Nose using CSS we can make this frame extend as needed. To do this, you will have to divide the original drawing into graphic editor into three parts - top, bottom and middle - and save each file separately. Like this:

Top of the frame


Bottom of frame


Middle of the frame


We will make the background with the image of the middle of the frame repeating along the axis Y, while the top and bottom of the frame will not be duplicated. Let's add all three backgrounds to the element, and also write down other necessary styles:

Frame ( background-image: url(https://goo.gl/tKyzHt), /* top of the frame */ url(https://goo.gl/SUKymM), /* bottom of the frame */ url(https: //goo.gl/Km7HVV); /* middle of the frame */ background-position: center top, /* position of the top of the frame */ center bottom, /* position of the bottom of the frame */ center top; /* position of the middle of the frame */ background -repeat: no-repeat, /* the top of the frame is not repeated */ no-repeat, /* the bottom of the frame is not repeated */ repeat-y; /* the middle of the frame is repeated vertically */ background-size: contain; /* here for all backgrounds have the same value */ height: auto; /* block height depends on the amount of content */ width: 400px; /* block width is fixed */ padding: 30px; /* block internal padding */ )

Each background must be separated by a comma, and only after the last one is a semicolon placed to indicate the end of the declaration. For convenience and better readability of the code, we recommend specifying each URL on a new line.

Background pictures are placed according to the principle of layers - one below the other. The background specified first will be on the top layer, the second background will be below the first, and the third will be below the first two. That's why we placed the picture with the middle of the frame at the very end - so that it does not overlap the top and bottom parts.

Next, the code sets the background-position and background-repeat properties for each background (the same order in which the background images are arranged). Yes, you guessed it right: if required, then you can specify the values ​​of other background properties, separated by commas. And if you need to apply one value to all backgrounds, you write it as usual (in our case, the background-size: contain property).

Well, let's take a look at the result:


As you can see, the frame is positioned correctly, and now it beautifully frames the contents of the block. What happens if we increase the amount of text in the block? Let's look:


The middle part of our frame was duplicated vertically the required number of times, as if stretching out in length and adjusting to the text. This is the effect that could not be realized if we used a solid image of the frame. Let's add even more text for clarity:


Of course, multiple backgrounds can be used to solve other problems. We showed just one example out of many. Try to come up with your own situation and practice using a group of background pictures.

Using shorthand notation

The background property also accepts multiple values. In the case of using several backgrounds, an abbreviated recording can be much more convenient, because it is more difficult to get confused. Let's rework our code for the frame:

Background: url(https://goo.gl/tKyzHt) center top / contain no-repeat, /* top of frame */ url(https://goo.gl/SUKymM) center bottom / contain no-repeat, /* bottom of frame */ url(https://goo.gl/Km7HVV) center top / contain repeat-y; /* middle of frame */

This option looks less cumbersome and is easier to understand.

You can add multiple background images to one element at once through a single background property. This allows you to get by with one element to create a complex background or one image, displaying it several times with various settings. All images with their parameters are listed separated by commas, with the image that is displayed on top of the other images indicated first, and the last, respectively, the lowest image. Example 1 shows how to create a background with three images.

Example 1. Three backgrounds

Background

If you need to separately set some style property for the background, like background-size as in the example above, then the parameters for each background are listed separated by commas. The result of this example is shown in Fig. 1.

Rice. 1. Background with three images

Individual background images allow you to change their position and also animate them, as shown in example 2.

Example 2: Animated background

Background

Let's now consider how to use one picture to create a block with a frame (Fig. 2). The width of the block is fixed, and the height varies depending on the volume of the block’s contents.

Rice. 2. Hand-drawn frame

The figure clearly shows the upper and lower parts, which need to be cut out in a graphics editor and positioned horizontally. The middle part is selected so that it is repeated vertically without seams. The picture has a pronounced repeating pattern, so there should be no difficulty in highlighting it. The result will be a prepared image (Fig. 3). The checkered field indicates transparency; it allows you to set a colored background along with images and easily change it through styles.

Rice. 3. Image prepared for the background

The background itself is displayed by the background property, which also specifies the coordinates of the desired fragment. The parameters of each background are listed separated by commas, and in this case their order matters. We want the top and bottom of the block to not overlap, so we put them first (example 3). The background color is specified last.

Example 3. Several background images

Background

Huitzilopochtli - "sorcerer of the hummingbird", god of war and the sun.

Tezcatlipoca - “smoking mirror”, the main god of the Aztecs.

Human sacrifices were made to both gods.



The first background displays the top border of the block, the second background - the bottom, and the third - the vertical borders. The last one is the color that is visible in the transparent central part of the block (Fig. 4).

Task

Add two background images for a block using CSS3.

Solution

Modern browsers allow you to add an arbitrary number of background images to an element, listing the parameters of each background separated by commas. It is enough to use the universal background property and specify one background for it first and a second one separated by a comma.

For example, consider creating vertical decorative lines to the left and right of the block. To do this, first prepare images that should be repeated vertically without joints. In Fig. 1 shows the background image, which will be displayed on the left edge, and in Fig. 2 picture for display on the right edge.

Rice. 1. Background image for the border on the left

Rice. 2. Background image for the border on the right

The block element to which the background is added is usually the tag

due to its convenience and versatility, in order to distinguish it from other elements, the block class is added to it (example 1).

Example 1: Two background images

HTML5 CSS3 IE Cr Op Sa Fx

Two background images

During 11 months of duty, radio operators conducted 8,642 communication sessions with a total volume of 300,625 groups. These are only meteorological and aerial telegrams. Received from the radio station Cape Chelyuskin 7450 groups.


The result of this example is shown in Fig. 3.