- 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
- How to Set Background Color with HTML and CSS
- Add the style attribute to the element
- Example of setting a background color with the style attribute:
- Result
- Add the CSS background-color property to the element
- Example of setting a background color with the CSS background-color property:
- Example of adding a background color to specific elements:
- Example of setting background colors with different color types:
- Create a background with gradients
- Example of setting a linear-gradient background:
- Example of setting a radial-gradient background:
- Create a changing background
- Example of creating a changing background:
- Which is the HTML Tag for Background Color?
- Introduction
- How to Set Background Color in HTML?
- Examples
- Example 1: Setting the Background Color of Body
- Example 2: Setting the Background Color of div Elements
- Browser Support
- Browser Support for
- Browser Support for background-color
- Learn more
- Conclusion
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.
How to Set Background Color with HTML and CSS
First thing you should know is that there are different types of HTML colors, such as Hex color codes, HTML color names, RGB and RGBa values, HSL colors, etc. To choose your preferred color use our Color Tools.
In this snippet, you can find many different ways of adding a background color. Let’s start from the easiest one.
Add the style attribute to the element
You can set a background color for an HTML document by adding style=»background-color:» to the element.
Example of setting a background color with the style attribute:
html> html> head> title>Title of the document title> head> body style="background-color:#1c87c9;"> h1>Some heading h1> p>Some paragraph for example. p> body> html>
Result
Some paragraph for example.
Add the CSS background-color property to the element
The background-color property is used to change the background color. Inserting it to the element you will have a full colored cover of the page.
Example of setting a background color with the CSS background-color property:
html> html> head> title>Title of the document title> style> body < background-color: #1c87c9; > style> head> body> body> html>
You can also add a background color to specific elements in your document. For example, let’s see how to change the color of the heading and paragraphs.
Example of adding a background color to specific elements:
html> html> head> title>Title of the document title> style> body < background-color: #e6ebef; > h2 < background-color: #8ebf42; > p < background-color: #1c87c9; > style> head> body> h2>Some heading with green background. h2> p>Some paragraph with blue background. p> body> html>
Let’s see another example, where we add a background color with a color name value to the element. We specify a RGB value for the , HSL for the , and RGBa value for the element.
Example of setting background colors with different color types:
html> html> head> title>Title of the document title> style> h1 < background-color: lightblue; > h2 < background-color: rgb(142, 191, 66); > p < background-color: hsl(300, 100%, 25%); > span < background-color: rgba(255, 127, 80, 0.58); > style> head> body> h1>Example h1> h2>Some heading with green background. h2> p>Some paragraph with blue background. p> span>Some paragraph for span> body> html>
Create a background with gradients
Gradient backgrounds let you create smooth transitions between two or more specified colors.
There are two types of gradient backgrounds: linear-gradient and radial-gradient.
In linear-gradient backgrounds, you can set a starting point for the colors. If you don’t mention a starting point, it will automatically set «top to bottom» by default.
Example of setting a linear-gradient background:
html> html> head> title>Title of the document title> style> #grad < height: 500px; background-color: blue;/* For browsers that do not support gradients */ background-image: linear-gradient(to right, #1c87c9, #8ebf42); > style> head> body> h1>Right to Left Linear Gradient background h1> div id="grad"> div> body> html>
The given example shows a linear gradient that starts from the left. It starts from blue, transitioning to green. Change it according to your requirements.
In radial gradient backgrounds, the starting point is defined by its center.
Example of setting a radial-gradient background:
html> html> head> title>Title of the document title> style> #grad < height: 500px; background-color: grey;/* For browsers that do not support gradients */ background-image: radial-gradient(#e6ebef, #1c87c9, #8ebf42); > style> head> body> h1>Radial Gradient Background h1> div id="grad"> div> body> html>
Create a changing background
You can create a background which will change its colors in the mentioned time. For that, add the animation property to the element. Use the @keyframes rule to set the background colors through which you’ll flow, as well as the length of time each color will appear on the page.
Example of creating a changing background:
html> html> head> title>Title of the document title> style> body < -webkit-animation: colorchange 20s infinite; animation: colorchange 20s infinite; > @-webkit-keyframes colorchange < 0% < background: #8ebf42; > 25% < background: #e6ebef; > 50% < background: #1c87c9; > 75% < background: #095484; > 100% < background: #d0e2bc; > > @keyframes colorchange < 0% < background: #8ebf42; > 25% < background: #e6ebef; > 50% < background: #1c87c9; > 75% < background: #095484; > 100% < background: #d0e2bc; > > style> head> body> h1>Changing Background h1> body> html>
The percentages specify the animation length mentioned in «colorchange» (e.g. 20s).
Which is the HTML Tag for Background Color?
The color combination of various elements on a webpage is really important. Companies keep it styled in a way to attract customers to their websites and keeps them hooked to it. And this is where the background color of elements came into the picture. Html provides us with a number of ways to modify the background color of any element of a webpage.
Introduction
The attribute that is used to set background color of an HTML element is bg color. Depending on the element whose background color is to be set, we use the appropriate tag. The bgcolor attribute can be used with the following tags- body, table, td, th, tr, marquee .
How to Set Background Color in HTML?
Let us see how to set the background color in HTML using the bgcolor attribute and other methods. The syntax for setting the background color using the bgcolor attribute is as follows:
Here value can be the color name, RGB number, or hex number of the color.
Let us see this with the help of an example:
The bgcolor property discussed above was used earlier but has been omitted from HTML 5 onwards. We now utilize the style attribute to set the background color of a page. In this section, we’ll go through the style attribute in detail.
The style attribute used with HTML tags specifies inline styles for an element. This attribute has a background-color property, which can be used to set the background color of the HTML element. If the style attribute is used, it will override all other global style sets. We can use an inline style attribute or use internal CSS for the same.
Inline CSS allows us to style specific HTML elements. Let us see this with the help of the following code:
Internal CSS is used to include CSS inside the head section of the document.
Let us see this with the help of the following code:
Output:
Let us now explore the various ways in which style attributes can be used to set the background color of the web page.
- By specifying the desired color — In this method, we mention the color that we want our background to be. Eg — red, blue, etc..
- By using hex color codes — The background color can be specified through hexadecimal color codes.
- By using RGB color values — The background color can be specified through RGB color values. These values refer to the amount of red, green and blue in color. Each RGB value is a number between 0 and 255 .
- By using HSL color values — HSL stands for hue, saturation, and lightness. The background color can be specified through HSL color values. Hue refers to the degree of intensity on the color wheel, where 0 is red, 240 is blue, and 120 is green. The saturation level is a percentage between 0 to 100 where 0% means grey and 100% means full color. Lightness is also a percentage between 0 to 100 , defining the intensity of the color, where 0% is black and, 100% is white, 50% is medium.
- By creating gradient background — We can set the background color of a webpage by creating a gradient background. It can be achieved by creating a background color gradient by mentioning the direction of the gradient along with the colors. Gradients can be linear, radial, or conic.
Let us see this with the help of the following example:
Output:
In the above example, we define a linear gradient that starts from the left with the color red and transits to yellow. If more control over the direction of the gradient is desired, angles can be used instead of predefined directions.
Examples
We’ll now see various examples of code snippets demonstrating how to use the background-color property of the style attribute with different HTML elements. We shall build a short and sweet webpage step by step that contains a heading and some text. We shall use imported fonts to make the texts eye-appealing.
Example 1: Setting the Background Color of Body
We have created a heading and applied our imported font. We have set the background color of the body as pink by using the style attribute inside the body tag.
Example 2: Setting the Background Color of div Elements
In this example, the style attribute has been used with the div tag, too, along with the body tag. The background color that we want the div element to be has been mentioned in the appropriate style attribute. We have set the background color of the second div element as light green and added some text to it.
Browser Support
Browser Support for
Browser Support for background-color
Learn more
Conclusion
- bgcolor attribute was used to set the background color of a page till HTML 5.
- Now, we use the style attribute for changing the background color.
- In style attribute, we have background-color property, which is used to set the background color of tags.
- Background color can be set by specifying the color, using RGB codes, hex color codes, HSL values, or by creating a color gradient.