Luckily, I brought along my Game Boy and collection of Pokemon games.Īs I slid my trusty Blue version into my Game Boy, I thought to myself, instead of battling Gary Oak for the thousandth time, maybe I can do a little computer vision. A baby is screaming right next to me while the accompanying mother looks forlornly out the window, clearly questioning whether or not having a child was the right life decision.Īnd to top it all off, the Wi-Fi doesn’t work. Riding the Amtrak 158 train, coming home after a long business trip. Finally, we discussed the values the background-color property can take. We also discussed which parts of the CSS box model are affected by the change in background-color. In this article, we saw how you can change the background-color of a div. Thus, it is more accessible and clearer for people to read.
The second div has a much better contrast ratio between the background color and the color of the text. 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 contrast between the background color of the first div and the color of the text is not high enough for everyone to see. This is to ensure that people with low vision can easily read the text. 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. You can read more on each of these values here. Other values the background-color property can take include HSL value, special keyword values and global values. Notice that they all result with the same background color. The background property can take six different values. In the example, we set the background-color of the div to red with different values. Let's consider the three most common values with an example. Just like the color property, the background-color property can take six different values. The margin area remains transparent and reflects the background color of the parent container.įinally, let's discuss the values the background-color property can take. We can change the color of the border using the border-color property. This example shows that changing the background color of a div does not affect the border and margin of the div.įrom the example above, we can see that the margin area and the border area are not affected by the change in background color. This is the parent div which contains the div we are testing 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. You can read up on the box model if you are not familiar with it. Every box is composed of 4 parts as shown in the diagram below. Background Color and the CSS Box ModelĪccording to the CSS box model, all HTML elements can be modeled as rectangular boxes. Let's see how the background-color property affects parts of the CSS-box model. Next, let's get to know more about this property.
Ĭool! We have successfully changed the background color of this div. You can follow along by trying the examples in an HTML file. Let's change the background color of the divs by adding styles to the classes.
Without any styling, this will translate to the following visually. In this example, we will change the background colors of the following divs. So if you do not specify the background-color of a div, it will display that of its parent element. The default background color of a div is transparent.
which parts of the CSS box model are affected by the background-color property, and.
how to change the background color of a div, which is a very common element.the default background color value of an HTML element.But it may be confusing to do if you do not understand how to use the CSS background-color property. 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.