CSS: CSS Colors

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Gray🏷 Category: CSS Tutorial
✅ 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)

Properties in this section: opacity color background-color background border-color background-image

1. Color Names

CSS supports over 140 predefined color names, the most intuitive to use.

▶ Example

▶ 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

▶ 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

▶ Try it Yourself

4. HSL and HSLA

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

▶ Example

▶ Try it Yourself

5. opacity Property

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

▶ Example

▶ Try it Yourself

Properties in this section: opacity color background-color background border-color background-image

❓ FAQ

Q How do I add color to an element in CSS?
A Two properties split the job: color for text, background-color for the background — h1 { color: crimson; background-color: #f5f5f5; }. The background shorthand sets background color and image together, and borders get color via border-color or inside border: 1px solid #333.
Q What are the ways to write color values in CSS?
A Four formats: color names (red, easy but limited to ~140), hex (#ff0000, the most common, with optional 8-digit alpha like #ff000080), rgb(255, 0, 0), and hsl(0, 100%, 50%). Use hex day-to-day, rgba/hsla when you need transparency, and hsl when designing palettes.
Q What do the three numbers in rgb() mean?
A rgb(R, G, B) is the intensity of red, green, and blue light, each 0–255. rgb(255, 0, 0) is pure red, rgb(0, 0, 0) black, rgb(255, 255, 255) white. Equal values give gray — rgb(128, 128, 128) is mid-gray. Add a fourth value and it becomes rgba, which adds transparency control.
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 How do I make the background semi-transparent but keep the text solid?
A Give the background an alpha with rgba: background-color: rgba(0, 0, 0, 0.5); — the 0.5 means 50% opaque. Don't use opacity: 0.5, which fades the whole element including its text. A banner with a dark translucent backdrop and readable white text is exactly rgba background + normal text.
Q How do I make a gradient in CSS?
A background: linear-gradient(to right, #ff7e5f, #feb47b);. The direction can be to right, to bottom right, or an angle like 45deg, and you can pass more than two stops: linear-gradient(to bottom, #fff, #ddd, #999). For radial gradients use radial-gradient(circle, ...).
Q My gradient isn't showing up — why?
A First check where you wrote it: gradients count as images, so they go on background or background-image — background-color ignores them. Then look for a missing comma or a misspelled direction (to right, not to-right). Old browsers needed the -webkit-linear-gradient prefix; modern ones no longer do.
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.
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.

📖 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).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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