×

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 Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

How to include CSS

How to include CSS CSS is included in HTML pages for formatting the documents based on the information inside a style sheet. We can add CSS in HTML documents in the following three ways: Inline...

5 minutes read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

Untitled Reusable Block

  ...

1 minute read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 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 Transition

CSS Transition The transitions in CSS are influenced, included to modify the component from one design to another, without the use of the JavaScript or Flash. We should describe two of the...

2 minutes read.

CSS Table

CSS table provides better look and feel. There are various properties of CSS table that are given below. border border-collapse padding width height text-align color background-color CSS Table Border You can set...

4 minutes read.

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS Opacity

The CSS Opacity property is used to set the clarity of the image. It is defined as degree and specify the transparency of an element. Note: Opacity value will be less...

1 minute read.

CSS Inline

What is CSS? CSS is an acronym for Cascading Style Sheets. CSS is the language that we use to style an HTML document. It specifies how HTML components should appear. CSS...

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

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 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 z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.