

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.

the different values this property can take.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.
