CSS Colors

✅ Recommend HSL: HSL is very designer-friendly — when adjusting colors, you only need to change the hue (H) value while keeping saturation and lightness constant to get different shades of the same color family. W3Schools' color picker uses HSL.

📌 Use case: RGBA's A (Alpha) channel lets you control transparency, perfect for overlay masks and semi-transparent backgrounds. For example, rgba(0, 0, 0, 0.5) is a 50% transparent black overlay.

💡 Tip: #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

HTML
<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>
▶ Try it Yourself

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.

📌 Tip: Color names are simple and intuitive, great for quick prototyping. However, in production environments, color names have limited selection and lack precision — hexadecimal or RGB notation is recommended.

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

HTML
<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>
▶ Try it Yourself

Shorthand rules:

3. RGB and RGBA

RGB uses decimal values 0-255 for red, green, blue channels. RGBA adds Alpha transparency (0-1).

Example

HTML
<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>
▶ Try it Yourself

4. HSL and HSLA

HSL uses Hue (0-360), Saturation (0-100%), Lightness (0-100%). HSLA adds Alpha transparency.

Example

HTML
<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>
▶ Try it Yourself

5. opacity Property

The opacity property controls overall element transparency (0-1), affecting all content including children.

Example

HTML
<style>
.opacity-demo {
  opacity: 0.5;
  background: blue;
  color: white;
  padding: 20px;
}
</style>
<div class="opacity-demo">50% transparent element (all content affected)</div>
▶ Try it Yourself

❓ FAQ

Q What's the difference between HEX and RGB colors?
A Both represent the same color, just in different formats. HEX uses 6 hexadecimal digits (0-9, A-F), RGB uses three decimal numbers (0-255). HEX is more concise (e.g., `#ff0000`), RGB is more intuitive for understanding values (e.g., `rgb(255, 0, 0)`). In practice, HEX is more commonly used.
Q When should I use RGBA vs opacity?
A RGBA controls transparency of a specific property (like background), while opacity controls transparency of the entire element including all children. If you only want background transparency without affecting text, use RGBA. If you want the entire element semi-transparent, use opacity.
Q What's the advantage of HSL over RGB?
A HSL is more intuitive for designers. You can easily create color variations by adjusting just the hue while keeping saturation and lightness constant. RGB requires adjusting three values simultaneously. HSL also makes it easier to create harmonious color schemes and understand color relationships.

📖 Summary

📝 Exercises

  1. Create a color palette using at least 5 different color representation methods.
  2. Create a semi-transparent overlay effect using RGBA.
  3. Create a color scheme using HSL by varying only the hue value.
  4. Compare the difference between opacity: 0.5 and background: rgba(0,0,0,0.5).
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