CSS Background color
Sure! CSS (Cascading Style Sheets) allows you to style HTML elements, including setting their background colors. The background-color property is used to define the color of an element's background. You can specify colors in several ways, including by name, hexadecimal value, RGB, RGBA, HSL, and HSLA.
Here are some examples:
1. Color Names
CSS supports a range of color names that you can use directly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Example</title>
<style>
.color-name {
background-color: lightblue; /* Light blue background */
padding: 20px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="color-name">This div has a light blue background.</div>
</body>
</html>
2. Hexadecimal Values
Colors can be specified using a hex code, which is a 6-digit combination of numbers and letters.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Example</title>
<style>
.hex-color {
background-color: #ff6347; /* Tomato red background */
padding: 20px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="hex-color">This div has a tomato red background.</div>
</body>
</html>
3. RGB Values
RGB (Red, Green, Blue) values allow you to define colors by specifying the intensity of red, green, and blue light.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Example</title>
<style>
.rgb-color {
background-color: rgb(255, 99, 71); /* Tomato red background */
padding: 20px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="rgb-color">This div has a tomato red background.</div>
</body>
</html>
4. RGBA Values
RGBA (Red, Green, Blue, Alpha) is similar to RGB but includes an alpha value for transparency.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Example</title>
<style>
.rgba-color {
background-color: rgba(255, 99, 71, 0.5); /* Tomato red with 50% opacity */
padding: 20px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="rgba-color">This div has a tomato red background with 50% opacity.</div>
</body>
</html>
5. HSL Values
HSL (Hue, Saturation, Lightness) values define colors based on hue, saturation, and lightness.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Example</title>
<style>
.hsl-color {
background-color: hsl(9, 100%, 64%); /* Tomato red background */
padding: 20px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="hsl-color">This div has a tomato red background.</div>
</body>
</html>
6. HSLA Values
HSLA (Hue, Saturation, Lightness, Alpha) is similar to HSL but includes an alpha value for transparency.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Example</title>
<style>
.hsla-color {
background-color: hsla(9, 100%, 64%, 0.5); /* Tomato red with 50% opacity */
padding: 20px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="hsla-color">This div has a tomato red background with 50% opacity.</div>
</body>
</html>
These examples show how to use different methods for specifying background colors in CSS. Feel free to experiment with these values to see how they affect your elements!
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
