How to set a background without repeating in css. Background in CSS (color, position, image, repeat, attachment) - everything for setting the background color or background image of Html elements
brief information
CSS versions
Values
url The value is the path to the graphic file, which is specified inside the url() construct. The path to the file can be written either in quotes (double or single) or without them. none Disables the background image for the element. inherit Inherits the value of the parent.HTML5 CSS2.1 IE Cr Op Sa Fx
Object model
document.getElementById("elementID ").style.backgroundImage
Browsers
Internet Explorer up to version 7.0 inclusive, applies the background to the inside border of an element that has the hasLayout property set. If the element does not have a hasLayout , the background-image property will respect the element's borders, as specified in the specification. The difference in display will be noticeable if the borders are dashed or dotted rather than solid.
If the element is set to scroll or auto , Internet Explorer 8 will have a one-pixel vertical delay when the background scrolls.
Internet Explorer versions up to and including 7.0 do not support the inherit value.
If the background is set for a table row (tag
HTML5 CSS2.1 IE Cr Op Sa Fx
1 | 2 | 3 |
Result this example V Chrome browser shown in Fig. 1. Internet Browser Explorer, Opera and Firefox correctly display the background for the line (Fig. 2).
Rice. 1. Repeat the background for each cell
Rice. 2. Background for the entire line
I think there is not a single site where the property is not used CSS background. It would seem that what could be simpler than this property? But no, its capabilities are much wider than the usual assignment of a picture or color as a page background. Some things will be familiar, while others will probably be new to many. In any case, it will be useful to know thoroughly how the background works.
CSS3 brought a lot of new things to the property, such as transparency and assigning several images as a background, but we will talk about this below, but first we will look at the basics of the property background.
background-color
I am more than sure that you have already assigned the background color more than once. This can be done using several types of notation: regular (the color name is used), hexadecimal or RGB notation. Each type is equivalent, use whichever you like best. I try to use the shortest option, and for easier perception, the resulting style file is a little smaller in size.
P (background-color: red;) p (background-color: #f00;) p (background-color: #ff0000;) p (background-color: rgb(255, 0, 0;))
CSS3 supports transparency, so we can add it to our color, for example like this:
P (background-color: rgba(255, 0, 0, 0.5);)
The last figure set the transparency to 50%. You can set the transparency value from 0 (fully transparent background) to 1 (fully opaque).
background-image
This property is also used very often; it allows you to assign an image to the background. CSS3 has added the ability to assign multiple images to the background, each creating its own layer, so each subsequent one overlaps the previous one. Why might this be useful? Everything is quite simple - let’s say you need to screw little frills into each corner of the site. Given a more or less rubbery layout, using one image is not an option. Therefore, we make 4 “layers”, move each image to its own corner and that’s all, the problem is solved
Body (background-image: url("image1"), url("image2"), url("image3"))
If you need to assign one image to the background, we leave only the first one in the code, I think this is understandable.
When using any images as a background, you should remember two rules:
- set a contrasting background color in case the user does not see the image for some reason. It can simply turn off the display of pictures, saving traffic.
- do not use a background image to convey any important information. For the reasons stated above, the user may not see it.
Support for multiple background images is quite extensive. All browsers, even IE8, support this property.
From the author: Hello everyone. Background colors and images play a huge role in web design, as they allow you to design any elements more attractively. Today we will look at how to make a background in HTML.
Is it possible to use HTML to set the background?
I’ll say right away that no. In general, html was not created to design web pages. It's just very inconvenient. For example, there is a bgcolor attribute, with which you can set the background color, but this is very inconvenient.
Accordingly, we will use Cascading Style Sheets (CSS). There are much more opportunities for setting a background. Today we will look at the most basic ones.
How to set background using css?
So, first of all, you need to decide which element you want to set the background to. That is, we need to find a selector to which we will write a rule. For example, if you need to set the background for the entire page as a whole, you can do this through the body selector, and for all blocks through the div selector. Well, etc. The background can and should be bound to any other selectors: style classes, identifiers, etc.
After you have decided on the selector, you need to write the name of the property itself. To set the background color (namely a solid color, not a gradient or an image), use the background-color property. After it you need to put a colon and write the color itself. This can be done in different ways. For example, using keywords, hex code, rgb formats, rgba, hsl. Any the method will work.
The most commonly used method hexadecimal code. To select colors, you can use a program that displays the color code. For example, photoshop, paint or some online tool. Accordingly, as an example, I will write a general background for the entire web page.
body( background-color: #D4E6B3; )
This code needs to be inserted into the head section. It is important that the files are in the same folder.
Picture as background
I'll use a small icon as the image. html language:
Let's create an empty block with an identifier:
< div id = "bg" > < / div > |
Let's give it explicit dimensions and background:
#bg( width: 400px; height: 250px; background-image: url(html.png); )
#bg( width: 400px; height: 250px; background - image : url (html . png ) ; |
From this code you can see that I used a new property - background-image. It is intended specifically for inserting a picture as a background to an html element. Let's see what happened:
To specify a picture, you must write after the colon keyword url, and then indicate the path to the file in parentheses. In this case, the path is specified based on the fact that the image is in the same folder as the html document. You also need to specify the image format.
If you have done this, and the background is still not displayed in the block, check again whether you wrote the name of the image correctly, whether the path and extension are set correctly. These are the most common reasons that the background simply does not appear because the browser cannot find the image.
But did you notice one thing? The browser took and multiplied the image throughout the block. So, just so you know, this is the default behavior of background images - they are repeated vertically and horizontally as long as they can fit into the block. By this behavior you can easily control. To do this, use the background-repeat property, which has 4 main values:
Repeat – default value, the image is repeated on both sides;
Repeat-x – repeats only on axis x;
Repeat-y – repeats only along the y axis;
No-repeat – does not repeat at all;
You can write each value and see what happens. I'll write it like this:
background-repeat: repeat-x;
background - repeat : repeat - x ; |
Now repeat only horizontally. If you set no-repeat, then there would be only one picture.
Great, we can finish here, since these are the basic capabilities of working with the background, but I will show you 2 more properties that allow you to get more control.
Through repetition, layout designers used to be able to create background textures and gradients using one tiny image. It could be 30 by 10 pixels or even smaller. Or maybe a little more. The image was such that when it was repeated on one or even both sides, no transitions were visible, so the result was a single, seamless background. By the way, this approach should be used now if you want to use seamless texture on your website as a background. Today, a gradient can already be implemented using css3 methods, we will definitely talk about this later.
Background position
By default, the background image, unless it is set to repeat, will be in the upper left corner of its block. But the position can be easily changed using the background-position property.
You can set it in different ways. One option is to simply indicate the sides in which the picture should be located:
background-position: right top;
background - position : right top ; |
That is, vertically everything remains the same: the background image is located on top, but horizontally we changed the side to right, that is, right. Another way to set a position is as a percentage. In this case, the countdown begins in any case from the upper left corner. 100% - the entire block. Thus, to place the picture exactly in the center, we write it like this:
background-position: 50% 50%;
background-position: 50% 50%; |
Remember one important thing about positioning - the first parameter is always the horizontal position, and the second is the vertical position. So, if you see a value of 80% 20%, then you can immediately conclude that the background image will be shifted a lot to the right, but it won’t go down much.
And finally, you can specify the position in pixels. Everything is the same, only instead of % there will be px. In some cases, such positioning may be necessary.
Shorthand notation
Agree that the code turns out to be quite cumbersome if everything is set the way we did it. It turns out that the path to the picture needs to be specified, the repetition, and the position. Of course, repetition and position are not always necessary, but in any case, it would be more correct to use a shorthand notation for the property. It looks like this:
background: #333 url(bg.jpg) no-repeat 50% 50%;
background: #333 url(bg.jpg) no-repeat 50% 50%; |
That is, the first step is to record the overall solid background color, if necessary. Then the path to the image, repetition and position. If some parameter is not needed, then simply omit it. Agree, this is much faster and more convenient, and we also significantly reduce our code. In general, I advise you to always write in abbreviated form, even if you only need to indicate a color or picture.
Controlling the size of the background image
Our current image isn't very good for demonstrating the next trick, so I'll take another one. Let it be the size of a block or larger. So, imagine that you are faced with the task of making a background image so that it does not completely fill its block. And the picture, for example, is even larger than the block size.
What can you do in this case? Of course, the simplest and most reasonable option would be to simply reduce the image, but it is not always possible to do this. Let's say it lies on the server and in this moment there is no time or opportunity to reduce it. The problem can be solved using the background-size property, which can be called relatively new and which allows you to manipulate the size of the background image, and indeed any background.
So my image now takes up all the space in the block, but I'll give it a background size:
background-size: 80% 50%;
background-size: 80% 50%; |
Again, the first parameter sets the horizontal size, the second - the vertical size. We see that everything was applied correctly - the photo became 80% of the width of the block in width and half in height. Here you just need to make one clarification - by setting the size as a percentage, you can influence the proportions of the picture. So be careful if you want not to upset the proportions.
As you can guess, the background size can also be specified in pixels. There are two more keywords that can also be used:
Cover – the image will be scaled so that at least on one side it completely fills the block.
Contain – scales it so that the image fits completely into the block at its maximum size.
The advantage of these values is that they do not change the proportions of the picture, leaving them the same.
You should also understand that stretching the picture can lead to a deterioration in its quality. I can give an example from the life and real practice of layout designers. Everyone knows and understands that when designing for desktops, you need to adapt the site to the main monitor widths: 1280, 1366, 1920. If you take a background image with a size of, say, 1280 by 200, and do not give it a background-size, then screens with a width larger An empty space will appear, the image will not fill the width completely.
In 99% of cases, this does not suit the web developer, so he sets background-size: cover so that the image always stretches to the maximum width of the window. This is a good technique to use, but now you will be faced with the problem that users with a screen width of 1920 pixels may see a suboptimal picture quality.
Let me remind you, it will stretch to its maximum width. Accordingly, the quality will automatically deteriorate. The only correct solution here would be to initially use a larger image – 1920 pixels wide. Then on the widest screens it will be in its natural size, and on others it will simply be gradually cut off, but at the same time, if the background image is properly selected, this will not affect the appearance of the site.
In general, this is just 1 example of how to use the knowledge you gained in this article when laying out real layouts.
Translucent background using css
Another feature that can be implemented using css is translucent background. That is, through this background it will be possible to see what is behind it.
As an example, I will set the entire page as the background to the image that we used earlier in the examples. For the block with identifier bg, on which we conduct all our experiments, we will set the background using the rgba color setting format.
As I said earlier, there are many formats for setting colors in CSS. One of them is rgb, a fairly well-known format for those who work in graphic editors. It is written like this: rgb(17, 255, 34);
The first value in brackets is the saturation of red, then green, then blue. The value can be numeric from 0 to 255. Accordingly, the rgba format is no different, only one more parameter is added - the alpha channel. The value can be from 0 to 1, where 0 is complete transparency.
Hello, dear readers of the blog site. Today we will look at five CSS rules that allow you to set a background for any element in Html - background-position (image, repeat, color, attachment). Well, let’s also not forget to mention the Background compound rule.
There is nothing complicated about this, but there are certain subtleties and nuances that you need to know and already ready-made template(remember about, which will help reveal all the ins and outs of any design).
Let me remind you once again that this article is part of a series and it would be best to start studying style markup from the beginning, namely with an article about what CSS is and what it is used with, and then follow in the order given in the reference book. Although, in any case, it’s up to you, but now let’s talk about setting the background.
Color, background-color and background-image
Let's first look at how color is set HTML elements by using Css color rules. In fact, everything is simple here. The syntax is completely normal and you can set the color in accordance with how it was done in hypertext markup language. As you remember, placed after the hash sign (hash - “#fe35a3”), or using three digits, if the first coincides with the value of the second, the third with the fourth, and the fifth, respectively, with the sixth (color code “#aa33ff” can be briefly written as "a3f").
Also colors in Html and Css code can be represented as words (for example, “red”), but most often it is the hexadecimal code that is used:
Color:#303
As an example, I colored this small paragraph the same color as above (#303). It is now slightly different from the color of all other paragraphs (darker), which is set to #555 in CSS file used by me WordPress themes. But setting the color using color is quite simple, but with the background it will be a little more complicated.
So, for the background in Css There are five rules that can be combined into one if desired. To see them, you can go to the current W3C specification page and look for anything with the word Background:
- background color - using this rule, you set the background color for any Html element. You can use either a code or the name of the shade, i.e. everything is exactly the same as it was when using color.
background image - with it you can use a picture as a background (but be sure to read about it, because heavy pictures will slow down the loading of pages), the path to which will be indicated in the url () functionality.
If you look at the specification you will see that default background color any element will be transparent (the default value of the rule is “background-color:transparent”). True, in elements it will not be transparent by default, because These are system elements and everything about them is different and different from ordinary hypertext markup language tags.
The color in background-color is set as standard (six or three digits of hexadecimal code, or a word):
Background-color:#FEFCDE
For example, the background of this paragraph is specified using background-color with the color code given just above.
All other four CSS rules will concern only the background image, which can be set for any Html element and, if desired, precisely positioned. Which graphic file will be used can be specified using background-image.
If you look at the markup language specification, you'll see that background-image defaults to "none" (i.e., no image is used for the background). Well, if you still need this, then in the url() functionality you will need to indicate the path to it:
Background-image:url(https://site/image/comment_top_focus.gif);
For example, for this paragraph I used a graphic file with a background, the path to which is described just above. You see that the entire area allocated for this paragraph is covered with a repeating image, which in the original looks like this:
Those. when using only one background-image rule specifying the path to graphic file, this same image will be multiplied both vertically and horizontally until it covers the entire area allocated on the web page for this specific Html element (in our example it was a paragraph). Why is this happening?
Background-repeat - repeat the background image
Yes, because we didn’t specify any value for the CSS rule background-repeat, which means that the default value will be used for it. Looking at the specification, we find out that this value corresponds to “repeat” (repeat the image on all axes). The answer came naturally.
Therefore, with background-repeat we can manage background image repetitions. This rule can only have four meanings:
![](https://i2.wp.com/ktonanovenkogo.ru/image/color.png)
Background-position - background positioning
Now the question arises, is it possible to move the background image away from the left? top corner area limiting the size of the element. Of course you can, and there is a separate rule for this purpose. background-position:
Looking at the CSS specification, it becomes clear why the background image by default is pressed exactly to the top left edge of the Html element area. Because the value "0% 0%" is the default value for the background position rule.
Well, when this rule is not explicitly set for an element (as in our case), then the browser selects its value, which is accepted in the specification by default (note that the coordinate axes in CSS are reported exactly from the upper left edge of the area element).
It is also clear from the specification that to position the background image using background-position, you can use both relative (percentages) and absolute values (for example, ). Well, you can also use words that will correspond to certain digital values. But first things first.
When setting the background image positioning using absolute units in background-position the following principle is used to determine its final position:
Those. the browser will calculate the specified offsets along the X and Y axes from the origin of the area in which the object is positioned to the origin of this image itself. For example, in this paragraph I positioned the background image via background position using the following CSS rules:
Background-image:url(https://site/image/logo.png); background-repeat:no-repeat; background-position:400px 25px;
Please note that in this case it will be aligned to the center of the viewing area, and not to the center of the area allocated for these paragraphs. It is clear that in reality such placement of a background image is unlikely to be used.
However, if you set a fixed background position for elements such as Body or Html (i.e. in tags that cover the entire web page), then this image will always be visible in the viewing area and this is exactly what is used CSS property background-attachment in modern block layout.
Is there some more prefabricated rule Background, which allows you to combine all five rules described above in one bottle. Moreover, the values for all five in the combined version can be used in any order and in any quantity (they are unique and the browser will not confuse them with each other). Anything you don't explicitly specify will be assumed by the browser to be the default value.
Png) no-repeat 50%;
The example prefab rule is applied to this paragraph for clarity. It didn't turn out pretty, but that's not the main thing. This paragraph uses a strange background fill yellow color, and also uses an image of the Liveinternet logo, aligned in the center of the paragraph. Because If the background-attachment rule is not given any value, then the scroll value (the default) is used.
If for some element you want to set only a color fill and not bother with a background image, then you can do this instead:
Background-color:#FEFCDE
write:
Background:#FEFCDE
Because all other values of the prefab rule will be taken by default, and that’s what you needed.
Good luck to you! See you soon on the pages of the blog site
You might be interested
List style (type, image, position) - Css rules for customization appearance lists in HTML code How to configure the alternating background color of rows of tables, lists and other Html elements on the site using the nth-child pseudo-class
Position (absolute, relative and fixed) - methods Html positioning elements in CSS (rules left, right, top and bottom)
Float and clear in CSS - block layout tools
Positioning using Z-index and CSS rule Cursor to change the mouse cursor
Padding, Margin and Border - set internal and external padding in CSS, as well as borders for all sides (top, bottom, left, right)
Display (block, none, inline) in CSS - set the display type of Html elements on the web page
Priorities in Css and their increase due to Important, combination and grouping of selectors, user and author styles
CSS - what is it, how do cascading style sheets connect to HTML code using Style and Link
Tag, class, Id and universal selectors, as well as attribute selectors in modern CSS