Contacts

html tags background fill. How to set the background and text color on a web page? Changing the background color with HTML

From the author: I welcome everyone. Background colors and images in web design play a huge role, as they allow you to more attractively design any element. How to make a background in html, we will look today.

Is it possible to get by with html when setting the background?

I'll tell you right now that it's not. In general, html is not designed to design web pages. It's just very inconvenient. For example, there is a bgcolor attribute that can be used to set the background color, but this is very inconvenient.

Accordingly, we will use cascading style sheets (css). There are many more options for setting the background. Today we will analyze the most basic ones.

How to set background with css?

So, first of all, you need to decide which element you want to set the background to. That is, we need to find the selector to which we will write the rule. For example, if you need to set the background for the entire page as a whole, then you can do this through the body selector, for all blocks through the div selector. Well, etc. The background can and should be attached 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 and not an image), the background-color property is used. 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 the hexadecimal code. To select colors, you can use a program in which you can see the color code. For example photoshop, paint or some online tool. Accordingly, for example, I will prescribe a common background for the entire web page.

body( background-color: #D4E6B3; )

This code needs to be inserted in the head section. It is important that the files are in the same folder.

Picture as background

For the picture, I will use a small html language icon:

Let's create an empty block with an ID:

< div id = "bg" > < / div >

Let's give it explicit dimensions and a 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 have used a new property called background-image. It is intended just for inserting an image as a background to an html element. Let's see what happened:

To specify an image, you must write the keyword url after the colon, and then specify 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 if you wrote the name of the image correctly, if the path and extension are set correctly. These are the most common reasons why the background is simply not displayed, because the browser cannot find the image.

But did you notice one feature? The browser took and multiplied the picture throughout the block. So, just so you know, this is the default behavior of background images - they repeat vertically and horizontally as long as they can fit into the block. By this behavior you can easily manage. To do this, use the background-repeat property, which has 4 main values:

Repeat - the 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 will write like this:

background-repeat: repeat-x;

background - repeat : repeat - x ;

Now repeat only horizontally. If you write no-repeat, then there would be only one picture.

Great, we can finish with this, since these are the basic features of working with the background, but I will show you 2 more properties that allow you to get more control options.

With repetition, coders used to achieve the point of creating 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 that in the end a single solid background was obtained. By the way, this approach is worth using now if you want to use a seamless texture on your site as a background. The gradient today can already be implemented using css3 methods, we will definitely talk about this later.

Background position

By default, the background image, if it is not set to repeat, will be in the upper left corner of its block. But the position can be easily changed with the background-position property.

You can ask it in different ways. One option is to simply specify the sides in which the image should be:

background-position: right top;

background - position : right top ;

That is, everything remained the same vertically: the background image is located on top, but horizontally we changed the side to right, that is, the right one. Another way to set the position is as a percentage. In this case, the countdown starts in any case from the upper left corner. 100% - the whole block. Thus, to place the image exactly in the center, we write it like this:

background-position: 50% 50%;

background-position: 50% 50%;

One important thing to remember about positioning is that the first parameter is always the horizontal position, and the second parameter is the vertical position. So, if you see a value of 80% 20%, then you can immediately conclude that the background image will be strongly shifted to the right, but it will not 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 also be necessary.

Abbreviated 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 set, and the repetition, and the position. Of course, repetition and position is not always necessary, but in any case, it would be more correct to use the property shorthand. She 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 write abbreviated always, even if you need to specify only a color or a picture.

Controlling the size of the background image

Our current image doesn't lend itself very well to the next trick, so I'll take a different one. In size, let it be like a block or larger than it. 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 size of the block.

What can be done in such a 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 at the moment there is no time and opportunity to reduce it. The problem can be solved with the help of 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 will give it a background size:

background-size: 80% 50%;

background-size: 80% 50%;

Again, the first parameter is the horizontal size, the second is the vertical size. We see that everything has been applied correctly - the photo has become 80% of the block width in width and half in height. Here you only need to make one clarification - by setting the size as a percentage, you can affect the proportions of the picture. So be careful if you want not to break the proportions.

As you might guess, the background size can also be specified in pixels. There are also two value keywords that can also be used:

Cover - the image will be scaled so that at least one side of it completely fills the block.

Contain - scales 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.

Also, you should 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 sized, say, 1280 by 200, and do not set background-size for it, then screens with a width larger than an empty space appears, the picture 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 trick to use, but now you will run into the problem that users with a screen width of 1920 pixels may see a sub-optimal image quality.

I remind you that it will stretch to the 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 cut off slowly, but at the same time, with a competent selection of the background image, this will not affect the appearance of the site.

All in all, this is just 1 example of how to apply the knowledge you have gained in this article to real-life layouts.

translucent background with 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'll set the background of the entire page to the image we used earlier in the examples. For the block with the bg identifier, 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 in css for setting colors. 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 full transparency.

61.5K

Any room will look much better with an expensive Persian rug on the floor. So what's wrong with your site? 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 the site:

Background for the site

It happens that the old site design is already boring. And I want something new and tasty. And the new design will be such if you cook it yourself.

But changing the entire design of the resource on your own is a thankless thing. And not everyone has their hands properly “sharpened” for this business. Therefore, it's easiest to refresh an old template by changing the resource's background color or background image.

There are several ways to change the background on a website. For this, the possibilities of CSS or html are used. But many of the background properties have the same name and usage in these web technologies.

HTML background basics

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 register it inside the tag . For example:

Website Background #55D52B




In addition to the hexadecimal color code, a keyword or RGB value 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.

In html, only 16 color keywords are supported. 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's better to use hex or RGB.

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

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




As you can see from the code, the image is bound via the url path given in parentheses. But not all pictures turn out to be so large that they fill the entire screen area with their size. Let's see how the smaller image will be displayed.

Let's assume that we are developing a site about poetry, and we want to use an image of Pegasus as a background. The winged horse will personify the freedom of the poet's creative thought!


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

Probably, four smiling horses with wings will be too much for poets for inspiration. Therefore, we prohibit the cloning of our Pegasus. We do this with 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 these 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 is better if our flyer is located in the middle of the screen. The background-position property is just for positioning the background image on the page. You can set location coordinates in several ways:
  • keyword ( top , bottom , center, left, right);
  • Percentage - the countdown starts from the upper left corner;
  • In units of measurement (pixels).

Let's use the simplest option for centering:



It happens that you need to fix the position of the picture when scrolling. Therefore, before making an image 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 for such beauty you have to pay in full. The weight of a high quality image can reach several megabytes.

You can completely customize your blog or website very easily. This article shows you how easy it is to change the font in the header and the background image of a page.

Bloggers and site owners usually change the background to make their site more visually appealing to visitors. However, a background image that is added incorrectly can confuse the visitor, and even cause distrust of your site or blog and unwillingness to visit it again. To add the background correctly, it's important to use the right HTML codes.

There are just a few easy ways to change the look of your existing site. Many users do not even realize how easy it is.

How to customize the font

First of all, you will have to add four meta tags anywhere after< head >. You can simply copy the symbols below and paste them into your resource, after removing the spaces.

How to customize the background color

Open the page in the HTML of your blog or website. When you edit a page in Dreamweaver, you can open it to make your work easier. If your site is built using a website builder, some services allow you to change your HTML settings online by going to the Design page and selecting the Edit HTML tab. Either way, you should be able to access the HTML codes for your blog or website. The setup process can vary greatly depending on what services are available to you and what engine you are using.

It's not worth trying to look for a separate image with a different color if all you have to do is change the background color. Instead, you can simply edit the HTML and change the existing color to whatever you want to keep.

You can easily find an HTML color chart in specialized publications. Each color is displayed in the markup as a six-digit code. For example, a white background is designated as #FFFFFF.

Thus, find the color code that you want to see on your site as the background color. In markup it would look like this:

background-color:#XXXXXX; (

After you save the background color in HTML, you will see that the appearance of your page has changed.

Adding a background image

Select the image you want to add as background. There are many sites that allow you to download such backgrounds. In addition, you can use a picture that is already saved on your computer.

Upload the image to the Internet. Many allow you to download images for free. Get a URL to your background image. To do this, you need to open the image in your browser and copy the URL.

Paste the code to add an image as a background. In HTML it looks like this:

background-image: url(image URL);

You need to add the code where the page body starts in the HTML. Save changes after editing and load your site. You will be able to see that the image has completely replaced the background color.

However, remember that images that are too large can take a very long time to load, which will be unpleasant for many visitors. For this reason, try to choose small images for this purpose.

In the previous chapters, we compiled the main template of HTML documents, decided what HTML tags are, learned how to save and edit html files, and in this chapter we Let's get acquainted with tag attributes and change the color of the text and background of the page.

Let's open our index.html, for editing: My first site I managed!!!

And let's change it a little, add attributes: My first site text="#000000" bgcolor="#ffffff"> I did it!!!

Attribute is a tag parameter that is inserted into the tag as a "pair" (parameter name + parameter value).
Tag attribute values ​​are inserted:
1) text=#000000 without quotes;
2) text="#000000" in single quotes;
3) text="#000000" in double quotes.
Any of these options are correct, but if you care about code ethics, it's better to use double quotes, as in my example above.

