How to make a background image in html. How to set background color in HTML. Basic tools for setting the background of web pages

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, rgba, hsl formats. Any method will do.

The most commonly used method is 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

For the image I will use a small html language icon:

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 the keyword url after the colon, 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 why 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 is worth using now if you want to use a 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 is on the server and at the 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 a 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.

Hi all! Your humble servant is in touch with you with a portion of useful and practical material on the topic of how to set a background for a website. This is the very beginning in html layout and here I will clearly demonstrate with an example how you can make a beautiful background for a site that will definitely attract the Internet user and add originality.

If you noticed, most online businessmen use a unique background on their sales and subscription pages. Today we will analyze the technical component of this process.

So, first we need, of course, a picture. There is one very good site on the foreign Internet where you can download various pictures for website background. There are simply a huge number of them there. The site is called Subtle Patterns.

It appears in the first place in the Google search engine list, so I recommend it for work. You can also find a large number of other sites if you type in a search engine something like “background image patterns”, “download background image for site” and so on.

If you have no problems with the English language, you will understand it without difficulty.

In this article I have chosen a topic and it is called tweed. You can download it.

This is what it looks like in a small version on the website

First, create a new document in the program

And before editing it, be sure to save it under a name, for example index.html and create a folder on the computer, for example, you can create a folder “My site” and place our index file (index.html) in it. It is better to create a folder in English so that the browser does not get confused and display the site incorrectly.

In addition to this, in the main folder “my site” you need to create two more subfolders, in one we will place all our pictures and it will be called “images”, and the other we will name “CSS” (cascading style sheets) and place the file there style.css

Now we can work in our document. The next step is to insert a blank HTML code, the so-called main framework from which it all begins. You can download it directly. Next, we will copy everything from this file and transfer it to our program file. The end result should be something like the following

Next to the file name you will notice a red floppy disk. So, if it is red, it means the file has not been saved, be sure to click on the save button so that the floppy disk turns blue.

In the title tag you can change the title of the document, for example you can make it “My first web page”. And make sure that your page is saved in encoding UTF-8

Otherwise, if there is a different encoding, for example windows-1251, then the text of the document in the browser will be displayed in hieroglyphs. You can change the encoding in the “Encodings – Encode in Utf-8 (without BOM)” section on the program toolbar.

And don’t forget to save every action we take.

Now let's start creating the background in our document. I’ll say right away that all our actions on the design of a web page will take place using cascading style sheets called CSS, that is, we will create the frame in html, and we will bring it to a beautiful style and appearance using CSS.

This way you will teach yourself the correct order of actions. You shouldn’t deal with styles directly in the html document; it’s better to put them in a separate document.

To do this, in our Notepad++ program we will create another file and call it Style.css and save it in a new css folder, which will be located in the shared folder “My site”

Great! In order for our browser to display the page correctly, we need to connect the style sheet to our html document. Here's how it's done

We directly enter the entire line above. With this line we connect our style sheet.
Now we will define a background image for our body tag through the style sheet. To do this, we create the following structure in the style.css document (just take it and write it in the program code)

Let me explain a little here. The background attribute has many values, one of which is background-repeat, which is responsible for stretching our background image for the web document.

BACKGROUND-REPEAT:

REPEAT //(stretch horizontally and vertically) REPEAT-X // (stretch horizontally only) REPEAT-Y //(stretch verticals only) NO-REPEAT //(do not repeat the background image)

In our case, we replicate our small picture both vertically and horizontally. As a result, the entire page is filled with our image. This is what it looks like in the browser:

I also made a selection of sites especially for you where you can download a beautiful background for the site

Any room will look much better if its floor is covered with an expensive Persian carpet. So why is your website worse? Maybe it’s time to “cover” its floor with an expensive, elegant handmade carpet. Let's take a closer look at how to make a background for a website:

Background for the site

It happens that the old website design has become boring. And I want something new and tasty. And the new design will be the same if you make it yourself.

But completely changing the entire design of a resource on your own is a thankless task. And not everyone’s hands are properly trained for this task. Therefore, the easiest way to refresh an old template is to change the background color of the resource or its background image.

There are several ways to change the background on a website. For this, the capabilities of CSS or html are used. But many of the properties for working with the background have the same name and method of application in these web technologies.

Basics of working with backgrounds in html

Several elements can be used as a background:

  • Color;
  • Background image;
  • Texture image.

Let's look at the use of each of them in more detail.

To set the background color for a site, use the background-color property of the style attribute. That is, to set the main color for a web page, you need to write it inside the tag . For example:

Website background #55D52B

In addition to the hexadecimal color code, a value in keyword or RGB format is supported. Examples:

Website background rgb(23,113,44)

Website background green

Setting the background color using keywords has a number of limitations compared to the other two methods.

HTML supports only 16 keywords to set colors. Here are a few of them: white, red, blue, black, yellow and others.

Therefore, in order to set the background for an html site, it is better to use hexadecimal or RGB format.

