CSS Colors
Learn how to work with colors in CSS using various color systems and values
What are CSS Colors?
CSS provides several ways to specify colors for text, backgrounds, borders, and other elements. Understanding the different color formats and when to use them is essential for web design.
Color Properties:
- color - Sets the text color of an element
- background-color - Sets the background color of an element
- border-color - Sets the color of an element's border
- outline-color - Sets the color of an element's outline
- text-decoration-color - Sets the color of text decorations
- column-rule-color - Sets the color of the rule between columns
- caret-color - Sets the color of the cursor in inputs and textareas
CSS Color Properties Reference
Property | Description | Values |
---|---|---|
color | Sets the color of text and text decorations | Any valid color value |
background-color | Sets the background color of an element | Any valid color value |
border-color | Sets the color of an element's border | Any valid color value |
outline-color | Sets the color of an element's outline | Any valid color value |
text-decoration-color | Sets the color of text decorations | Any valid color value |
column-rule-color | Sets the color of the rule between columns | Any valid color value |
caret-color | Sets the color of the cursor in inputs | Any valid color value |
CSS Colors in Action
Example Code
/* CSS Color Values and Properties */ /* Named colors */ .element { color: red; background-color: blue; border-color: green; } /* Hexadecimal values */ .hex { color: #ff0000; /* Red */ background: #00ff00; /* Green */ border: #0000ff; /* Blue */ } /* RGB values */ .rgb { color: rgb(255, 0, 0); /* Red */ background: rgb(0, 255, 0); /* Green */ border: rgb(0, 0, 255); /* Blue */ } /* RGBA values (with transparency) */ .rgba { color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */ background: rgba(0, 255, 0, 0.3); /* Light green with transparency */ } /* HSL values */ .hsl { color: hsl(0, 100%, 50%); /* Red */ background: hsl(120, 100%, 50%); /* Green */ border: hsl(240, 100%, 50%); /* Blue */ } /* HSLA values (with transparency) */ .hsla { color: hsla(0, 100%, 50%, 0.5); /* Semi-transparent red */ background: hsla(120, 100%, 50%, 0.3); /* Light green with transparency */ } /* CurrentColor keyword */ .current-color { color: blue; border: 2px solid currentColor; /* Border uses same color as text */ } /* Transparent keyword */ .transparent { background-color: transparent; border: 1px solid transparent; } /* Color functions */ .color-functions { background-color: color-mix(in srgb, red 30%, blue 70%); color: color(display-p3 1 0.5 0); } /* Gradient backgrounds */ .gradient { background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet); background: radial-gradient(circle, red, yellow, green); } /* System colors */ .system { color: ButtonText; background-color: ButtonFace; border-color: ActiveBorder; } /* Color manipulation with CSS variables */ :root { --primary-color: #3498db; --primary-light: color-mix(in srgb, var(--primary-color) 20%, white); --primary-dark: color-mix(in srgb, var(--primary-color) 20%, black); } .dynamic-colors { background-color: var(--primary-color); color: var(--primary-light); border-color: var(--primary-dark); }
Color Values in Detail
Named Colors
CSS provides 140+ named colors that are easy to remember and use.
Hexadecimal Values
Hexadecimal colors are specified with #RRGGBB, where RR, GG, and BB are hexadecimal values (00-FF).
RGB and RGBA Values
RGB defines colors using Red, Green, and Blue components (0-255). RGBA adds an Alpha channel for transparency (0-1).
HSL and HSLA Values
HSL defines colors using Hue (0-360), Saturation (0-100%), and Lightness (0-100%). HSLA adds an Alpha channel for transparency (0-1).
Special Keywords
currentColor
Represents the value of an element's color property.
transparent
Fully transparent color (equivalent to rgba(0,0,0,0)).
Color Functions
color-mix()
The color-mix() function allows you to mix two colors in a specified color space.
color()
The color() function allows specifying colors in different color spaces.
Supported color spaces:
- srgb
- display-p3
- a98-rgb
- prophoto-rgb
- rec2020