How to make a picture background in HTML and CSS. 3 simple ways

Sets the color of the back plan out of the palette. The color of the heir always coincides with the color of the ancestor element.
Greetings. In this article, I want to tell about three ways to place an image as a background of the entire page using only HTML + CSS (without using JS).

Greetings. In this article, I want to tell about three ways to place an image as a background of the entire page using only HTML + CSS (without using JS).

So, the requirements for the background image we are as follows:

  • 100% width and pages height covered
  • The background scales if necessary (Background is stretched or compressed depending on the size of the screen)
  • Paste the proportions of the pictures (Aspect Ratio)
  • Image centered on the page
  • The background does not cause scrolls
  • The solution is maximally crossbrawser
  • No other technologies are used except CSS

Sets the color of the back plan out of the palette. The color of the heir always coincides with the color of the ancestor element.

Method 1

In my opinion, this is the best way, because it is the easiest, concise and modern. It uses the CSS3 property Background-Size. which we apply to the tag HTML. . Exactly HTML. , but not Body. because Its height is greater than or equal to the height of the browser window.

Install a fixed and centered background, then adjust its size using Background-Size: Cover .

HTML { 

Background-image: url (images / background.jpg);

Background-Repeat: No-Repeat;

Background-Position: Center Center;

Background-Attachment: Fixed;

-Webkit-Background-Size: Cover;

-MOZ-Background-Size: Cover;

-O-Background-Size: Cover;

Background-Size: Cover; 
} 

Demo

This method works in

  • Chrome (any version)
  • Opera 10+.

Firefox 3.6+

Safari 3+.

IE 9+

In order for images to load quickly, place your sites only from proven hosting providers, for example, beget.com 
Users and search engines love quick sites. 

Background-image: url (images / background.jpg);

Method 2.

This method provides for the use of the IMG element, the size of which will vary depending on the size of the browser window. To stretch the image to the full screen, it needs to set min-height and width with a value of 100%. And so that the image is not compressed to the smaller size than the original, install min-width with the value of equal width of the picture.

If the window width is less than the width of the image, Media Query will be used to align the background in the center.

inherit.

<img src = "/ images / background.jpg" class = "background" />

img.background { MIN-Height: 100%; MIN-WIDTH: 640px; width: 100%; Height: AUTO;

Position: Fixed; Top: 0; left: 0;

/ * Depends on the size of the image * /

@Media Screen and (Max-Width: 640px) { img.bg { и Left: 50%; margin-left: -320px; } } } Demo

Inherits all the characteristics of the parent element

This method works in:

Any versions of good browsers (Chrome, Opera, Firefox, Safari) IE 9+ и Method 3. . Another way is as follows: fix the image <img /> to the upper left corner of the page and stretch it using the MIN-WIDTH and MIN-Height properties of 100%, while maintaining the aspect ratio. True, with this approach, the picture is not centered. But this problem is solved by wrapped the picture in <div />, which we make 2 times the size of the window. And the image itself we stretch and put in the center. <div class = "background"> <img src = "/ images / background.jpg" />

</ div> Div.Background { <div class = "background"> Position: Fixed;

Top: -50%;

Left: -50%; } width: 200%; Height: 200%; }

img { Position: Absolute; Top: 0;

left: 0; Right: 0; :

Bottom: 0;

Margin: AUTO; MIN-WIDTH: 50%; min-height: 50%; } .

This method works in good browsers and IE 8+.

I hope this information will be useful for you. Personally, I often use these methods, especially first. Surely there are other ways to put an image on the back background with CSS. If you know about them, share, please comments. Set background color and / or a picture for a page or a separate item is simple enough. The main thing is to know where and how it can be done, as well as to have a color code and / or a background picture. It is possible that from this article you will learn a lot of new things, but I created it specifically for beginners. Therefore, everything will be as short as possible and in detail simultaneously. The main thing is that you will receive not only the overall presentation and ready-made examples, but also understanding how to make a background in HTML. To set the background in HTML use the intermediate doctype And I will start with the fact that HTML5. There is no possibility to set the background for the site. This feature was decided to endure in CSS. Therefore, if you plan to use below, and you want to get a valid » (correct) Code, you need to stay at the transition type of the document. To do this, your web page should start with the following line: <! DOCTYPE HTML PUBLIC "- // W3C // DTD HTML 4.01 » Transitional // en "" http://www.w3.org/tr/html4/loose.dtd ">

