Set Background Color using CSS
Last Updated :
24 Jun, 2024
Setting the background color in CSS involves using the background-color property to define the color displayed behind the content within an HTML element. This can be achieved through three primary methods: inline CSS, internal CSS, and external CSS. Each method offers different advantages depending on the use case.
The background color can be changed in three ways:
1. Setting the background color using Inline CSS
Inline CSS involves adding the style attribute directly within the HTML element's opening tag. This method is straightforward for quick, specific changes but can make your HTML code harder to manage if overused.
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.
HTML
<!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:
- In this example by using Inline CSS sets the background color of the <body> element to pink using the style attribute.
- Inline CSS also styles headings, making them green and center-aligned.
- Renders headings with "GeeksForGeeks" and "How to change color of Background?" in the specified styles.
2. Setting the background color using Internal CSS
Internal CSS involves defining a <style> block within the HTML document's <head> section. This method keeps styles centralized within the document, making it easier to manage than inline styles for larger projects.
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.
HTML
<!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:
- In the above example Internal CSS sets the background to powder blue for the entire page.
- <h1> text is green and center-aligned; <h3> is center-aligned.
- Output displays "GeeksForGeeks" in <h1> and "How to change color of Background? (Using Internal CSS)" in <h3>.
3. Setting the background color using External CSS
External CSS involves defining styles in a separate CSS file and linking it to the HTML document using the <link> tag. This method is highly effective for large projects, as it separates style from content and allows for easy reuse across multiple pages.
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
HTML
<!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>
CSS
body {
background-color: rgb(219, 176, 230);
}
h1 {
color: green;
text-align: center;
}
h3 {
text-align: center;
}
Output:
Setting the background color External CSSExplanation:
In this example we links an external stylesheet (styles.css) using the <link> tag in the <head> section.
- CSS in styles.css sets the background to rgb(219, 176, 230); for the entire page.
- CSS defines styles for <h1> (green color, center-aligned) and <h3> (center-aligned).
- Renders "GeeksForGeeks" in <h1> and "Changing Background Color (External CSS)" in <h3> with specified styles.
Understanding how to set background colors in CSS using inline, internal, and external methods is crucial for effective web design. Inline CSS is useful for quick changes, internal CSS for centralized control within a single document, and external CSS for maintaining consistency and reusability across multiple documents.