How to change background color in CSS
How to change background color in CSS?
The background-color property in CSS would be used to specify the color of an element's background.
As the element's background, it uses solid colors. The backdrop of an element encompasses the entire size of the element, including space and border, but not margin. It applies to all HTML components.
Syntax: -
background-color: name_of_color/value_of_color;
Properties of Value: -
The value of background-color property in CSS can be set in three ways, and the following are the ways:
- By using the decimal value, that is, the RGB value of the color name.
- By using the hexadecimal value of the color name.
- By using the name of color directly, it should be a valid name of the color.
- Setting Inherit value to background-color property uses the background color from the parent element.
- Setting transparent value to background color is the default one in CSS, which shows the element's background color behind it.
We can set a background color on any element like tag, id, or class of the HTML element.
It's necessary to ensure that the contrast ratio between the background color and the color of the text placed on top of it is high enough for those with low vision to read the page's content.
The color contrast ratio is calculated by analyzing the luminosity of the text and the color values of the background.
Chrome, Firefox, Internet Explorer, Opera, Safari, etc., browsers are supported by background-color property.
One can modify the background color of an HTML element using the CSS background color attribute. Many elements, like a table, div, heading, and span element, have a background color that you can change.
Now, let’s learn how to apply background-color property by using decimal value, hexadecimal value, or color name on id, class, and HTML Element tag.
1) Background-color property by using hexadecimal value.
Example: -
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #FF4500;
}
#a
{
background-color : #DD6790;
font-size: 35px;
}
</style>
</head>
<body>
<h1>The Example of background-color property</h1>
<p id="a">The background is set with hexadecimal value. </p>
</body>
</html>
Output: -

In the above example, the background-color property is applied on the HTML element's id and the body tag; different colors are applied, but the color value is hexadecimal. In the above example, the background-color property is applied on the HTML element's id and the body tag, and a different color is applied. Still, the color value is hexadecimal.
2) Background-color using decimal value or RGB value
Example: -
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: rgb(153,102,153);
}
.a
{
background-color : rgb(255,255,204);
font-size: 35px;
}
</style>
</head>
<body>
<h1>The Example of background-color property</h1>
<p class="a">The background is set with decimal or rgb value. </p>
</body>
</html>
Output: -

In the above example, the background-color property is applied to the class and the HTML element's body tag; this time, the background-color value is applied by using a decimal value.
3) Background-color using the valid color name.
Example: -
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: coral;
}
.a
{
background-color : limegreen;
font-size: 35px;
}
</style>
</head>
<body>
<h1>The Example of background-color property</h1>
<p class="a">The background is set with valid color name </p>
</body>
</html>
Output: -
