How to Change Background Color in CSS
What You’ll Build or Solve
You’ll apply background colors to the entire page and to individual elements using background-color. By the end, you’ll control contrast and section styling with clean, predictable CSS rules.
When This Approach Works Best
Use this approach when you:
Learn CSS on Mimo
- Set a full-page background color.
- Highlight cards, alerts, or navigation sections.
- Improve readability by adding contrast behind text.
Avoid this approach when you need gradients or background images. Those require background-image or gradient functions.
Prerequisites
- A basic HTML file
- A CSS file or
<style>block - Familiarity with CSS selectors
No additional setup is required.
Step-by-Step Instructions
Step 1: Use background-color
background-color changes the solid background of any element.
Apply it to the full page:
CSS
body {
background-color:lightblue;
}
Apply it to a specific element:
CSS
.card {
background-color: #f4f4f4;
padding:20px;
}
Both examples use the same property. Only the selector changes.
Color Formats You Can Use
You can define colors in several formats:
- Named colors like
coral - Hex values like
#ff5733 - RGB values like
rgb(255, 87, 51) - RGBA values like
rgba(0, 0, 0, 0.5)for transparency
RGBA allows transparency values between 0 and 1.
0 is fully transparent.
1 is fully opaque.
Change Background on Hover
If you want to change the background color when a user interacts with an element, use a pseudo-class:
CSS
.button:hover {
background-color:navy;
}
If a color does not apply, a more specific selector or inline style may be overriding it.
Also adjust text color when needed to maintain readability.
That is the complete technique. Everything else builds on this single property.
Examples You Can Copy
Example 1: Full-Page Dark Theme
CSS
body {
background-color: #121212;
color:white;
}
Example 2: Highlighted Content Card
CSS
<divclass="card">
<h2>Welcome</h2>
<p>This section stands out.</p>
</div>
CSS
.card {
background-color: #e9f5ff;
padding:20px;
border-radius:8px;
}
Example 3: Hoverable Button
<buttonclass="cta">Sign up</button>
CSS
.cta {
background-color:teal;
color:white;
}
.cta:hover {
background-color:darkslategray;
}
Common Mistakes and How to Fix Them
Mistake 1: Using color Instead of background-color
You might write:
CSS
.box {
color:blue;
}
Why it breaks: color changes text color, not the background.
Correct approach:
CSS
.box {
background-color:blue;
}
Mistake 2: Ignoring Text Contrast
You might apply:
CSS
.notice {
background-color:yellow;
}
Why it breaks: Light background with light text reduces readability.
Correct approach:
CSS
.notice {
background-color:yellow;
color:black;
}
Mistake 3: Background Not Covering Full Screen
You might set:
CSS
body {
background-color:lightgray;
}
Why it breaks: The page might not fill the entire viewport height.
Correct approach:
CSS
html,
body {
min-height:100%;
background-color:lightgray;
}
Troubleshooting
If the background color does not appear, confirm that the selector matches the element.
If a different color appears, check for a more specific selector or inline style.
If transparency does not work, confirm that you used rgba().
If only part of the page changes color, confirm the element has height or padding.
If styles do not update, confirm the CSS file is properly linked in your HTML.
Quick Recap
- Use
background-colorto change solid backgrounds. - Apply it to
bodyfor full-page color. - Apply it to classes or IDs for sections.
- Use RGBA when you need transparency.
- Watch for selector conflicts and maintain proper text contrast.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot