How to change Background Color in HTML ?
Last Updated :
21 May, 2025
The background color in HTML sets the visual backdrop behind your webpage content. Around 85% of beginner developers start by customizing background colors to enhance design and readability. Using CSS (Cascading Style Sheets), you can easily apply colors through the background-color
property. This can be done using color names, hex codes, RGB, or HSL values, and applied to specific elements or the entire page.
1. Using bgcolor attribute
HTML allows you to alter the background color of an element using the bgcolor attribute. However, it’s crucial to note that the bgcolor attribute is considered deprecated in HTML5, and it’s recommended to use CSS for styling purposes instead.
Syntax:
bgcolor="color_name"
Example: Here, we set the background color of the body to light green using the bgcolor attribute.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Background Change</title>
</head>
<body bgcolor="lightgreen">
<h1>
Hello reader my name is sachin
Welcome to GeekforGeeks
</h1>
</body>
</html>
Output:

2. Using Inline CSS
In HTML, you can change the background color of an element using inline styling. This approach involves directly adding the style attribute to the HTML element and specifying the desired background color using the background-color property.
Syntax:
< tag style="background-color: colorname;">..</tag>
Example: Here we are using Inline CSS. Inline CSS directly applies styles to individual HTML elements using the style attribute within the HTML tag
index.html
<!DOCTYPE html>
<html>
<head>
<title>Background Change using Inline CSS Example</title>
</head>
<body style="background-color: greenyellow;">
<h1>Welcome to GeeksforGeeks</h1>
<h2>We are using the Inline CSS</h2>
</body>
</html>
Output:

3. Using Internal CSS
Internal CSS is applied within the <style> tags in the <head> section of an HTML document. This method allows you to set styling rules for specific elements, such as setting the background color.
Syntax:
<style>
Body {
background-color: greenyellow;
}
</style>
Example: Here, the background color is set to greenyellow using internal CSS within the <style> tags in the <head> section.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS</title>
<style>
Body {
background-color: greenyellow;
}
</style>
</head>
<body>
<h1>Welcome to GeeksforGeeks</h1>
<h2>We are using the Internal CSS</h2>
</body>
</html>
Output:

4. Using External CSS
In this method, we define style rules in a separate CSS file and link it to an HTML document using the <link> tag. This file contains all the styling information, including background color settings.
Syntax:
body{
background-color: greenyellow;
};
Example: In this example we are using External CSS to creating a separate CSS file with styling rules and linking it to an HTML document using the <link> tag. This file contains the background color to target our html element.
index.html
<!DOCTYPE html>
<html>
<head>
<title>inline style attribute</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Welcome to GeeksforGeeks</h1>
<h2>We are using the External CSS</h2>
</body>
</html>
style.css
body{
background-color: greenyellow;
}
Output:

Note: Changing the background color of an element in HTML can be achieved through various methods, with CSS being the preferred and more flexible approach. While the bgcolor attribute can be used, it is deprecated in HTML5, and it’s recommended to use CSS instead. Inline CSS, internal CSS, and external CSS offer different levels of control and maintainability, making them suitable for different use cases.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read