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.
Similar Reads
Tailwind CSS Background Color
This class accepts more than one value in tailwind CSS in which all the properties are covered in class form. It is the alternative to the CSS background-color property. This class is used to specify the background color of an element. The background covers the total size of the element with padding
2 min read
CSS background-color Property
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
4 min read
Is background-color:none valid CSS ?
CSS "background-color:none" is valid. But it is better to specify it as "transparent" instead of "none". The CSS background-color property is used to specify the background color of an element. The background covers the total size of the element with padding and border but excluding margin. It makes
1 min read
Spectre Background Colors
Spectre colors are one of the most useful utilities, there are two types of things where we can use colors. Like we can set colors for the text and we can set colors for the background. In this article, we will learn about the various classes available for the background, along with understanding ho
2 min read
Primer CSS Colors Background Color
Primer CSS is a free open-source CSS framework that is built upon GitHub design system to provide support to the broad spectrum of GitHub websites. It creates the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady
4 min read
HTML Background Color
HTML Background Color enables the inclusion of the background color on the web page, which can be accomplished with the help of the background-color property used with HTML elements. It applies to the total size of the element specified, but the margin is not included. The property has the default v
4 min read
How to Set Transparent Background Color in CSS ?
Creating a simple transparent background involves styling HTML elements using CSS to achieve the desired visual effect. We can use different approaches to achieve this effect including, RGBA color values and the Opacity property. Below are the approaches to set transparent background color in CSS: T
2 min read
How to Change the Background Color of Table using CSS?
Changing the background color of a table using CSS can help improve the visual appearance of a website or web application. we will learn how to change the background color of the table using CSS with different approaches. These are the following approaches: Table of Content Using Inline CSSUsing Int
2 min read
Change Background color using onmouseover property
In this article, we are going to make a background color changer using the onmouseover property. we will make different buttons for the color and on hovering the button the color of the background will change according to it.  We have used basic HTML and CSS for styling. Syntax: document.bgColor =
2 min read
How to set background color for an elements using jQuery ?
In this article, we will set the background color for an element using jQuery. To add the background color, we use css() method. The css() method in JQuery is used to change style property of the selected element. The css() in JQuery can be used in different ways. The css() method can be used to che
1 min read