CSS Colors
rgba(0, 0, 0, 0.5) is a 50% transparent black overlay.
#FF0000 can be abbreviated as #F00 — when each pair in the six digits is the same, it can be compressed to three digits. Similarly #aabbcc becomes #abc. It's worth memorizing hex values for common colors: #fff (white), #000 (black), #f00 (red).
Colors are one of the most fundamental visual elements in web design. CSS provides multiple ways to represent colors, from simple color names to flexible HSL, each with its appropriate use cases.
Color Value Quick Reference
| Format | Example | Description |
|---|---|---|
| Color name | red, blue, tomato, skyblue |
Intuitive but limited (~140 colors) |
| HEX | #ff0000, #333, #e3f2fd |
Most common format, 6 or 3 digit shorthand |
| RGB | rgb(255, 0, 0), rgb(51, 51, 51) |
Red, Green, Blue values 0-255 |
| RGBA | rgba(255, 0, 0, 0.5) |
RGB + transparency (alpha: 0-1) |
| HSL | hsl(0, 100%, 50%) |
Hue(0-360) + Saturation(0-100%) + Lightness(0-100%) |
| HSLA | hsla(0, 100%, 50%, 0.5) |
HSL + transparency |
opacity |
0.5, 1, 0.25 |
Overall transparency (affects all content including children) |
1. Color Names
CSS supports over 140 predefined color names, the most intuitive to use.
Example
<style>
p {
color: red;
}
h1 {
color: blue;
}
h2 {
color: green;
}
</style>
<p>This is a paragraph example.</p>
<h1>First Level Heading</h1>
<h2>Second Level Heading</h2>
Common color names include: red, blue, green, black, white, gray, orange, purple, yellow, pink, gold, navy, teal, coral, tomato, skyblue, violet, brown, cyan, magenta, lime, olive, silver.
2. Hexadecimal (HEX)
Hexadecimal colors start with #, format is #RRGGBB, where RR is red, GG is green, BB is blue, range 00 to FF (hexadecimal).
Example
<style>
.hex-demo {
color: #ff0000; /* Red */
background: #333; /* Dark gray (3-digit shorthand) */
border: 1px solid #e3f2fd; /* Light blue */
}
</style>
<div class="hex-demo">Hexadecimal color example</div>
Shorthand rules:
#FF0000→#F00(red)#00FF00→#0F0(green)#0000FF→#00F(blue)#aabbcc→#abc
3. RGB and RGBA
RGB uses decimal values 0-255 for red, green, blue channels. RGBA adds Alpha transparency (0-1).
Example
<style>
.rgb-demo {
color: rgb(255, 0, 0); /* Red */
background: rgba(0, 0, 0, 0.5); /* 50% transparent black */
}
</style>
<div class="rgb-demo">RGB/RGBA color example</div>
4. HSL and HSLA
HSL uses Hue (0-360), Saturation (0-100%), Lightness (0-100%). HSLA adds Alpha transparency.
Example
<style>
.hsl-demo {
color: hsl(0, 100%, 50%); /* Red */
background: hsla(120, 100%, 50%, 0.3); /* 30% transparent green */
}
</style>
<div class="hsl-demo">HSL color example</div>
5. opacity Property
The opacity property controls overall element transparency (0-1), affecting all content including children.
Example
<style>
.opacity-demo {
opacity: 0.5;
background: blue;
color: white;
padding: 20px;
}
</style>
<div class="opacity-demo">50% transparent element (all content affected)</div>
❓ FAQ
📖 Summary
- CSS provides multiple color representation methods: names, HEX, RGB/RGBA, HSL/HSLA
- HEX is the most commonly used format, HSL is most designer-friendly
- RGBA and HSLA add transparency control to RGB and HSL
opacityaffects the entire element including children- Choose the format that best fits your workflow and use case
📝 Exercises
- Create a color palette using at least 5 different color representation methods.
- Create a semi-transparent overlay effect using RGBA.
- Create a color scheme using HSL by varying only the hue value.
- Compare the difference between
opacity: 0.5andbackground: rgba(0,0,0,0.5).



