CSS Introduction

CSS Tutorial What is CSS CSS Syntax CSS Selector How to include CSS CSS Comments

CSS Attributes

CSS Background CSS Border CSS Display CSS Float CSS Font CSS Color CSS Hover CSS Important CSS Line-height CSS Margin CSS Opacity CSS Filter CSS Images CSS Overflow CSS Padding CSS Position CSS Vertical align CSS White space CSS Width Word-wrap in CSS Box-shadow in CSS Text-transform in CSS CSS Outline CSS Visibility CSS Counters CSS Clear fix CSS Icons CSS Justify-content Text-decoration in CSS CSS Lists CSS nth selector CSS Sticky CSS Background-clip CSS Checkbox-style CSS Letter-spacing CSS Navigation bar CSS Overlay CSS Root CSS Specificity CSS Text-indent CSS Text-stroke CSS Zoom CSS Order CSS Descendent selector CSS Clip CSS calc() CSS Background-blend-mode CSS radio-button CSS Superscript and subscript CSS Text-effects CSS Text-align CSS Variables CSS Page-break-before CSS Page-break-inside CSS Page-break-after CSS Content property CSS Word-spacing CSS Animation CSS @keyframes rules CSS Pseudo-classes CSS Pseudo-elements CSS Radial-gradient CSS Translate CSS Gradients CSS z-index CSS Loaders CSS Units CSS Transition CSS Masking CSS Arrow CSS Pagination

Misc

What is Bootstrap CSS What is CSS used for Difference between HTML and CSS How to center a table in CSS What is a CSS File How to center a button in CSS How to change background color in CSS How to change the font in CSS How to change font size in CSS How to resize an image in CSS How to get rid of bullet pioints in CSS Is CSS a programming language Grid Vs Flexbox in CSS How to edit CSS in WordPress How to use google fonts in CSS Create a 3D text effect using HTML and CSS How to create blinking text using CSS Hover condition for a:before and a:after in CSS Difference between CSS Grid and CSS Flexbox

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:

  1. By using the decimal value, that is, the RGB value of the color name.
  2. By using the hexadecimal value of the color name.
  3. By using the name of color directly, it should be a valid name of the color.
  4. Setting Inherit value to background-color property uses the background color from the parent element.
  5. 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: -

How to change background color in CSS

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: -

How to change background color in CSS

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: -

How to change background color in CSS



ADVERTISEMENT
ADVERTISEMENT