- Change Background Color In HTML Using Inline CSS
- How to create colors in HTML
- How to change background color in HTML with inline CSS
- How to change background color in HTML (another example)
- Why Did I Write This Tutorial?
- How to change HTML color using inline CSS (video tutorial)
- background-color
- Try it
- Syntax
- Values
- Accessibility concerns
- Formal definition
- Formal syntax
- Examples
- HTML
- CSS
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- HTML Background Color Tutorial – How to Change a Div Background Color, Explained with Code Examples
- Default Background Color of an Element
- Changing the Background Color of a Div
- Background Color and the CSS Box Model
- Background-color Values
- Extra Note
- Conclusion
Change Background Color In HTML Using Inline CSS
First, we need to figure out how to create an HTML color value.
How to create colors in HTML
Before adding your background color in style attribute of your HTML element, let’s take a look at how to actually create HTML colors (there are different ways to specify the same color).
Please watch this video to support our content network 🙂
There are several ways to create colors in HTML. Here is a list of some of the most common ways:
Use a named color. HTML supports a set of predefined color names, such as red, blue, green, black, and white. These named colors can be used directly in HTML without the need for any additional code.
Use a hexadecimal color code. Hexadecimal color codes are six-digit strings that represent the red, green, and blue components of a color.
The first two digits represent the red component, the middle two digits represent the green component, and the last two digits represent the blue component.
Hexadecimal color codes are preceded by a # symbol, and they are commonly used in HTML and CSS.
Use a RGB/RGBA color value. RGB/RGBA color values are numerical representations of colors that use the red, green, and blue components of a color.
RGB values are three-digit numbers that specify the intensity of the red, green, and blue components of a color on a scale of 0 to 255.
RGBA values are similar to RGB values, but they also include an alpha channel value that specifies the transparency of the color. RGB/RGBA values are commonly used in HTML and CSS.
Note that there are many other ways to create colors in HTML, and the methods listed above are just a few examples.
Additionally, the specific syntax and values may vary depending on the version of HTML being used, and some of these methods may not be supported in older browsers.
For more information, you can refer to the documentation for the HTML and CSS color-related properties and functions.
How to change background color in HTML with inline CSS
The simplest way to change color in HTML is to use inline CSS. The following example demonstrates that. Just use style attribute and type CSS in quotes:
This is called inline CSS: it’s typed directly into the HTML element. But you can also use internal CSS. This is when you include your CSS code separately in a pair of tags:
Somewhere in tag include tags and type your CSS commands between them. In this case background:black will be applied to all DIV elements in your HTML page.
How to change background color in HTML (another example)
To change the background color of an HTML element, you can use the style attribute and the background-color CSS property.
The style attribute allows you to specify inline styles for an HTML element, and the background-color property sets the background color of an element.
Here is an example of how to use the style attribute and the background-color property to change the background color of a div element:
In this example, the style attribute is added to the div element, and the background-color property is set to red. This sets the background color of the div to red.
Note that you can specify the background color using a named color (e.g. red), a hexadecimal color code (e.g. #ff0000 ), or a RGB/RGBA color value (e.g. rgb(255, 0, 0) or rgba(255, 0, 0, 1) ).
Additionally, you can use the style attribute and the background-color property in other HTML elements, such as body, p, h1, and more.
While using the style attribute is a simple and quick way to change the background color of an HTML element, it is generally considered best practice to use an external CSS file (or at least included inside style tags) to separate the presentation of an HTML document from its content.
This allows you to easily reuse styles across multiple elements and pages, and it makes it easier to maintain and update the styles of your HTML documents. For more information, you can refer to the documentation for the style attribute and the background-color property.
Why Did I Write This Tutorial?
Changing background color using CSS is one of the first things you might want to learn.
What if you’re completely new to HTML? At this point you barely know what a CSS property is. Every day someone new is making decision to learn HTML from scratch. Plus I simply get bored on writing about advanced subjects.
What if, for some reason, you need to change background color in some website editor, game or an app with built-in HTML. And you need a quick solution for changing the background of an HTML element.
This might mean you’ll never have to touch CSS ever again in your life. Just looking for a quick way to help you figure out how to do it.
I know this is is an incredibly simple tutorial, but I decided to write it anyway. Changing background color of an HTML element is probably one of the most simplest things you’ll ever learn as a web developer.
Still people, especially beginners, search for it online. And perhaps we already have a flood of tutorials about «how to change background color» out there. But you can also say there are advanced tutorials on important subjects as well that are hard to find or not many get to read (flooded by other content.)
How to change HTML color using inline CSS (video tutorial)
I’ve just published html — how to change background color tutorial on my YouTube coding channel. The idea came to me after starting to record a new series of tutorials covering the basics of HTML & CSS. So why not also document it on one of my article sites for extra SEO boost? So here it goes.
Please watch this video to support our content network 🙂
background-color
The background-color CSS property sets the background color of an element.
Try it
Syntax
/* Keyword values */ background-color: red; background-color: indigo; /* Hexadecimal value */ background-color: #bbff00; /* Fully opaque */ background-color: #bf0; /* Fully opaque shorthand */ background-color: #11ffee00; /* Fully transparent */ background-color: #1fe0; /* Fully transparent shorthand */ background-color: #11ffeeff; /* Fully opaque */ background-color: #1fef; /* Fully opaque shorthand */ /* RGB value */ background-color: rgb(255 255 128); /* Fully opaque */ background-color: rgb(117 190 218 / 0.5); /* 50% transparent */ /* HSL value */ background-color: hsl(50 33% 25%); /* Fully opaque */ background-color: hsl(50 33% 25% / 0.75); /* 75% opaque, i.e. 25% transparent */ /* Special keyword values */ background-color: currentcolor; background-color: transparent; /* Global values */ background-color: inherit; background-color: initial; background-color: revert; background-color: revert-layer; background-color: unset;
The background-color property is specified as a single value.
Values
The uniform color of the background. It is rendered behind any background-image that is specified, although the color will still be visible through any transparency in the image.
Accessibility concerns
It is important to ensure that the contrast ratio between the background color and the color of the text placed over it is high enough that people experiencing low vision conditions will be able to read the content of the page.
Color contrast ratio is determined by comparing the luminance of the text and background color values. In order to meet current Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for text content and 3:1 for larger text such as headings. Large text is defined as 18.66px and bold or larger, or 24px or larger.
Formal definition
Formal syntax
Examples
HTML
div class="exampleone">Lorem ipsum dolor sit amet, consectetuerdiv> div class="exampletwo">Lorem ipsum dolor sit amet, consectetuerdiv> div class="examplethree">Lorem ipsum dolor sit amet, consectetuerdiv>
CSS
.exampleone background-color: transparent; > .exampletwo background-color: rgb(153, 102, 153); color: rgb(255, 255, 204); > .examplethree background-color: #777799; color: #ffffff; >
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
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.
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:
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.
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.
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.
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.
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.