The simplest way to change background color
The fastest way to change a background color in HTML is to add a style attribute to the opening tag of the element you want to color. For a whole page, put the style on the <body> tag. For a single section, put it on a <div>, <p>, or any other tag that holds content.
The syntax is always the same: style="background-color: colorname;" where colorname is either a color word, a hex code, or an RGB value. For example, <body style="background-color: lightblue;"> makes the entire page light blue. The semicolon at the end is required.
This method works when ready and requires no separate stylesheet or external file. It is the fastest route when you are testing or making a one-off change to a single element.
Key Takeaways
- Add style="background-color: colorname;" directly to any HTML tag to color that element's background.
- Color names like blue, red, and lightblue work, but hex codes like #FF5733 and RGB values like rgb(255, 87, 51) give you more control.
- Put the style on the <body> tag to color the entire page, or on a <div> or <p> to color just one section.
- For pages with many colored elements, a separate CSS file keeps your HTML cleaner and makes changes faster.
Using color names versus hex codes
HTML recognizes about 140 color names you can type directly: red, blue, green, orange, purple, lightblue, darkgreen, and so on. These are the easiest to read and remember, but they give you only a fixed set of colors. If you need a specific shade, use a hex code instead.
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. Each pair ranges from 00 (none of that color) to FF (full intensity). For example, #FF0000 is pure red, #00FF00 is pure green, and #FFFFFF is white. You can find hex codes for any color you want by searching "color picker" in your browser.
RGB values work the same way but use decimal numbers instead of hex. rgb(255, 0, 0) is red, rgb(0, 255, 0) is green, and each number ranges from 0 to 255. Use whichever format feels natural to you — the browser understands all three.
Coloring the whole page versus one section
To color the entire page background, add the style to the <body> tag at the top of your HTML file. This is the tag that wraps all visible content. For example:
<body style="background-color: #E8F4F8;">
Everything inside the body tag will sit on that background color. If you want only one paragraph, box, or section to have a colored background, wrap that content in a <div> tag and add the style there instead. A div is a container that holds other elements and does nothing on its own except organize your page. For example:
<div style="background-color: lightyellow;"> <p>This paragraph sits on a yellow background.</p> </div>
The yellow background applies only to that div and everything inside it. Text outside the div keeps the original page background.
Using a separate CSS file for multiple colors
If your page has many elements with different background colors, writing style attributes on every tag gets messy and hard to change later. A better approach is to create a separate CSS file and link it to your HTML.
Create a new file called style.css in the same folder as your HTML file. Inside it, write rules like this:
body { background-color: #F5F5F5; } .highlight { background-color: #FFFACD; }
Then link the CSS file in the <head> section of your HTML:
<link rel="stylesheet" href="style.css">
Now any tag with class="highlight" will have a light yellow background, and you can change the color once in the CSS file instead of hunting through your HTML. This method scales well when your page grows.
Understanding background color versus text color
Background color fills the space behind text. If you want to change the text color itself, use color: instead of background-color:. For example, style="color: white; background-color: navy;" puts white text on a navy background.
Always check that your text is readable against its background. Dark text on a light background and light text on a dark background are the easiest to read. If you use a bright background color, use dark text. If you use a dark background, use light text. Testing on a real screen is faster than guessing.
Transparency and layered backgrounds
Sometimes you want a background color that is slightly see-through, so you can see the page behind it. Use rgba instead of rgb or hex. The rgba format is rgba(red, green, blue, transparency) where transparency ranges from 0 (completely invisible) to 1 (completely solid).
For example, style="background-color: rgba(255, 0, 0, 0.5);" is red at half transparency. This is useful when you have a background image on the page and want a colored overlay that does not completely hide it. Hex codes do not support transparency directly, so rgba is the tool for that job.
Troubleshooting background colors that do not appear
If you add a background color and it does not show up, check three things. First, make sure you spelled the color name correctly or used a valid hex code. Second, check that the tag you colored actually has content or a height — an empty div with no text and no set height will not be visible even if it has a background color. Third, check that no other CSS rule is overriding your color. If you have both an inline style and a CSS file, the inline style wins, but if you have two CSS rules pointing at the same element, the one that comes last in the file wins.
Open your browser's developer tools by pressing F12 or right-clicking and selecting "Inspect". Find your element in the HTML tree and look at the Styles panel on the right. It will show you which rules are applied and which ones are crossed out (overridden). This tells you exactly what the browser is doing.
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 combine both: style="background-color: lightblue; background-image: url('pattern.png');" will show the image, and if it does not load, the light blue shows instead. This is a safety net.
What is the difference between background-color and background?
The background property is a shorthand that can set color, image, size, position, and repeat all at once. background-color sets only the color. For straightforward color changes, use background-color. For complex backgrounds with images and positioning, background is faster to write.
Do I have to use style attributes or can I put colors in the HTML tag itself?
HTML tags themselves do not have a color attribute. You must use either a style attribute (inline) or a CSS file (external or internal). The style attribute is the quickest for testing; a CSS file is cleaner for real projects.
What happens if I set a background color on a tag that already has a background image?
The color shows behind the image if the image is transparent or does not load. If the image is solid and fully opaque, you will not see the color. The color acts as a fallback.
Can I make the background color change when someone hovers over an element?
Yes, but you need CSS for that. In your CSS file, write .button:hover { background-color: darkblue; } to change the color when a mouse hovers over an element with class="button". Inline styles cannot do this — you need a separate CSS file or a <style> tag in your HTML head.