Set Background Color using CSS (original) (raw)
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:
Table of Content
- Setting the background color using Inline CSS
- Setting the background color using Internal CSS
- Setting the background color using External CSS
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
**Setting the background color using Inline CSS Example
Below is an example that illustrates the use of inline CSS.
HTML `
GeeksForGeeks
<h3 style="text-align:center;">
How to change color of Background?
</h3>
`
**Output:

Explanation:
- In this example by using Inline CSS sets the background color of the 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
**Setting the background color using Internal CSS Example:
Below is the example that illustrates the use of internal CSS.
HTML `