Open In App

CSS background-color Property

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The background-color CSS property sets the background color of an element, allowing you to create a solid color backdrop. You can define the color using named colors, hexadecimal values, RGB, RGBA, HSL, or HSLA. This property applies to both the content and padding areas of the element.

By using the background-color property, you can easily enhance the visual appearance of your webpage. It provides flexibility in how you style the background, whether for a specific element or the entire page.

Syntax

element {
background-color : color | transparent| initial | inherit;
}

Default value: It has a default value i.e. transparent.

Property Values

Values

Description

color

Defines the color of text, borders, backgrounds, and other elements.

transparent

Sets the color to be fully transparent, allowing underlying content to show through.

initial

Resets the property to its default value, which varies depending on the element.

inherit

Inherits the value of the property from the parent element.

Here is the detailed explanation of each property value.

CSS background-color Property Values

1. color Value:

It defines the background color value or color codes. For example: A color name can be given as: “green” or HEX value as “#5570f0” or RGB value as “rgb(25, 255, 2)”.

Syntax:

element {
background-color: color_name;
}

Example: In this example The background-color CSS property is used to set the background color of elements. Here, it styles the body in green, <h1> in blue, and <h2> in black with contrasting text colors.

html
<!DOCTYPE html>
<html>

<head>
    <title>background-color property</title>
    <style>
        body {
            text-align: center;
            background-color: green;
        }

        h1 {
            color: white;
            background-color: blue;
        }

        h2 {
            color: white;
            background-color: black;
        }
    </style>
</head>

<body>
    <h1>Geeksforgeeks </h1>
    <h2>background-color: color_name;</h2>
</body>

</html>

Output:

2. transparent Value:

It is the default value. It specifies the transparent background-color.

Syntax

element {
background-color : transparent;
}

Example: In this example the body and headings (<h1> and <h2>) have a transparent background, allowing the page background color (if any) to show through.

html
<!DOCTYPE html>
<html>

<head>
    <title>background-color property</title>
    <style>
        body {
            background-color: transparent;
            text-align: center;
        }

        h1 {
            background-color: transparent;
        }

        h2 {
            background-color: transparent;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>background-color: transparent;</h2>
</body>

</html>

Output:

3. initial Value:

It is used to set the default value. It does not set the background color.

Syntax:

element {
background-color: initial;
}
HTML
<!DOCTYPE html>
<html>

<head>
    <title>background-color initial property</title>
    <style>
        body {
            text-align: center;
            background-color: green;
        }

        h1 {
            color: white;
            background-color: initial; /* Setting background-color to its initial value */
        }

        h2 {
            color: white;
            background-color: initial; /* Setting background-color to its initial value */
        }
    </style>
</head>

<body>
    <h1>Geeksforgeeks</h1>
    <h2>background-color: initial;</h2>
</body>

</html>

Output:

background color initial

4. Inherit:

The inherit keyword for the color property causes an element to take the color value from its parent element. This means the text, borders, backgrounds, or other elements within the element will automatically inherit the color of their parent.

Syntax:

element {
background-color: Inherit;
}

Example: In this example, the #parent div has a color: blue; property. The #child div inherits this color, so its text will also be blue.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Inherit Example</title>
    <style>
        body {
            text-align: center;
            background-color: green; /* Setting background color for body */
        }

        h1 {
            background-color: inherit; /* Inheriting background color from body */
        }

        h2 {
            background-color: inherit; /* Inheriting background color from body */
        }
    </style>
</head>

<body>
    <h1>Geeksforgeeks</h1>
    <h2>background-color: inherit;</h2>
</body>

</html>

Output:

Screenshot-2024-09-11-170934

Supported Browsers

The browser supported by css background-color Property are listed below:

Note -: These major browsers fully support the background-color property across various versions, ensuring consistent rendering of background colors on web pages.



Next Article

Similar Reads