The Simplest Way to Set a Background Color
The fastest way to change your page background color is to add a style attribute to the opening <body> tag. Write <body style="background-color: blue;"> and the entire page turns that color. Replace "blue" with any color name HTML recognizes — red, green, yellow, purple, orange, pink, brown, gray, black, white, and dozens more.
This method works when ready and requires no separate file. If you are editing a single HTML page and want to see the change right away, this is the fastest route. The color applies to the whole page background behind all your text and images.
You can also use hex color codes instead of color names. A hex code is a six-character code that starts with a hash mark, like #FF5733 or #000000. Write <body style="background-color: #FF5733;"> and the page becomes that shade. Hex codes let you pick from millions of colors instead of just the named ones.
Key Takeaways
- Add style="background-color: colorname;" to your <body> tag to change the entire page background in one line.
- Use color names like blue, red, or green, or use hex codes like #FF5733 for precise color control.
- A <style> block in the head section keeps your colors organized if you are styling multiple elements.
- The background-color property works on any HTML element — not just the body, but also divs, paragraphs, buttons, and headers.
- RGB values like rgb(255, 87, 51) are another option if you prefer to mix red, green, and blue numbers instead of hex codes.
Using a Style Block for Cleaner Code
If you are building a page with multiple background colors or plan to edit the colors later, put your styles in a <style> block inside the <head> section. This keeps all your color rules in one place instead of scattered through your HTML tags.
Write your style block like this:
<head> <style> body { background-color: lightblue; } </style> </head>
The page background becomes light blue. You can change "lightblue" to any color name or hex code. If you want different background colors for different sections, add more rules — for example, div { background-color: yellow; } makes all divs yellow, and h1 { background-color: gray; } makes all headings gray.
Changing Background Color for Specific Elements
You do not have to color the entire page. You can color just one paragraph, button, heading, or box by adding the style attribute to that specific tag. Write <p style="background-color: green;">This paragraph has a green background.</p> and only that paragraph gets the color.
This is useful when you want the page background to stay white or light, but you want to highlight certain content. A colored box around important text draws the reader's eye. You can combine background color with text color too — for example, <p style="background-color: navy; color: white;"> creates dark blue background with white text, which is straightforward to read.
Common Color Names and Hex Codes
HTML recognizes about 140 color names. The most common ones are black, white, red, blue, green, yellow, orange, purple, pink, brown, gray, and cyan. You can also add "light" or "dark" in front — lightblue, darkgreen, lightgray — and HTML understands those too.
If you want a specific shade, use a hex code. Hex codes run from #000000 (pure black) to #FFFFFF (pure white). The first two characters control red, the middle two control green, and the last two control blue. #FF0000 is pure red, #00FF00 is pure green, and #0000FF is pure blue. A color picker tool (search "hex color picker") shows you the code for any color you want.
Using RGB Values Instead of Hex
RGB stands for red, green, blue. Instead of a hex code, you can write three numbers from 0 to 255, one for each color. Write style="background-color: rgb(255, 0, 0);" for red, rgb(0, 255, 0) for green, or rgb(0, 0, 255) for blue. The number 255 means full strength, and 0 means none.
RGB is useful if you think in terms of mixing colors. rgb(255, 255, 0) mixes full red and full green to make yellow. rgb(128, 128, 128) mixes equal amounts of all three to make medium gray. Most people find hex codes or color names easier, but RGB works just as well.
Mistakes to Avoid
The most common mistake is forgetting the semicolon at the end of the style rule. Write style="background-color: blue;" with the semicolon, not style="background-color: blue" without it. The semicolon tells the browser the rule is finished.
Another mistake is misspelling the color name. "Lightblue" works, but "light blue" with a space does not — HTML color names have no spaces. If you are unsure of the exact spelling, use a hex code instead.
Do not forget the hash mark on hex codes. Write #FF5733, not FF5733. The hash mark tells the browser it is a hex code and not something else.
Frequently Asked Questions
Can I use a background image instead of a solid color?
Yes. Use background-image: url('image.jpg'); instead of background-color. The image file must be in the same folder as your HTML file, or you need the full web address. You can combine both — set a background color as a fallback in case the image does not load.
What if the text is hard to read on my background color?
Change the text color with the color property. Write style="background-color: navy; color: white;" to put white text on a dark background. Dark text on light backgrounds and light text on dark backgrounds are easiest to read.
Does background color work on all HTML elements?
Yes. You can add background-color to body, div, p, h1, button, span, table, or any other element. Each element gets its own background color independent of the others.
How do I make the background color transparent?
Use background-color: transparent; or background-color: rgba(255, 0, 0, 0.5); where the last number (0.5) controls transparency. Zero is fully transparent, and one is fully solid. This is useful when you want to see the page or parent element behind it.
What is the difference between a hex code and a color name?
Color names are easier to read and remember — you can write "red" and know what you get. Hex codes give you millions of options instead of 140. Both work the same way in the browser; choose whichever is clearer to you.