Colors help make websites visually appealing and functional. They enhance readability, draw attention to important elements, and support a site’s branding and accessibility.
For example, error messages are often red, while success messages are green.
The color property sets the text color, and the background-color property sets the background of an element.
Here’s a live inline HTML example:
<p style="color: blue; background-color: lightyellow;">
This paragraph has blue text on a green background.
</p>CSS supports multiple formats for defining colors:
red, blue, green#ff0000 (Red)rgb(255, 0, 0)rgba(255, 0, 0, 0.5) (with transparency)hsl(0, 100%, 50%)hsla(0, 100%, 50%, 0.3)Use color to style the text and background-color to color the background of elements.
h1 {
color: white;
background-color: #333333;
}RGBA and HSLA formats allow transparency by adjusting the alpha channel (last value):
0 = fully transparent1 = fully opaquediv {
background-color: rgba(0, 0, 255, 0.3);
}Create a styled box with the following:
<div style="background-color: red; color: #ffffff; padding: 20px;">
<p style="background-color: rgba(0, 0, 255, 0.5);">
This is a semi-transparent paragraph inside a red box.
</p>
</div>Ask the AI if you need help understanding or want to dive deeper in any topic