Attribute "text" controls the color of the text on the entire page, and "bgcolor" controls the background color of the page.

Now I propose to talk about colors for HTML documents. The color is set:
1) text="gold"- words in English, for example: gold (gold), red (red), green (green) and so on ...
But the color can only consist of one word, for example "red", but if you write "green-red", then the browser will not understand this and will simply ignore it.
2) text="#000000"- RBG (red green blue) color scheme. "#" this character says that it is a color number, the first two characters (zero in my example) say how much we took the "red" color, the second green and the last two blue.
Each color is set from 00 to FF (0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F), for example #000000-black, #ffffff-white , #ff0000-red , #00ff00- green #0000ff-blue

As a bonus from the site, I suggest you download the color palette (2kB), which is shown in the picture above. You need to unzip the archive to run the file color.html, then on a large field, put the mouse pointer on the color you need, which will be shown in a small window, and if you click the mouse at this time, the color code will appear in a small window on the right.

Now back to our file index.html and save it as tsvet.html, now let's see what happened. What was it like and what is it like? And you're absolutely right, as white is for the background and black for the text is the default color. To notice the difference, let's change the attribute values:

My first site text=gold" bgcolor="#0900b8"> I did it!!!

Save and Watch (opens in a new tab)

There are other ways to set text in html files, but these two are considered the main ones.

In the next chapter, we will learn how to manage text with BR tags, how to wrap text on another line.

Lesson 7. Text and background color in HTML.

Date: 2008-12-05

How to set the background and text color on a web page?

By default, the color of text and, in general, any font on web pages is always black (#000000 ). But we can always set absolutely any color that we like and looks decent or more suitable for the design of a particular site.

Set the text color

In HTML, the color of text, font, background, and other elements can be set in two ways:

1. Method 1. In pair tags attribute is written with the name of the desired color. The name of the color is indicated in English. For example, the title color on our web page can be set in this way:


Comments on this article (lesson):

Andrey! What a wonderful site you have! I have been using it regularly for the second week now: I have been studying your lessons and video tutorials; I read your literature and download programs; I'm taking my first steps in web programming! And, most importantly, I can do it!!! And I'm not a physicist at all, but a lyricist! And everyone who visits my literary site can be convinced of this: "POEMS BY OLEG GUZ" My details: e-mail: STIH [email protected] website: http://sites.google.com/site/stihiolegguz/

check the code carefully

I've tried changing the background color and it doesn't work! How to change the background color?



Liked the article? Share it