Color Formats: Names, HEX, RGB, and HSL
Color Formats: Names, HEX, RGB, and HSL
Welcome to Chapter 3! 🎨
Colors give life, emotion, and personality to a website. A medical website might use calm blues and teals, a school sports portal might burst with energetic reds and oranges, while an eco-friendly nature blog shines with lush greens.
In CSS, you have 4 standard ways to pick and declare colors:
- 1Color Keywords (Names) (e.g.,
crimson,navy,gold) - 2HEX Codes (e.g.,
#ff0000,#2563eb) - 3RGB / RGBA Values (e.g.,
rgb(37, 99, 235),rgba(37, 99, 235, 0.5)) - 4HSL / HSLA Values (e.g.,
hsl(217, 83%, 53%))
Let us explore each format using a fun watercolor palette analogy!
The Art Room Paint Palette Analogy 🎨
Imagine you are sitting in your school art class with an empty white paper and a paint palette:
Visual Architecture & Process Flow
How data and code flow step-by-step
1. Color Names (Keywords)
CSS comes with 140 built-in color names that modern browsers understand directly. You can simply write the name as the value:
Popular CSS Color Names:
- Reds/Pinks:
crimson,coral,tomato,hotpink - Blues:
navy,royalblue,deepskyblue,steelblue - Greens:
forestgreen,seagreen,lime,mediumseagreen - Neutrals:
whitesmoke,slategray,darkslategray,gold
Limitations:
While color names are easy to remember, you cannot fine-tune the shade. If royalblue is slightly too dark for your school logo, you cannot adjust it using color names. For exact branding, professionals use HEX or RGB.
2. HEX Codes (Hexadecimal)
A HEX color code is the most widely used color format on the web. It always begins with a hash symbol (#) followed by 6 characters representing three color channels:
In the hexadecimal number system, numbers count from 0 to 9, and then use letters A to F (where A = 10 and F = 15).
00means zero intensity (completely dark / off).FFmeans maximum intensity (full blast: 255).
Common HEX Examples:
3-Digit Shorthand HEX:
If each pair of characters has matching digits, you can shorten it to 3 digits:
#ffffffcan be written as#fff#000000can be written as#000#ff0000can be written as#f00
3. RGB and RGBA (Red, Green, Blue + Alpha)
Every computer screen and phone display produces color by shining tiny Red, Green, and Blue light sub-pixels.
The RGB Syntax:
rgb(red, green, blue) takes 3 values ranging from 0 to 255:
Adding Transparency with RGBA (Alpha):
What if you want a color to be semi-transparent (like tinted sunglasses)? You add a 4th value called Alpha (a): rgba(red, green, blue, alpha)
alphais a decimal number between0and1:0= completely invisible (100% transparent)0.5= half transparent (50% see-through)1= completely solid (opaque)
opacity: 0.5;, the entire box AND all the text inside it becomes see-through!
If you use background-color: rgba(...);, only the background is see-through, keeping your text 100% crisp and readable!4. HSL and HSLA (Hue, Saturation, Lightness)
Many designers consider HSL the easiest color format to understand because it matches human intuition rather than computer sub-pixels!
hsl(hue, saturation, lightness)
+-------------------------------------------------------------------------+ | UNDERSTANDING HSL | +-------------------------------------------------------------------------+ | 1. HUE (0 to 360 degrees on the color wheel): | | 0° / 360° = Red 120° = Green 240° = Blue | | 60° = Yellow 180° = Cyan 300° = Magenta | | | | 2. SATURATION (0% to 100%): | | 0% = Dull gray (no color) 100% = Full vibrant vivid color | | | | 3. LIGHTNESS (0% to 100%): | | 0% = Completely Black 100% = Completely White | | 50% = Perfect normal color shade | +-------------------------------------------------------------------------+
Example:
Notice how easy it is to create darker or lighter shades with HSL: you only need to change the Lightness percentage without recalculating any hex numbers!
Comparison of the 4 Formats
| Format | Syntax Example | When to Use | Transparency Support |
|---|---|---|---|
| Color Name | color: crimson; | Quick tests, prototypes, basic colors | ❌ No |
| HEX Code | color: #2563eb; | Most common on the web, brand style guides | ✅ Yes (#2563eb80) |
| RGB / RGBA | color: rgba(37, 99, 235, 0.5); | When creating translucent backgrounds & overlays | ✅ Yes (alpha) |
| HSL / HSLA | color: hsl(217, 83%, 53%); | Dynamic color themes, hover states (tints & shades) | ✅ Yes (hsla) |
Common Beginner Mistakes (Do's and Don'ts)
| What Beginners Do (Don't ❌) | What You Should Do Instead (Do ✅) | Why? |
|---|---|---|
Forgetting the hash: color: 2563eb; | Always include #: color: #2563eb; | Without #, the browser does not recognize it as a HEX code. |
Using opacity: 0.5 on a card with text. | Use background-color: rgba(...). | opacity makes the text inside hard to read; rgba keeps text solid. |
Writing RGB values greater than 255: rgb(300, 0, 0). | Keep values between 0 and 255: rgb(255, 0, 0). | 255 is the maximum allowed integer for standard 8-bit color channels. |
Quick Revision Summary
- CSS supports 4 main color formats: Keywords, HEX, RGB/RGBA, and HSL/HSLA.
- HEX codes use
#RRGGBBwith values from00(darkest) toFF(brightest). - RGB mixes Red, Green, and Blue light from
0to255. - RGBA adds an Alpha channel (
0to1) to control transparency without fading text. - HSL represents Hue (0–360° on color wheel), Saturation (0–100%), and Lightness (0–100%).
Practice Quiz
Test your understanding with these multiple-choice questions:
1. In the HEX color code #00FF00, which color channel is set to maximum intensity?
A. Red B. Green C. Blue D. Yellow Answer: B Explanation: In #RRGGBB, the middle two characters represent Green. FF is the maximum value, while Red and Blue are both set to 00.
2. What is the valid range of numbers for the Red, Green, and Blue values in the rgb() function?
A. 0 to 100 B. 1 to 10 C. 0 to 255 D. 0 to 360 Answer: C Explanation: Standard RGB values range from 0 (minimum intensity) to 255 (maximum intensity) per channel.
3. What does the "A" stand for in the rgba() color format?
A. Angle B. Alignment C. Alpha D. Ambient Answer: C Explanation: "A" stands for Alpha. It represents the opacity/transparency level, measured on a decimal scale from 0.0 (transparent) to 1.0 (opaque).
4. Which of the following HSL values represents pure Red on the 360-degree color wheel?
A. hsl(0, 100%, 50%) B. hsl(120, 100%, 50%) C. hsl(240, 100%, 50%) D. hsl(60, 100%, 50%) Answer: A Explanation: On the HSL color wheel, 0 degrees (and 360 degrees) corresponds to Red, 120 degrees is Green, and 240 degrees is Blue.
5. Why is background-color: rgba(0, 0, 0, 0.5) usually preferred over opacity: 0.5 for card backgrounds?
A. Because rgba works on older browsers from 1995 B. Because rgba only makes the background transparent, keeping text crisp and readable C. Because opacity requires an expensive software license D. Because rgba uses less computer memory Answer: B Explanation: Applying opacity: 0.5 makes the element and all child elements (including text and icons) transparent. In contrast, rgba() applies transparency exclusively to the background color.
Practice Challenge (Try It Yourself)
- 1Open your code editor and create
palette.html. - 2Build a color testing dashboard using all 4 formats:
- 1Open the file in your browser to admire your 4 colorful cards.
- 2Try changing the lightness of
.color-hslfrom50%to30%(dark purple) and then80%(light pastel lavender) to see how intuitive HSL is! 🎯
Background Color, Image, Position, and Size
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Grouping and Combinator Selectors | Background Color, Image, Position, and Size |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.