Open In App

Set Background Color using CSS

In CSS, setting the background color involves specifying a color for the background of an element using the CSS `background-color` property. This determines the color displayed behind the content within the element on a webpage.

The background color can be changed in three ways:



1. Setting the background color using Inline CSS

Setting the background color using inline CSS involves adding the style attribute to the HTML element and specifying the background-color property with a color value, directly within the element’s opening tag.



Setting the background color using Inline CSS Syntax

<tag style = " "></tag>

Setting the background color using Inline CSS Example

Below is an example that illustrates the use of inline CSS.




<!DOCTYPE html>
<html>
 
<!--This line changes the color of background-->
 
<body style="background-color:pink">
    <h1 style="color:green;text-align:center;">
        GeeksForGeeks
    </h1>
 
    <h3 style="text-align:center;">
        How to change color of Background?
    </h3>
</body>
 
</html>

Output:

Explanation:

2. Setting the background color using Internal CSS

Setting the background color using internal CSS involves defining a style block within the HTML document’s <head> section and specifying the `background-color` property with a color value for the desired elements.

Setting the background color using Internal CSS Syntax:

<style>
/* Internal CSS starts here */
</style>

Setting the background color using Internal CSS Example:

Below is the example that illustrates the use of internal CSS.




<!DOCTYPE html>
<html>
 
<head>
    <style>
        body {
            background-color: powderblue;
        }
 
        h1 {
            color: green;
            text-align: center;
        }
 
        h3 {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
 
    <h3>
        How to change color of
        Background?(Using Internal CSS)
    </h3>
</body>
 
</html>

Output: This will be displayed when html file is opened in browser

Explanation:

3. Setting the background color using External CSS

To set the background color using external CSS, define a CSS rule for the desired element’s background color in an external CSS file, then link the CSS file to the HTML document.

Setting the background color using External CSS Syntax

body {
background-color: rgb(219, 176, 230);
}
h1 {
color: green;
text-align: center;
}
h3 {
text-align: center;
}

Setting the background color using External CSS Example:

Here is the basic implementation of Setting the background color using External CSS




<!DOCTYPE html>
<html>
 
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h3>Changing Background Color (External CSS)</h3>
</body>
 
</html>




body {
    background-color: rgb(219, 176, 230);
}
 
h1 {
    color: green;
    text-align: center;
}
 
h3 {
    text-align: center;
}

Output:

Setting the background color External CSS

Explanation:


Article Tags :