How to Change Background Color in HTML: Your Step-by-Step Guide

By Cristian G. Guasch •  Updated: 09/18/23 •  7 min read

With the ever-evolving world of website design, one aspect remains constant – the importance of color. It’s not just about aesthetics; it’s also about creating an immersive user experience and delivering your message effectively. If you’ve been wondering how to change the background color in HTML, you’ve come to the right place.

HTML or HyperText Markup Language is a standard language used for designing web pages. It consists of various tags and attributes that help in structuring a webpage. One such attribute is ‘bgcolor’, which allows changing the background color.

In this article, I’ll be walking you through the process step by step. Whether you’re new to HTML or looking to refresh your knowledge, I’m confident this guide will assist you in mastering this fundamental skill.

Understanding HTML and CSS Basics

I’m going to start by saying this: if you’re planning on changing the background color of a webpage, you’ll need to know a thing or two about HTML and CSS. They’re the building blocks that make up every single website on the internet.

HTML (Hyper Text Markup Language) is like the skeleton of a webpage. It’s what structures your content, giving it headers, paragraphs, images and more. Think of it as setting up a stage for a play – where each actor goes, where props are placed etc.

Here’s an example of what basic HTML code might look like:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

But while HTML sets up the stage, CSS (Cascading Style Sheets) is all about giving it style – colors, fonts, layouts etc. It’s like deciding what costumes your actors wear or how lighting should be designed in that play we talked about earlier.

Here’s an example of how you could use CSS to change the background color:

body {
  background-color: lightblue;
}

This code would give your entire body element a light blue background color.

But here’s something cool – you can also include this CSS directly into your HTML file using ‘style’ attribute:

<body style="background-color:lightblue;">

There are numerous ways to manipulate these codes to create stunning web designs. But remember – keep experimenting! That’s really how everyone learns.

Different Methods to Change Background Color

When it comes to manipulating the background color of your HTML pages, there’s a variety of methods you can apply. Let’s dive in and discuss some of these techniques.

One common method is using inline CSS. You’d simply declare the style attribute directly within your HTML tag. Here’s an example:

<body style="background-color:blue;">

In this case, “body” represents the element we’re applying the style to, while “background-color:blue;” defines the specific styling applied – turning our background blue.

Another popular approach is leveraging external CSS. This involves creating a separate .css file where all your styles live and linking it to your HTML document. It’s ideal when dealing with complex styles or multiple pages requiring similar styling.

Here’s an example:

<!-- Your HTML file -->
<body class="blue-background">
</body>

/* Your CSS file */
.blue-background {
    background-color: blue;
}

This method assigns a class name “blue-background” to our body tag, which corresponds to a rule set up in our external CSS file that turns backgrounds blue.

Next up is internal or embedded CSS – utilizing a <style> block within your HTML document’s head section. This strategy allows for centralized control over styles without needing additional files.

Example:

<head>
<style>
body { 
    background-color: blue; 
}
</style>
</head>

It might seem like there are quite a few ways to change your website’s backdrop color—and you’d be right! From direct inline styling through to external and internal CSS methods, there’s no shortage of options available for web designers wanting their sites to stand out from the crowd.

How to Use Inline Styles for Background Color in HTML

You’re ready to spice up your webpage, and I’m here to guide you on how to use inline styles for background color in HTML. We’ll delve into the specifics and explore how you can transform a bland page into a vibrant canvas using mere lines of code.

First off, let’s clarify what we mean by ‘inline styles’. In HTML, styles are typically defined within the <style> tag. However, when we talk about inline styles, we’re referring to those directly added within an HTML element using the style attribute. This method gives that specific element its unique style declaration.

To set a background color with inline style, you’d simply assign the style attribute followed by background-color: inside any of your chosen HTML tags. It looks something like this:

<div style="background-color:blue;">
    This is a blue box!
</div>

What if you want more than just plain colors? That’s where hexadecimal color codes come in handy! They open up an entire spectrum of colors at your disposal. Here’s how it works:

<p style="background-color:#FF6347;">
    The background color of this paragraph is Tomato!
</p>

In addition to basic colors and hex codes, CSS offers RGB (Red Green Blue) values as well. They allow even greater control over the exact shade of color you desire:

<span style="background-color:rgb(255,99,71);">
    This span has a Tomato colored background too!
</span>

And there you have it – three different ways of adding pizzazz to your webpage with inline styling for backgrounds in HTML! But remember that while inline styles offer convenience and specificity, they can become difficult to manage as your project grows. Hence it’s generally advisable to use them sparingly.

Changing Background Color using External CSS

If you’re looking to switch things up on your webpage, changing the background color can make a significant difference. One of the most common methods is using external Cascading Style Sheets (CSS). Let’s walk through it together.

First off, what’s CSS? In essence, it’s a style sheet language that describes the look and formatting of a document written in HTML. Using an external CSS file allows you to change the layout and design of multiple pages at once. Quite nifty, I must say.

To kick things off with changing your background color via external CSS, you’ll need to link your HTML file to your CSS file. Here’s an example of how this is done:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

Once linked, head over to the mystyle.css document and use something like this snippet:

body {
  background-color: lightblue;
}

And just like that, you’ve set the body background color as light blue! Remember that you can use any valid color name or hexadecimal color code instead of ‘lightblue’.

You might be wondering: what if I want different sections of my page to have different colors? Well, we’ve got you covered there too. Assign classes in your HTML tags then specify different colors for each class in your CSS stylesheet.

Here’s a quick example:

HTML:

<div class="section1">Some text here...</div>
<div class="section2">Some more text here...</div>

CSS:

.section1 {
  background-color: yellow;
}

.section2 {
  background-color: green;
}

Before you know it, you’ll be a master of changing background colors using external CSS. Just remember: practice makes perfect!

Cristian G. Guasch

Hey! I'm Cristian Gonzalez, I created HTML Easy to help you learn HTML easily and fast.

Related articles