Time for practical classes

How is the background on a web page? (#)If you figured it out, let's get to understand how the background is created. And the first thing here should be noted - this is the difference between

Background color

Background picture

. At the beginning goes

Background color Left: 50%; which floods all available page space or its element. The repeated on top of it background picture }

. Visually, this can be depicted as follows: Position: Absolute; Top: 0;

Background of HTML document and its elements

After disappointing the main elements of the backdrop control, as well as their attributes, proceed directly to the analysis of a specific example. In the program code shown below, I tried to enable all described elements.

The second thing you need to know is the difference between

body of the document Right: 0; :

element of the document

Body of the document

indicated in the HTML code of the web page tag

Body. which includes all the content of the web page. Obviously, the background of the document of the document cannot be transparent. If the background of the document's body is not specified, the default value specified in the browser settings is used. Page Elements are inside the tag }

. It is noteworthy that you can set the color and / or background picture using HTML to all elements of the document. For example, the background picture can be specified only for tables. By default, they usually have a transparent background. Position: Absolute; Top: 0;

Attribute BGColor to create background color

In order to ask

The document or its elements is used attribute Height: 200%; и background picture bgcolor , eg: <Body BGColor = "# EC008C">

<! - Document Body ->

</ Body>

In this case, we set the background color for the page as a whole. But an example of how to set the background color for the table, in the tag Table <Table BGColor = "# EC008C"> <tr> <td> text with background </ td> </ tr> </ table>

It is noteworthy that the background color in the table can also be specified for rows in the tag

Tr And for their cells in the tag TD.

How to find out the color code? You probably have already noticed that the color in HTML is set to special code HTML. , eg:

# EC008C.

  • . In order to find out the code you need the color you can use one of the graphics editors. For example, in Photoshop you can use "
  • Eyedropper Tool.
  • (Pipette)

To get a color from a point in the picture. Then, you need to click the resulting color on the toolbar and in the window that opens "

Color Picker. (Color selection) Copy color code. I draw your attention to the fact that this code will be without a lattice sign At the beginning, this sign will need to be added manually.

You can also use numerous online services, for example: 
Their principle of work is even easier - click the desired color and get it code. Attribute Background to create a background image As well as in the case of background color and in the case of
You need to use a special attribute, namely 
background. 
<body background = "/ 2014/06 / bg.jpg">

В HTML. In this case, we ask the background picture for the page as a whole. It is noteworthy that due to the limitations of the size of the picture, it will be repeated, for example: As you see, when repetition, the transition between the pictures is noticeable. Therefore, special pictures are often used, where this moment is taken into account. But an example of how to set a background picture for a table, in tag

<table background = "/ 2014/06 / bg.jpg"> <tr> <td> text with background </ td> </ tr> </ table> HTML. It is noteworthy that the background picture can be set only for the table as a whole and / or its separate cell. For a string it will not work. Attribute Background to create a background image .

Absolute and relative path to the background picture (Color selection) Separately worth paying attention Address background picture . In this case, the relative path to the picture is used, i.e. The address is indicated on the location of the image file to the web page file. This option can not be called particularly successful. It is best to use the absolute path to the picture, i.e.

its full URL <body background = "/ images / bg.jpg"> .

In this case, you will not have concomitant problems. For more about it you can read here.

Let's summarize 
Using attributes morally outdated, because for the validity of the HTML code you will have to use the transitional DoCtype.

. To set the background color in HTML, uses special codes that you can find out in a graphic editor or using special online services. The background picture is duplicated in the tails of the area allotted and lies on top of the background color. To specify a background picture, it is better to use its full URL. I have everything on this. Thanks for attention. Good luck!

