Lesson 10: Mastering CSS Colors
Learn how to use RGB, HEX, HSL, and named colors to create visually appealing designs with comprehensive examples and interactive demos.
Start LearningCSS Color Systems
CSS provides multiple ways to specify colors, each with its own advantages. Understanding these systems helps you create consistent designs and work with existing color schemes.
Color Systems Comparison
rgb(red, green, blue)
rgb(255, 0, 0)
rgba(red, green, blue, alpha)
rgba(255, 0, 0, 0.5)
#RRGGBB
#ff0000
hsl(hue, saturation, lightness)
hsl(0, 100%, 50%)
colorname
red
RGB & RGBA Colors
RGB (Red, Green, Blue) is an additive color model where colors are created by combining different intensities of red, green, and blue light.
RGB Syntax
/* RGB syntax */ selector { color: rgb(red, green, blue); background-color: rgb(red, green, blue); } /* RGBA syntax (with transparency) */ selector { color: rgba(red, green, blue, alpha); }
Interactive RGB Demo
HEX Color Codes
HEX color codes are six-digit hexadecimal numbers that represent RGB values. They are the most common way to specify colors in web development.
HEX Syntax
selector { color: #RRGGBB; background-color: #RRGGBB; } /* Shorthand for identical pairs */ selector { color: #RGB; /* Expands to #RRGGBB */ }
HEX Shorthand Notation
When both digits of each component are the same, you can use the shorthand three-digit notation:
HSL & HSLA Colors
HSL (Hue, Saturation, Lightness) provides a more intuitive way to work with colors by representing them as points in a color wheel.
HSL Syntax
/* HSL syntax */ selector { color: hsl(hue, saturation, lightness); } /* HSLA syntax (with transparency) */ selector { color: hsla(hue, saturation, lightness, alpha); }
Saturation and Lightness Variations
Adjusting saturation and lightness creates variations of the same hue:
Named Colors
CSS provides 147 predefined color names for convenience. These are easier to remember but offer less precision than other methods.
Common Named Colors
Extended Color Names
Practical Applications
CSS colors are used in various properties to style different aspects of web elements:
/* Text color */ h1 { color: #2c3e50; } /* Background color */ .container { background-color: #f8f9fa; } /* Border color */ .card { border: 2px solid rgba(0, 0, 0, 0.1); } /* Box shadow */ .button { box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); } /* Gradient background */ .hero { background: linear-gradient( to right, #1a237e, #4a148c ); }
CSS Colors in Action
This container has a light background color
This card has a subtle border
Creating a Color Palette
Best Practices
Follow these guidelines for effective use of colors in CSS:
Accessible Color Combinations
Next Steps
Now that you understand CSS colors, continue to the next lesson to learn about CSS opacity and transparency effects.