The Simplest Way to Change Background Color
The fastest way to change a page's background color is to add a style attribute to the opening <body> tag. Write <body style="background-color: blue;"> and the entire page behind your text will turn blue. You can use any color name that HTML recognizes — red, green, yellow, purple, orange, pink, brown, gray, black, white — or you can use a six-digit color code like #FF5733 if you want a specific shade.
This method works when ready and requires no separate file or setup. The color applies to everything on the page unless you override it with a different color on a specific element. If you are building a single page or testing how a color looks, this is the method to use.
Key Takeaways
- Add style="background-color: colorname;" to your <body> tag to color the entire page background.
- You can use color names like red, blue, or green, or six-digit hex codes like #FF5733 for precise colors.
- A <style> block in the <head> section keeps your colors organized if you are styling multiple elements.
- RGB values like rgb(255, 87, 51) let you mix red, green, and blue numbers from 0 to 255 to create any color.
- Background color on a <div> or other container only colors that box, not the whole page.
Using a Style Block for Cleaner Code
If you are changing colors on more than one element, or if you plan to add more styles later, put your colors in a <style> block inside the <head> section of your HTML. Write it like this:
<head> <style> body { background-color: lightblue; } </style> </head>
This method separates your styling from your HTML tags, making the page easier to read and update. If you decide later that you want the background to be a different shade, you change it in one place instead of hunting through your tags.
You can add as many color rules as you need inside the same <style> block. For example, you could color the body one shade and a specific <div> another shade by adding a new rule below the first one.
Color Names Versus Hex Codes Versus RGB
HTML recognizes about 140 color names you can type directly: red, blue, green, navy, teal, salmon, khaki, and many others. These are the easiest to remember and read, but they give you only a limited set of colors to choose from.
Hex codes are six-digit numbers that start with a hash mark, like #FF5733. Each pair of digits controls how much red, green, and blue mix together. #FF0000 is pure red, #00FF00 is pure green, and #0000FF is pure blue. You can find hex codes for almost any color online by searching "color picker" — paste the code directly into your HTML and you get that exact shade.
RGB values work the same way but use three numbers from 0 to 255 instead of hex digits. rgb(255, 0, 0) is red, rgb(0, 255, 0) is green, and rgb(128, 128, 128) is gray. Some people find this format easier to understand because the numbers are more straightforward.
Coloring a Single Box or Section
To color only part of your page — a box, a paragraph, a navigation bar — use a <div> or other container element with a background color. Wrap the content you want to color inside the opening and closing tags, then add the style:
<div style="background-color: yellow;"> <p>This text sits on a yellow background.</p> </div>
The color applies only to that box, not to the rest of the page. This is useful when you want to highlight a section, create a colored sidebar, or make a button stand out. You can also add padding (space inside the box) and other styles to the same style attribute if you separate them with semicolons.
Common Mistakes to Avoid
The most frequent error is forgetting the colon after background-color. Write background-color: blue;, not background-color blue;. The colon tells HTML that a value is coming next.
Another common mistake is misspelling the color name or forgetting the hash mark on a hex code. #FF5733 works, but FF5733 without the hash does not. If your color does not appear, check that you typed the name or code correctly.
If you put a <style> block in the wrong place — for example, inside the <body> instead of the <head> — it may still work in most browsers, but it is not proper HTML structure. Always put <style> in the <head> section.
When to Use an External CSS File
Once your page grows and you have many colors and styles, move them to a separate .css file. Create a new file called styles.css, put all your color rules inside it, and link to it from your HTML <head> like this:
<link rel="stylesheet" href="styles.css">
This keeps your HTML clean and lets you reuse the same styles on multiple pages. For a single page or a quick test, the inline style attribute or a <style> block is fine. For a full website, a separate CSS file is the standard approach.
Frequently Asked Questions
Can I make the background transparent?
Yes, use background-color: transparent; or background-color: rgba(0, 0, 0, 0);. The rgba format lets you control transparency with the fourth number — 0 is fully transparent, 1 is fully opaque, and numbers in between create a see-through effect.
What if my background color does not show up?
Check that you spelled the color name correctly or that your hex code starts with a hash mark. Also make sure you are using background-color, not just background (though background works too). If the page has a background image, the color may be hidden behind it.
How do I change the background color of just the text, not the whole box?
Use background-color on a <span> tag around just the text you want to highlight: <span style="background-color: yellow;">highlighted text</span>. This colors only that small section, not the entire container.
Can I use color names I make up, or do they have to be standard?
HTML only recognizes the standard 140 color names. If you type a name that does not exist, the browser ignores it and shows no color. Use hex codes or RGB values if you need a color that is not in the standard list.