Css container div 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.

Читайте также:  Свойство display в CSS3

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.

Читайте также:  Filtering data with php

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.

Источник

Div Background Color – How to Change Background Color in CSS

Kolade Chris

Kolade Chris

Div Background Color – How to Change Background Color in CSS

If you’re working on a web development project, setting a nice background color can give the website a more enticing look.

To set a background color for a div or related element in CSS, you use the background-color property.

While setting this background color, your creativity is your limit as to how far you want to go.

So in this article, I’m going to show you how you can set this background color.

How to Set Background Color with Named Colors

With named colors, you can set the background color by bringing in the background-color property and assigning it a value expressed in a color name like red , green , blue , and so on.

ss-1-1

You can use the following styles to make the web page look better. Just set a width and height for the div , so the background-color can take effect:

Modern browsers recognize around 147 colors, so you’re still limited a little.

How to Set Background Color with Hexadecimal Colors

With Hexadecimal values, you can set a background color for a div or any other element with a total of 6 characters.

Hex colors start with the hash sign (#), any number from 0 to 9, and finally any letter from A to F.

The first 2 values stand for red, the next two stand for green, and the last 2 represent blue.

With hex values, you can dive deep into the color wheel and even use colors no one has ever used.

ss-2-1

How to Set Background Color with RGB Colors

RGB stands for Red Green, and Blue.

To set the background color with RGB, you specify the amount of red, green, and blue you want with numbers between 0 and 255.

Читайте также:  Command patterns in java

ss-3

RGB also has a variation called RGBA. The last A means alpha and it lets you determine how opaque you want the color to be.

The alpha takes a value from 0.0 to 1.0. 0.0 means 0% opacity, 0.5 means 50% opacity, and 1.0 means 100% opacity.

ss-4

How to Set Background Color with HSL Colors

HSL stands for Hue, Saturation, and Lightness. It is the most dynamic within the ways you can specify a background color for a div or other elements.

  • Hue represents the color wheel in 360°. 0° is red, 120° is green and 240° is blue
  • Saturation is the amount of gray in the color expressed in percentages. 0% is the shade of gray and 100% is the color itself.
  • As the name implies, lightness is the amount of darkness and lightness in the color expressed as a percentage. 0% is black and 100% is white.

ss-5

Conclusion

Since you can apply colors in 4 different ways, you must be wondering which one you should use.

When you use named colors, you’re kind of limited in how far you can go in applying different shades of colors.

Each color like red, green, blue, yellow, or any other color has a lot of variations you won’t get access to with named colors.

You can only access about 147 predefined colors recognized by browsers.
Hexadecimal colors, on the other hand, are very dynamic. They are mostly used among developers, and creativity is the limit. These hex colors allow you to use different shades of the same color.

RGB colors are as dynamic as hexadecimal colors. You can specify the amount of red, green, and blue from 0 to 255, and you can also use the added alpha value to specify the transparency of the color.

HSL is the most dynamic of all of them. You can specify exactly the color from 0 to 360 degrees in the color wheel, set the saturation and darkness as a percentage, and set the opacity to any value between 0.0 and 1.0.

So deciding which to use between named, hex, RGB and HSL colors depends on you and how creative you want to be and what your project’s needs are.

Источник

Оцените статью