Short link: http://goo.gl/03tsnz How to make a background for the site? Any room will look much better if her floor rises dear Persian carpet. So the worse your site? Maybe it's time and his floor " set
  • »Expensive elegant handmade palace. We will understand more about how to make a background for the site:
  • It happens that the old design of the site has already come. And I want something new and tasty. And the new design will be such if you cook it with your own hands.
  • But to change completely all the design of the resource yourself - the thing is ungrateful. Yes, and not everyone has a thing about this. "
  • sharpened

" hands. Therefore, it is easiest to refresh the old template, changing the color of the background of the resource or its background image.

There are several ways to change the background on the site. This uses opportunities. 
CSS. or . But many of the properties for working with the background have the same name and methodology of application in these web technologies.
  • As a background, you can use multiple items: Color; ;
  • Background picture;
  • Textural image. We will deal with the use of each of them in more detail. In order to set the color of the back background for the site, the property is used.

background-color.

Style attribute 

Style. . That is, to set the basic color for the web page, you need to register it inside the <Body> tag. For example: <Body Style = "Background-color: # 55D52B"> <p> Background of the site # 55D52B </ P> </ Body>

In addition to hexadecimal color code, the value is supported in the keyword format or

RGB. 

. Examples:

<Body Style = "Background-color: RGB (51,255,153)"> <p> Background of the RGB website (23,113,44) </ p> </ body> <Body Style = "Background-Color: Green"> <p> Background of the Green website </ P> </ Body> Setting the background color using keywords has a number of restrictions compared to the remaining two ways.

Total 16 keywords are supported for setting color. Here are some of them:

White, Red, Blue, Black, Yellow other. Therefore, in order to set the background for the site

it is better to use hexadecimal format or 

In addition to selecting color, other settings are available. If property Set value Transparent

The background of the page will become transparent. This value is assigned to this property by default. Now consider the possibility of a hypertext language to install the background pattern for the site. It is possible to make it using the property. background-image.

Example: 

<Body Style = "Background-image: URL (img_1809.jpg)"> </ Body>

As can be seen from the code, the image binding occurs through the path

URL

Posted in brackets. But not all pictures turn out to be so big in order to fill the entire area of ​​the screen with their sizes. Let's see how the smaller figure will be displayed.

Suppose that we are developing a site about poetry, and as a substrate you need to use the image of Pegasus. The winged horse will personify the freedom of creative thought of the poet!

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

Probably four smiling horses with wings of poets will be too much for inspiration. Therefore, prohibit the cloning of our Pegasus. Do it using the property

background-repeat. . Possible values: repeat-x - repetition of the background image horizontally;

So, we facilitate the task to set the back background from two pictures: animation and regular image. In this case, the Gif animation will be fixed, and the drawing is scrolled along with the contents of the page.

REPEAT-Y - vertically;

REPEAT - on both axes;

NO-REPEAT - repetition is prohibited.

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

<Body Style = "Background-image: url (3.jpg); background-repeat: no-repeat"> </ body>

But, of course, it is better if our letow is located in the middle of the screen. Property

Background-Position Just is intended to position the background pattern on the page. Set the location coordinates in several ways:
. That is, to set the basic color for the web page, you need to register it inside the <Body> tag. For example: Keyword (
<body background = "/ images / bg.jpg"> TOP, BOTTOM, CENTER, LEFT, RIGHT)
or Percentages - the countdown begins from the upper left corner;
Any room will look much better if her floor rises dear Persian carpet. So the worse your site? Maybe it's time and his floor " In units of measure (
(Color selection) Pixels
). We use the easiest option for centering:

<Body Style = "Background-image: url (3.jpg); background-repeat: no-repeat; background-position: top center"> </ body>

It happens that you need to fix the position of the drawing when scrolling. Therefore, before making a picture of the site, use a special property.

background-attachment.

. Valid values: <ul> <li> scroll; </ li> <li> fixed. </ Li> </ ul>

