The simplest way to change background color
The fastest way to change your HTML page's background color is to add a style attribute to the opening <body> tag. Inside that attribute, write background-color: followed by a color name or code. For example: <body style="background-color: lightblue;"> makes the entire page light blue.
This method works when ready in any browser because the style attribute tells the browser exactly what to do without needing a separate stylesheet. You can use color names like red, green, navy, or coral — or you can use hex codes like #FF5733 if you want precise control over the shade.
If you want to change only part of the page background instead of the whole thing, use the same approach on a <div> or other container element. A <div> is just a box that holds content, so styling it changes only what's inside that box.
Key Takeaways
- Add style="background-color: colorname;" directly to your <body> tag to change the entire page background in one line.
- You can use color names (blue, coral, gold) or hex codes (#0066FF) — both work the same way in the style attribute.
- For more control and cleaner code, move your color styles into a <style> block in the <head> section instead of writing them in every tag.
- CSS classes let you explore the same background color to multiple elements without repeating the style code each time.
Using a style block for cleaner code
If you're changing colors on multiple elements or want your code to be easier to read, put your styles in a <style> block inside the <head> section of your HTML. This keeps all your color rules in one place instead of scattered throughout your tags.
Here's what that looks like:
<head> <style> body { background-color: lightblue; } </style> </head>
Inside the <style> block, you write the tag name (like body), then put curly braces around the rule. This does exactly the same thing as the style attribute, but it's easier to manage when you have many colors to set. You can also style multiple elements in one block — for example, you could set the body background to light blue and a <div> background to white, all in the same <style> section.
Using CSS classes to reuse colors
When you want the same background color on several different elements, create a CSS class instead of repeating the style code. A class is a named style rule that you can attach to any tag.
First, define the class in your <style> block:
<style> .highlight { background-color: yellow; } </style>
Then use the class attribute on any element you want to highlight:
<p class="highlight">This paragraph has a yellow background.</p> <div class="highlight">This box also has a yellow background.</div>
The class name starts with a dot in the <style> block but no dot when you use it in the tag. This approach saves time because you only write the color rule once, and any element can use it.
Color formats: names, hex codes, and RGB
HTML and CSS accept three main ways to specify a color. Color names are the simplest — you just type the color: red, blue, green, coral, navy, gold. These work in any browser and are fine for basic pages.
Hex codes give you exact control. A hex code is a six-character code starting with a hash mark, like #FF5733. The first two characters control red, the next two control green, and the last two control blue. You can find hex codes for any color using an online color picker, and they're useful when you need a specific shade that doesn't have a straightforward name.
RGB values work the same way as hex codes but use numbers instead. For example, rgb(255, 87, 51) is the same as #FF5733. You'll see RGB used in modern CSS, but hex codes are more common and work everywhere.
All three formats work in the background-color property, so use whichever one you're comfortable with. If you're copying a color from a design file or another website, hex codes are usually what you'll find.
Changing background color on specific sections
You don't have to color the entire page the same. Use <div> tags to create sections and give each one its own background color. A <div> is a container that groups content together, and styling it affects only what's inside.
Here's a practical example:
<body> <div style="background-color: lightblue;"> <h1>Header Section</h1> </div> <div style="background-color: white;"> <p>Main content goes here.</p> </div> </body>
The first <div> has a light blue background and contains the header. The second <div> has a white background and contains the main text. You can nest <div> tags inside each other and give each one a different color, which lets you create layered designs with multiple background colors on one page.
Troubleshooting background colors that don't show
If you set a background color but it doesn't appear, check three things. First, make sure you spelled background-color correctly — a common mistake is writing "backgroundcolor" without the hyphen, which the browser won't recognize. Second, verify that your color name or hex code is spelled right. Third, check that your <style> block is inside the <head> section, not the <body>.
Another common issue: if your text is the same color as the background, you won't see it. For example, white text on a white background is invisible. If this happens, change either the text color using color: or pick a different background color. You can set text color the same way you set background color: style="color: black;" on the tag or color: black; in your <style> block.
If you're using a class and the color still doesn't show, make sure you're using the class attribute correctly. The class name in your HTML tag should match exactly what you wrote in the <style> block, including capital letters.
When to use inline styles versus a style block
You have two choices for where to put your background-color rules: directly in the tag (inline) or in a <style> block. For a single element or a quick test, inline is faster. For anything larger, a <style> block is cleaner and easier to maintain.
Inline styles look like this: <div style="background-color: blue;">. They work when ready and are useful when you're learning or testing. But if you have ten <div> tags that all need the same blue background, writing the style ten times is repetitive and hard to update later.
A <style> block lets you write the rule once and use it everywhere. If you later decide to change blue to green, you change it in one place and all ten <div> tags update automatically. This is why professional developers prefer <style> blocks and classes — they save time and prevent mistakes.
Frequently Asked Questions
Can I use a background image instead of a solid color?
Yes. Use background-image: url('imagefile.jpg'); instead of background-color. You can also combine both — set a background color as a fallback in case the image doesn't load. The syntax is the same whether you use inline styles or a <style> block.
What's the difference between background-color and background?
The background property is a shorthand that can set color, image, and other properties at once. background-color sets only the color. For beginners, background-color is clearer because it does one thing. Both work the same way in your HTML.
Do I need to close the style tag?
Yes. Every <style> tag must have a closing </style> tag. If you forget it, the browser may ignore all your styles. The same rule applies to <head>, <body>, and most other HTML tags — they need an opening and closing pair.
Can I make the background transparent?
Yes, use background-color: transparent;. This makes the element's background see through so whatever is behind it shows through. This is useful when you want to layer elements or remove a background color you set earlier.
What happens if I set background color on both the body and a div?
The <div> color takes priority for everything inside that <div>. The body color still shows everywhere else on the page. If a <div> is inside the body, the <div> background covers the body background only in that area.