Home 〉 CSS Tutorials 〉 CSS background-color Tutorial with Examples – Learn Easily
CSS background-color Tutorial with Examples – Learn Easily !
How do you change the background color of HTML elements using CSS?
This tutorial teaches you how to use the 'background-color' property to enhance the visual appeal of your web pages. Learn through examples how to apply solid colors, gradients, and more.
The 'background-color' property in CSS is used to set the **background color of HTML elements**. It's one of the most used CSS properties to improve the visual style of your webpage.
Let's break it down step-by-step with real examples.
Syntax ✍
selector {
background-color: value;
}
<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: lightblue;
padding: 20px;
color: black;
}
</style>
</head>
<body>
<div class="box">This box has a light blue background.</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
You can use common color names like 'red', 'green', 'blue', 'lightblue', etc.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: #ffcc00;
padding: 20px;
color: #000;
}
</style>
</head>
<body>
<div class="box">Background color using hex code #ffcc00.</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Hex codes allow precise color control, especially for brand colors.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: rgb(255, 99, 71);
padding: 20px;
color: white;
}
</style>
</head>
<body>
<div class="box">This box uses RGB color (tomato red).</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
RGB uses values from 0 to 255 for red, green, and blue.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: rgba(0, 128, 0, 0.5);
padding: 20px;
color: white;
}
</style>
</head>
<body>
<div class="box">This green background is 50% transparent.</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
RGBA adds an alpha value (opacity) from 0 (fully transparent) to 1 (fully opaque).
<!DOCTYPE html>
<html>
<head>
<style>
.box {
background-color: hsl(240, 100%, 75%);
padding: 20px;
color: #000;
}
</style>
</head>
<body>
<div class="box">Background uses HSL (light blue color).</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
HSL stands for Hue, Saturation, and Lightness. It's another color format used in CSS.
Example 📄
<style>
body {
background-color: #f0f8ff;
}
</style>
This changes the entire page background color.
The 'background-color' property is a simple but powerful tool in CSS. Whether you're setting color with names, hex codes, RGB, or HSL, it helps bring your layout to life. Try each format and find what works best for your website style.
It sets the background color of an HTML element.
Yes. CSS supports over 140 color names like red, green, blue, and lightgray.
RGBA includes a fourth value for transparency (alpha), while RGB does not.
Yes, apply 'background-color' to the 'body' tag in your CSS.
Use hex for simple colors, RGB for full control, and RGBA for transparency. HSL is helpful for adjusting color tones.