Complete CSS Colors Guide
Learn everything about CSS colors, including HEX, RGB, HSL, RGBA, named colors, gradients, transparency, variables and accessibility with practical examples.
1. What are CSS Colors?
CSS provides several ways to define colors, including HEX, RGB, RGBA, HSL, HSLA and predefined color names. Choosing the right format makes your styles easier to maintain while helping create consistent, accessible and visually appealing websites.
2. CSS Color Formats
Stylesheets support multiple functions to represent color coordinates:
color: #3498DB; /* 6-digit hex */
color: #FFF; /* 3-digit shorthand */
color: rgb(52, 152, 219);
color: rgba(52, 152, 219, 0.5); /* 50% opacity */
color: hsl(204, 70%, 53%);
color: hsla(204, 70%, 53%, 0.5);
3. Predefined Named Colors
CSS includes 148 predefined color keywords like red, gold, tomato, rebeccapurple, and cornflowerblue.
4. Custom CSS Variables
Declaring color variables inside the :root selector helps you manage themes and avoid hardcoding color codes.
:root {
--primary-color: #3498DB;
--secondary-color: #2ECC71;
}
.button {
background-color: var(--primary-color);
border-color: var(--secondary-color);
}5. Every CSS Color Property
You can style various parts of your elements using specific CSS color properties:
colorSets text color.background-colorSets background solid color.border-colorSets border stroke color.outline-colorSets outline highlight color.text-decoration-colorSets underline/strike-through colors.caret-colorSets input text cursor color.accent-colorSets checkboxes/radios branding accents.column-rule-colorSets columns divider line colors.fillSets SVG vectors interior fill color.strokeSets SVG vectors border stroke color.6. Opacity & Transparency
Transparency can be declared in several ways in CSS:
- Opacity Property: Applying
opacity: 0.5;makes the entire element (including text and children) translucent. - Alpha Channels: Using
rgba(),hsla(), or 8-digit HEX leaves child elements fully opaque. - transparent Keyword: Sets background transparency without affecting text.
- currentColor Keyword: Automatically inherits the text color of the current element.
7. CSS Color Level 4 & 5
Modern browsers support wide color gamuts that go beyond the standard sRGB space.
CSS Color Level 4: Wide-Gamut Spaces
Functions like lab(), lch(), hwb(), oklab(), and oklch() allow monitors to project brighter, more saturated reds, greens, and blues.
CSS Color Level 5: Dynamic Color Functions
Introduces color mixing functions like color-mix() and Relative Color Syntax (RCS) to modify color values directly in your stylesheets:
/* CSS Level 5 examples */ background-color: color-mix(in srgb, var(--primary) 80%, white); background-color: rgb(from var(--primary) r g b / 50%); /* RCS */
8. Variables in Frameworks
Color variables can be declared in popular design frameworks like Tailwind CSS, Bootstrap, and Material Design:
theme: { extend: { colors: { brand: '#3498db' } } }$primary: #3498db;
palette: { primary: { main: '#3498db' } }9. Dark Mode Implementation
You can implement dark mode easily by redefining your CSS variables inside media queries or target classes:
:root {
--background: #FFFFFF;
--text: #1E293B;
--border: #E2E8F0;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0F172A;
--text: #F8FAFC;
--border: #334155;
}
}10. WCAG Accessibility Rules
To ensure readable contrast between text and background colors, you must comply with WCAG accessibility standards:
- WCAG AA Standard: Requires a minimum contrast ratio of 4.5:1 for body copy and 3.0:1 for large text.
- WCAG AAA Standard: Requires a minimum contrast ratio of 7.0:1 for body copy and 4.5:1 for large text.
11. Comparison Tables
HEX vs RGB
| Feature | HEX Format | RGB Format |
|---|---|---|
| Syntax | #3498DB | rgb(52, 152, 219) |
| Opacity support | 8-digit Hex | rgba() Function |
RGB vs HSL
| Feature | RGB Format | HSL Format |
|---|---|---|
| Coordinates | Red, Green, Blue amounts | Hue, Saturation, Lightness parameters |
| Vibrancy adjustments | Hard | Easy |
RGBA vs Opacity
| Feature | RGBA Color | Opacity Property |
|---|---|---|
| Element Scope | Applies transparency only to the styled element. | Applies transparency to the element and all its children. |
| Inheritance | Children remain opaque. | Children inherit transparency levels. |
12. CSS Design Examples
.btn { background-color: #3498DB; color: white; border: 1px solid #2980B9; }.alert { background: #FADBD8; border-left: 5px solid #E74C3C; color: #78281F; }13. Frequently Asked Questions
What are CSS colors?
CSS colors are value specifications applied inside stylesheets to paint element text highlights, background sheets, margins, rules, and overlay shadows.
Which color format is best?
HEX is best for standard color constants, RGB is useful for opacity math (RGBA), and HSL is excellent for modifying saturation and lightness programmatically.
Does CSS support HEX?
Yes, CSS natively supports 3-digit shorthand, 6-digit standard, and 8-digit hexadecimal transparency notation.
Does CSS support RGB?
Yes, via the rgb() or rgba() functions, supporting comma-separated or modern space-separated specifications.
What is RGBA?
RGBA adds a fourth channel called alpha (0.0 to 1.0) to specify element transparency.
What is HSL?
HSL defines colors using Hue angle degrees (0-360), Saturation percentages, and Lightness percentages.
What is currentColor?
currentColor is a special CSS keyword that references the computed text color value of the current element.
What is transparent?
transparent is a keyword representing a fully clear color coordinate, mathematically equivalent to rgba(0, 0, 0, 0).
How many named colors exist?
There are exactly 148 named color keywords recognized by CSS standards (including duplicate spellings like gray and grey).
Should I use CSS variables?
Yes, CSS variables allow you to store theme color tokens globally inside :root, making dark mode swaps easy.
Are gradients CSS colors?
Gradients are classified as CSS images rather than solid colors, but they are created by declaring multiple color stop transition values.
Is RGBA deprecated?
No, RGBA is not deprecated. However, modern CSS Level 4 merges rgb() and rgba() so that rgb() natively accepts alpha parameters without the "a".
Which browsers support HSL?
All modern desktop and mobile browsers have supported HSL color declarations for over a decade.
Can CSS use CMYK?
No, CSS color spaces are designed for screen display illumination, so CMYK values are not supported directly in web layouts.
Can CSS use LAB colors?
Yes! CSS Color Level 4 introduces the lab(), lch(), oklab(), and oklch() color spaces for wide-gamut monitors.