In addition to color selection, other customization options are available. If the background-color property is set to transparent , the background of the page will become transparent. This value is assigned to this property by default.

Now let's look at the capabilities of the hypertext language for setting a background image for a site. This can be done using the background-image property.

As you can see from the code, the image is linked through the url path specified in parentheses. But not all pictures are so large that their size fills the entire screen area. Let's see how the smaller picture will be displayed.

Suppose we are developing a website about poetry, and we need to use an image of Pegasus as the background. The winged horse will personify the freedom of the poet’s creative thought!

We need the image to be displayed in the middle of the screen once. But, unfortunately, the browser does not understand our lofty desires. And it displays a smaller image for the background of the site as many times as the screen area can accommodate:

Perhaps four smiling horses with wings will be too much inspiration for poets. Therefore, we prohibit the cloning of our Pegasus. We do this using the background-repeat property. Possible values:

  • repeat-x – repeat the background image horizontally;
  • repeat-y – vertically;
  • repeat – on both axes;
  • no-repeat – repetition is prohibited.

Among the listed options, we are interested in the last one. Before changing the background of the site, we use it in our code:

But, of course, it would be better if our flyer was located in the middle of the screen. The background-position property is precisely intended for positioning the background image on the page. You can set location coordinates in several ways:

  • Keyword ( top, bottom, center, left, right);
  • Percentage – counting starts from the upper left corner;
  • In units of measurement (pixels).

Let's use the simplest centering option:

It happens that you need to fix the position of a picture when scrolling. Therefore, before making an image as the background of the site, use the special property background-attachment . The values ​​it accepts are:


  • scroll;

  • fixed.

We need the last value. Now our example code will look like this:

Website texture background

In the first example we used a large and beautiful desert landscape for the background. But you have to pay in full for such beauty. The weight of an image made in high quality can reach several megabytes.

This volume does not in any way affect the page loading speed of the browser with a high-speed Internet connection. But what about the mobile Internet, when using it, loading several “meters” will take a lot of time?

All these problems are solved with the help of a textured background. It uses a small image as a texture pattern. Even if it is repeated many times, the drawing is loaded only once.

17.10.2015

Not yet


Hi all!
Let's continue learning the basics of HTML.
In this lesson I will tell you and show examples how to make a background from a color or picture in an HTML page.
It's quite simple!
Let's start with color!
I think you didn’t miss it, where I told you how to change the color of the text and at the end of the article I gave codes for various colors and shades. Why did I remind you of this lesson? Yes, because there is a table with ready-made color codes, take them and practice them right away in this lesson.
So, how to make a background color in HTML...

Background color in HTML

The default background color in an HTML document is white. How to set the color as desired?
The “bgcolor” attribute will help us with this. To paint over the background, add this attribute to the “body" tag:

Or like this:

Here is the complete HTML code:

Changing the background color - website The page text will be green and the background will be black.

The result will be like this:

If you want to make a background from a picture, then add the “background” attribute to the “body” tag:

Fill in any picture where your web page is (in my example, the page is called “fon” with the extension “.gif”):

Here is the complete HTML code:

background – website

The result will be like this:

If the background image is located in the images folder or some other folder, it will look like this:

Here is the complete HTML code:

background – website Page text on a beautiful background.

That's all for today! I think the lesson was not difficult and you understood everything. If you have questions, write in the comments.
I look forward to seeing you in the next lessons.

Previous post
Next entry

In HTML, images are inserted using a tag img.
Tag img- empty, it contains attributes and does not have a closing tag.


The attribute is used to display an image on the page src. Src came from source, which means Source. The value of the src attribute is url Images.


The line above means that the image is in the same directory (folder) as the html file itself that links to this image. Let's say you have a folder html, which contains index.html with the above code and the image itself with the title image.jpg.





In this case, when opening index.html You will see this image in your browser. If you have it somewhere else (a folder higher or lower), then instead you will see either a white field or a small rectangle with a red cross (the image could not be loaded).


Images are not always located in the same directory (folder) as the file itself, so specifying paths will be described more specifically a little later.

img tag attributes

In addition to src, the img tag has other attributes that are responsible for the size of the displayed image, its caption, and so on.


src- image address
width- image width
height- image height
title- signature that appears when you hover over the image
alt- alternative text. Needed for search robot and image indexing
border- thickness of the image border. 0 - no border, 1 - thinnest border, etc.

Address of inserted image (examples)

Usually, Images are not stored in the same folder as the html file. For this purpose, a folder is created in the same directory images(or img, depending on taste and color). And all the necessary images are already placed in it. In the case of separate storage, you will need to specify a different address for the src attribute


If the file is in the folder above, then so


You can also insert an image from another site altogether, without downloading it to your folder. To do this, you must have a stable Internet connection and approximately the following code, in which you enter the address of the image on the Internet in the address:

Background image in HTML

As background image files with extensions may appear gif, jpg, jpeg And png. If the image size is smaller than the browser window, the image will automatically continue to fill the remaining background. IN body use the attribute background, in which we specify the path to the image