CSS provides multiple ways to specify colors for your web elements. You can use predefined color names, hexadecimal codes, RGB values, HSL values, and CSS variables. Understanding these different color formats helps you create visually appealing and consistent designs.
CSS has 140 predefined color names that you can use directly. These are easy to remember and perfect for quick prototyping:
/* Basic color names */
h1 {
color: red;
}
p {
color: blue;
}
div {
background-color: lightgray;
}
button {
background-color: green;
color: white;
}
| Color Name | Hex Code | Preview |
|---|---|---|
red |
#FF0000 | |
blue |
#0000FF | |
green |
#008000 | |
yellow |
#FFFF00 | |
black |
#000000 | |
white |
#FFFFFF |
Hexadecimal (hex) codes are the most common way to specify colors in CSS. A hex code starts with # followed by 6 hexadecimal digits:
/* Hex color format: #RRGGBB */
.header {
background-color: #2e7d52; /* Dark green */
}
.button {
background-color: #007bff; /* Bright blue */
color: #ffffff; /* White */
}
.text {
color: #333333; /* Dark gray */
}
If the hex code has three pairs of identical digits, you can use the 3-digit shorthand:
/* Full hex codes */
#FF0000 /* Red */
#00FF00 /* Green */
#0000FF /* Blue */
#FFFFFF /* White */
#000000 /* Black */
/* Short hex codes (equivalent) */
#F00 /* Red */
#0F0 /* Green */
#00F /* Blue */
#FFF /* White */
#000 /* Black */
RGB (Red, Green, Blue) colors specify the intensity of each color channel using values from 0 to 255:
/* RGB format: rgb(red, green, blue) */
.element {
color: rgb(255, 0, 0); /* Red */
background-color: rgb(0, 128, 0); /* Dark green */
border-color: rgb(0, 0, 255); /* Blue */
}
| RGB Value | Hex Code | Color |
|---|---|---|
rgb(255, 0, 0) |
#FF0000 | |
rgb(0, 255, 0) |
#00FF00 | |
rgb(0, 0, 255) |
#0000FF | |
rgb(128, 128, 128) |
#808080 |
RGBA adds an alpha channel (transparency) to RGB colors. The alpha value ranges from 0.0 (fully transparent) to 1.0 (fully opaque):
/* RGBA format: rgba(red, green, blue, alpha) */
.overlay {
background-color: rgba(0, 0, 0, 0.5); /* 50% transparent black */
}
.button {
background-color: rgba(255, 0, 0, 0.8); /* 80% opaque red */
}
.text {
color: rgba(255, 255, 255, 0.9); /* 90% opaque white */
}
.transparent-25 { background-color: rgba(255, 0, 0, 0.25); } /* 25% opacity */
.transparent-50 { background-color: rgba(255, 0, 0, 0.50); } /* 50% opacity */
.transparent-75 { background-color: rgba(255, 0, 0, 0.75); } /* 75% opacity */
.transparent-100 { background-color: rgba(255, 0, 0, 1.0); } /* 100% opacity */
HSL (Hue, Saturation, Lightness) is an intuitive color model that's easier for humans to understand:
/* HSL format: hsl(hue, saturation%, lightness%) */
/* Hue: 0-360 (degrees on color wheel)
Saturation: 0-100% (gray to full color)
Lightness: 0-100% (black to white) */
.element {
color: hsl(0, 100%, 50%); /* Red */
background-color: hsl(120, 100%, 50%); /* Green */
border-color: hsl(240, 100%, 50%); /* Blue */
}
| HSL Value | Description | Color |
|---|---|---|
hsl(0, 100%, 50%) |
Pure Red | |
hsl(120, 100%, 50%) |
Pure Green | |
hsl(240, 100%, 50%) |
Pure Blue | |
hsl(60, 100%, 50%) |
Yellow | |
hsl(0, 0%, 50%) |
Gray |
HSLA adds transparency to HSL colors, similar to how RGBA adds transparency to RGB:
/* HSLA format: hsla(hue, saturation%, lightness%, alpha) */
.overlay {
background-color: hsla(0, 0%, 0%, 0.7); /* 70% transparent black */
}
.button {
background-color: hsla(120, 100%, 50%, 0.8); /* 80% opaque green */
}
.text-shadow {
text-shadow: 2px 2px 4px hsla(0, 0%, 0%, 0.5); /* 50% transparent black shadow */
}
CSS custom properties (variables) make it easy to manage colors throughout your website:
/* Define color variables */
:root {
--primary-color: #2e7d52;
--secondary-color: #6c757d;
--success-color: #28a745;
--danger-color: #dc3545;
--warning-color: #ffc107;
--info-color: #17a2b8;
/* Light variants */
--primary-light: #4caf50;
--secondary-light: #adb5bd;
/* Dark variants */
--primary-dark: #1b5e20;
--secondary-dark: #495057;
/* Neutral colors */
--white: #ffffff;
--gray-100: #f8f9fa;
--gray-200: #e9ecef;
--gray-300: #dee2e6;
--gray-400: #ced4da;
--gray-500: #adb5bd;
--gray-600: #6c757d;
--gray-700: #495057;
--gray-800: #343a40;
--gray-900: #212529;
--black: #000000;
}
/* Use color variables */
.header {
background-color: var(--primary-color);
color: var(--white);
}
.button {
background-color: var(--primary-color);
border: 1px solid var(--primary-dark);
}
.button:hover {
background-color: var(--primary-light);
}
CSS provides functions to manipulate colors dynamically:
The currentColor keyword uses the value of the color property:
.button {
color: #2e7d52;
border: 2px solid currentColor; /* Uses the same green as the text */
box-shadow: 0 0 10px currentColor; /* Green shadow */
}
Modern CSS supports color mixing functions:
/* Mix two colors */
.element {
background-color: color-mix(in srgb, #ff0000, #0000ff 50%); /* 50% red, 50% blue */
}
/* Adjust color lightness */
.element {
background-color: color-adjust(#ff0000, 20%); /* Lighter red */
}
/* Using different shades of the same color */
:root {
--blue-50: #e3f2fd;
--blue-100: #bbdefb;
--blue-200: #90caf9;
--blue-300: #64b5f6;
--blue-400: #42a5f5;
--blue-500: #2196f3;
--blue-600: #1e88e5;
--blue-700: #1976d2;
--blue-800: #1565c0;
--blue-900: #0d47a1;
}
.header { background-color: var(--blue-900); }
.content { background-color: var(--blue-50); }
.button { background-color: var(--blue-500); }
/* Opposite colors on the color wheel */
:root {
--primary: #2196f3; /* Blue */
--complement: #ff9800; /* Orange */
--primary-light: #e3f2fd;
--complement-light: #fff3e0;
}
.primary-section { background-color: var(--primary); }
.accent-section { background-color: var(--complement); }
Ensure your color choices are accessible to all users:
| Background | Text | Contrast Ratio | WCAG Level |
|---|---|---|---|
| White | Black | 21:1 | AAA |
| Blue | White | 4.5:1 | AA |
--primary-color not --blue)| Color Name | Hex | RGB | HSL |
|---|---|---|---|
| Red | #FF0000 | rgb(255,0,0) | hsl(0,100%,50%) |
| Green | #008000 | rgb(0,128,0) | hsl(120,100%,25%) |
| Blue | #0000FF | rgb(0,0,255) | hsl(240,100%,50%) |
| Yellow | #FFFF00 | rgb(255,255,0) | hsl(60,100%,50%) |
| Cyan | #00FFFF | rgb(0,255,255) | hsl(180,100%,50%) |
| Magenta | #FF00FF | rgb(255,0,255) | hsl(300,100%,50%) |