Html div text background color

HTML Background Color Tutorial – How to Change a Div Background Color, Explained with Code Examples

HTML Background Color Tutorial – How to Change a Div Background Color, Explained with Code Examples

One of the most common things you may have to do as a web developer is to change the background-color of an HTML element. But it may be confusing to do if you do not understand how to use the CSS background-color property.

In the article, we discuss

  • the default background color value of an HTML element
  • how to change the background color of a div, which is a very common element
  • which parts of the CSS box model are affected by the background-color property, and
  • the different values this property can take.

Default Background Color of an Element

The default background color of a div is transparent . So if you do not specify the background-color of a div, it will display that of its parent element.

Changing the Background Color of a Div

In this example, we will change the background colors of the following divs.

 
I love HTML
I love CSS
I love JavaScript

Without any styling, this will translate to the following visually.

Screen-Shot-2020-05-08-at-12.22.48-PM

Let’s change the background color of the divs by adding styles to the classes. You can follow along by trying the examples in an HTML file.

 .div-1 < background-color: #EBEBEB; >.div-2 < background-color: #ABBAEA; >.div-3  
I love HTML
I love CSS
I love JavaScript

This will result in the following:

Screen-Shot-2020-05-08-at-11.12.29-AM-1

Cool! We have successfully changed the background color of this div. Next, let’s get to know more about this property. Let’s see how the background-color property affects parts of the CSS-box model.

Background Color and the CSS Box Model

According to the CSS box model, all HTML elements can be modeled as rectangular boxes. Every box is composed of 4 parts as shown in the diagram below.

Screen-Shot-2020-05-08-at-11.07.00-AM-1

You can read up on the box model if you are not familiar with it. The question is, which part of the box model is affected when you change the background color of a div? The simple answer is the padding area and the content area. Let’s confirm this by using an example.

 body < background-color: #ABBAEA; >.child  

This is the parent div which contains the div we are testing

This example shows that changing the background color of a div does not affect the border and margin of the div.

Screen-Shot-2020-05-08-at-11.07.10-AM-1

From the example above, we can see that the margin area and the border area are not affected by the change in background color. We can change the color of the border using the border-color property. The margin area remains transparent and reflects the background color of the parent container.

Finally, let’s discuss the values the background-color property can take.

Background-color Values

Just like the color property, the background-color property can take six different values. Let’s consider the three most common values with an example. In the example, we set the background-color of the div to red with different values.

 /* Keyword value/name of color */ .div-1 < background-color: red; >/* Hexadecimal value */ .div-2 < background-color: #FF0000; >/* RGB value */ .div-3  

The background property can take six different values.

The background property can take six different values.

The background property can take six different values.

Notice that they all result with the same background color.

Screen-Shot-2020-05-08-at-11.07.24-AM-1

Other values the background-color property can take include HSL value, special keyword values and global values. Here are examples of each of them.

/* HSL value */ background-color: hsl(0, 100%, 25%; /* Special keyword values */ background-color: currentcolor; background-color: transparent; /* Global values */ background-color: inherit; background-color: initial; background-color: unset; 

You can read more on each of these values here.

Extra Note

When setting the background color of an element, it is important to ensure that the contrast ratio of the background color and the color of the text it contains is high enough. This is to ensure that people with low vision can easily read the text.

Screen-Shot-2020-05-08-at-11.11.44-AM-1

The contrast between the background color of the first div and the color of the text is not high enough for everyone to see. So unless you are the only one using the website you are building and you have very good eyesight, you should avoid such color combinations.

The second div has a much better contrast ratio between the background color and the color of the text . Thus, it is more accessible and clearer for people to read.

Conclusion

In this article, we saw how you can change the background-color of a div. We also discussed which parts of the CSS box model are affected by the change in background-color. Finally, we discussed the values the background-color property can take.

I hope you found this article useful. Thanks for reading.

Источник

CSS background for text only & not parent span/div/body?

I’m not sure this is possible, but i was wondering if it is possible to set a background color for text only, & not the span/div/P tags that contain the text. EG This text here. each character of «this» will be a black background with white text, the ‘space’ will have the blue background the word «text» will be a black background with white text etc. Something like what deaf people see on some TV shows — captions. But I don’t want to contain each / every word with a div or span — as that will make the total HTML coding huge. From what i have gathered / googled, I can set a background for an entire ‘container’ but not just for «text» in the container. example: How do I set background color of text only in CSS? The above sets the whole h1 tag as a green background. PS — i’m only using ‘green’ as an example — but i’ve got other colours in mind, or even pictures as the background. but i want the text content to be visible.. PS, if the above can be done, is it also possible to ‘opaque’ the text-background ? so the actual / main background is partially visible, but keep the text «solid». Ive used opaque, but it makes the foreground text opaque (not kept as solid).

No. it’s not. Backgrounds apply to elements and unless each word is a separate element you cannot achieve the result I think you are looking for.

As Paulie suggested, you’ll have to give each word a span element at the least to achieve the result you want. So for every word in the example text, you’ll have to style 2 span elements in the css.

Thanks to both of you. I can set the background of a few words easy enough, but some pages contain 50, 100+, 500+ words or more so that wont work. I’ll need to think of what backgrounds to use. Thanks.

An option would be to use javascript to achieve this. Is it a certain word that you want to give a black/blue background?

Источник

Читайте также:  Index python not found
Оцените статью