Use CSS for background color, not old HTML attributes. HTML should describe the page structure, while CSS should handle how that page looks. The CSS background-color property is built for this job and can be applied to the whole page or to specific elements [source: MDN].
TLDR: Use body { background-color: #f5f5f5; } in CSS instead of adding color directly in HTML. If a small business site has 20 pages, one CSS rule can update all page backgrounds at once instead of editing 20 files, which cuts the work by about 95%. HTML color attributes still appear in old tutorials, but they are not the clean way to style modern websites. CSS is easier to maintain, easier to reuse, and far less annoying when a client asks for “just a slightly warmer gray.”
Why CSS wins for background colors
HTML and CSS have different jobs. HTML gives content meaning. It says, “this is a heading,” “this is a paragraph,” or “this is a button.” CSS controls presentation. It says, “make this background blue,” “add space here,” or “use this font.”
That split matters. When styling is mixed into HTML, pages get messy fast. It drives me crazy when one simple color change turns into a hunt through ten files, three templates, and one forgotten inline style from 2018.
Here is the clean CSS way:
body {
background-color: #f4f7fb;
}
This sets the background color for the whole page. Simple. Clear. Easy to change.
The old HTML way: bgcolor
Older HTML allowed attributes like this:
<body bgcolor="lightblue">
That may still work in some browsers because browsers try hard not to break old pages. But working does not mean recommended. The bgcolor attribute is outdated. It also mixes structure and styling in one place.
Expect to waste time on this later. If every page sets its own background inside the HTML, global design changes become slow. A color update that should take ten seconds can turn into several minutes of search-and-replace work. Worse, you may miss one page and end up with an uneven site.
Best ways to set background color with CSS
You can set background color in several places. Some are better than others.
- External CSS file: Best for most websites. One file can style many pages.
- Internal CSS: Useful for a single page or quick test. The CSS sits inside a
<style>tag. - Inline CSS: Good for rare one-off cases, but easy to overuse.
Example using an external stylesheet:
<link rel="stylesheet" href="styles.css">
Then in styles.css:
body {
background-color: white;
}
Example using internal CSS:
<style>
body {
background-color: #fff8e7;
}
</style>
Example using inline CSS:
<body style="background-color: #eeeeee;">
Use inline styles with care. They are quick, but they do not scale well. They also override many stylesheet rules, which can make debugging irritating.
Using color names, hex, RGB, and HSL
CSS gives you several ways to write color values. Each has a place.
- Color names: Easy to read, like
red,white, orlightblue. - Hex codes: Common in design systems, like
#ffffffor#0f172a. - RGB: Good when working with red, green, and blue values, like
rgb(255, 255, 255). - HSL: Handy for adjusting shade and brightness, like
hsl(210, 40%, 96%).
For real projects, hex and HSL are often the most practical. Hex is common in design files. HSL is nice when you want related colors, such as a light blue background and a darker blue border.
Background color on sections, cards, and buttons
You do not have to style only the whole page. CSS can set backgrounds for smaller parts too.
.hero {
background-color: #e0f2fe;
}
.card {
background-color: #ffffff;
}
.button {
background-color: #2563eb;
}
This is where CSS feels much better than HTML attributes. You can create reusable classes. Then you can apply those classes anywhere.
<section class="hero">
<h1>Welcome</h1>
</section>
If the brand color changes later, you update the CSS class once. Every matching section follows.
Do not forget readability
A background color is not just decoration. It affects how easy the page is to read. Pale gray text on a white background may look soft, but it can be painful for users. Strong contrast helps everyone, including people reading on cheap screens or in bright sunlight.
A safe pattern is simple:
- Use a light background with dark text.
- Use a dark background with light text.
- Test buttons and links, not just body text.
- Avoid bright full-page backgrounds unless the design truly needs them.
So, HTML or CSS?
Choose CSS. Use HTML to build the content. Use CSS to style the background. The old HTML bgcolor method belongs in legacy code, not in fresh work.
For a quick page, use internal CSS. For a real website, use an external CSS file. For a tiny one-off tweak, inline CSS is acceptable, but do not let it spread everywhere. Future you will not enjoy cleaning that up.
The best default is:
body {
background-color: #ffffff;
color: #111111;
}
It is clean, readable, and easy to change. That is the whole point.
yehiweb
Related posts
New Articles
Visual Studio Code Linux Ubuntu: Visual Studio Code vs VSCodium for Coding on Ubuntu
Use Visual Studio Code on Ubuntu if you want the smoothest setup, the official Microsoft extension marketplace, and fewer weird…