×

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

Related Topics

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

CSS: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

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

3 minutes read.

Css Box Model

Introduction The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to...

5 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

CSS Text-stroke

Text-stroke Text-stroke CSS property inserts the stroke to any text and gives a decoration option to them also. It describes the stroke’s width and color for various text characters. It is...

2 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.