How to change the background color in HTML (CSS background)
background-color on the page, a section and a word, the value formats, background images and gradients, text contrast, and fixes when the background will not change.
To change the background color in HTML, set the CSS background-color property. For the whole page: <body style="background-color: #f4f6fb;">. The old bgcolor attribute (<body bgcolor="yellow">) was removed from the HTML standard, so use CSS instead.
The whole page
Put a <style> block in the <head> and set the color on body:
<style> body { background-color: #f4f6fb; } </style>
If a thin white strip shows at the bottom on short pages, set it on html too: html, body { background-color: #f4f6fb; min-height: 100%; }.
One section, box or word
- ✓A section: <section style="background-color: #fff3c4;">…</section>. Add padding: 24px; so the text does not touch the edges.
- ✓Several boxes with a class: <style> .card { background-color: #ffffff; padding: 16px; border-radius: 8px; } </style> then <div class="card">…</div>.
- ✓One word, like a highlighter: <span style="background-color: yellow;">due Friday</span>, or the <mark> tag, which is yellow by default.
- ✓A table row: <tr style="background-color: #eef2ff;">. For striped rows: tr:nth-child(even) { background-color: #f7f7f7; }.
Color value formats
- ✓Names: lightblue, beige, black. Browsers know about 140 named colors.
- ✓Hex: #f4f6fb, or the short form #eee for #eeeeee.
- ✓rgb() with transparency: rgb(0 0 0 / 50%) gives a half-see-through dark layer, useful over a photo.
- ✓hsl(): hsl(220 40% 97%). Change only the last number to get a lighter or darker shade.
Background image or gradient
An image: body { background-image: url("hero.jpg"); background-size: cover; background-position: center; }. Keep the image file in the same folder as the .html file and match the file name exactly, capital letters included.
Always set a background-color as well, so the text stays readable while the image loads or if the file is missing.
A gradient needs no image file: background: linear-gradient(135deg, #1a4fd6, #6b3fc2);.
When the background does not change
- ✓You used color instead of background-color. color changes the text, not what is behind it.
- ✓Another rule wins: a more specific selector or a later rule overrides yours. Right-click, choose Inspect, and look for your rule crossed out in the Styles panel.
- ✓The element has no height: an empty <div> is 0 pixels tall, so its background is invisible. Add content, padding or a height.
- ✓A child covers it: a full-width section with its own background hides the body color behind it.
- ✓Typo or missing semicolon: style="background-color: #f4f6f" (five digits) is ignored with no error.
- ✓Background image path is wrong: open the image URL on its own in the browser. If it 404s, fix the folder or file name.
- ✓Cached file: reload with Ctrl+Shift+R (Cmd+Shift+R on a Mac).
Keep the text readable
Dark text on a light background or light text on a dark one, with a contrast ratio of at least 4.5:1 for body text. Chrome shows the ratio when you click the color swatch in Inspect. If you change the background to a dark color, change the text color too; see how to change text color in HTML.
Pages made with AI tools
Pages from Claude, ChatGPT and other tools often use Tailwind, where the background is a class such as bg-white or bg-slate-900. Swap the class on the element instead of adding a style attribute. Some of these pages switch to a dark background when the device is in dark mode, so check both before you send it.
Share the page as a link
Backgrounds look different on different screens, so check the page on a phone before you send it. With FileShare Pro you upload the .html file from the dashboard (or a folder with the background image in it), copy the link and send it; it opens as a page in any browser and the person opening it needs no account. To change the color after you send it, fix the file and upload it to the same link. New to HTML? Start with how to create an HTML file. Sign up free to share your first page.
Frequently asked
Add <style> body { background-color: #f4f6fb; } </style> in the <head>, or put style="background-color: …" on the <body> tag.
Most browsers still show it, but it is obsolete in HTML. Use the CSS background-color property instead.
Wrap it in <mark> or in a <span style="background-color: yellow;">.
Usually the path or file name is wrong, or the image was not uploaded with the page. Open the image URL directly; if it does not load, the page cannot load it either.
Drop the file, copy the link — live in seconds, free plan included.
Start sharing free