We need the last meaning. Now the code of our example will look like this: [/ HTML] 
<body style = "background-image: url (3.jpg); background-repeat: no-repeat; background-position: top center; background-attachment: fixed"> </ body> 

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

This volume does not affect the download speed of the browser page with high-speed connection with the Internet. But what about the mobile Internet, when using which the download of several "

meters

"Will take a lot of time?

"Will take a lot of time? All these problems are solved using a texture background. It uses a small image as a pattern of texture. Even under the condition of its multiple repetition, the pattern is loaded only once.

To create a dark background for the site go to Body. Photoshop. Body. , Create an image in the form of a strip of a long 1200 pixel, and a width of 15 pixels. Then we use a simple black and white gradient and connect the resulting texture to the site page:

<Body Style = "Background-image: URL (1.PNG); Color: RGB (0,51,204)"> </ Body>

For clarity, we added text and set it color using the property. Body. Color Body. . That's what happened:

  • Height: 200%; All properties described above are also applicable for cascading style sheets. Create a background of the site
  • background picture CSS.
  • By rewriting the code of one of our previous examples: <Head> <title> Document Untitled </ title> <style type = "text / css"> body {background-image: url (3.jpg); Background-Repeat: no-repeat; Background-Position: top center; background-attachment: fixed;} </ style> </ head> <body> </ body>
  • The result will be similar. Well, we looked at all the options, how to change the background on the site. Now it remains only to create a drawing of the future carpet and spread it on the pages of your resource. But this is your hand.
  • Good day to everyone to learn and learn something new! Have you ever paid attention to the appearance of sites when developing the creators have been lazy to arrange the background of pages? And I turned. It looks slaughter. Often, due to the lack of usual separations between different types of information, it is mixed and simply disappears the desire to further look at such a web resource. So that such trouble happened to your project, I decided to write an article on the topic: "How to make a background of pages in HTML." After reading the publication, you will learn, with what tools you can set the background design, how to make a background fixed or changing and much more, which will help make your website attractive. Now let's start!
  • Basic tools for setting the background web pages To set the background image, web-language developers have provided an attribute Background. It is available in both HTML and CSS.
  • In the markup language, this is an attribute of the Body tag, and in the style tables - a universal property that allows you to set up to 5 the characteristics of the rear plan at the same time. Background is a fairly flexible element that can be used to task the background as a color, color picture or even animation. So, to install the background image through the unit HTML <Body> it is enough to write such a code:
  • <Body Background = "File Address"> ... </ Body> And instead of the words "file address" insert the path to the picture.
  • However, notice! If in the form of the background you want to see a monochon canvas specified from the color palette, then this is done using the BGColor attribute. For example, <body bgcolor = "# 000000"> ... </ Body>, we asked a black background for our site.
  • Colors in CSS and HTML are set either by an English word (for example, Red), or a special code that consists of a sign # and six characters after it (for example, # FFDAB9). When typing a second option in specialized software products for developers, the palette will automatically appear in front of you. If you just started learning these web languages, then the color code can be spacked on the Internet.

Background as property in cascading style tables Height: 200%; :

It is set or in a separate file with CSS styles, or in the <Style> element. With this property, you can install background patterns or color, specify the starting location on the page, set recurring and fixed images. For greater clarity, I made definitions in the table. 

Parameters Purpose

Used to install a fixed or scrolled image along with the content of the site. Happens fixed, scroll and inherit

Sets the background image. It may be indicated simultaneously with the color. Then the latter will be displayed until the picture is completely loaded.

Sets the initial location of the object for the previous parameter. Horizontally indicates Right, Left and Center, and vertically - Bottom, Top and Center. In addition, you can inherit position (inherit), set in percentage, pixels and other dimensions 

Apply when using the Background-Image attribute. Regulates how the image will be repeated. You can specify 2 values: for horizontal and vertical. There are characteristics: No-repeat, Repeat, Repeat-X, Repeat-Y, Inherit, Space

Добавить комментарий