CSS Color Properties — A Complete Guide for Web Developers
Master every CSS color property from color and background-color to gradients and opacity. A comprehensive reference for web developers with practical examples.
The Basics: color and background-color
The two most fundamental CSS color properties are color (for text) and background-color (for element backgrounds). They accept any valid CSS color value including named colors, HEX, RGB, HSL, and modern color functions.
The color property sets the foreground color of an element, which primarily affects text content. It is also used as the default color for borders, text decorations, and other visual elements that do not have their own color specified.
The background-color property fills the entire content and padding area of an element. It sits behind any background images and is often used as a fallback when background images fail to load. Unlike background-image, background-color does not create a new stacking context.
Modern CSS Color Functions
CSS has evolved beyond basic rgb() and hsl() with powerful new color functions:
The rgb() function now supports the modern space-separated syntax: rgb(255 0 0) instead of the legacy rgb(255, 0, 0). Both syntaxes work in all modern browsers. Alpha transparency uses a slash: rgb(255 0 0 / 50%).
The hsl() function works similarly: hsl(210 100% 50%) for a vivid blue. HSL is particularly useful for creating color variations because you can adjust lightness and saturation independently.
The newer color-mix() function lets you blend two colors: color-mix(in srgb, red 30%, blue) creates a mix of 30% red and 70% blue. This is powerful for creating intermediate colors without manual calculation.
The oklch() and oklab() functions provide perceptually uniform color spaces, meaning equal changes in values produce visually equal changes in color. oklch(0.7 0.15 180) gives you precise control over lightness, chroma, and hue.
Gradients: Linear, Radial, and Conic
CSS gradients create smooth color transitions and are set using the background-image property (or the background shorthand).
Linear gradients flow in a straight line: background: linear-gradient(to right, #ff0000, #0000ff) creates a red-to-blue horizontal gradient. You can specify any angle: linear-gradient(45deg, red, blue). Multiple color stops create complex gradients: linear-gradient(to bottom, red, yellow 30%, green 60%, blue).
Radial gradients radiate from a center point: background: radial-gradient(circle, red, blue) creates a circular gradient. You can control the shape (circle or ellipse), size, and position: radial-gradient(ellipse at top left, red, transparent).
Conic gradients sweep around a center point like a pie chart: background: conic-gradient(red, yellow, green, blue, red). They are excellent for creating color wheels, pie charts, and decorative patterns.
Opacity and Transparency
CSS offers multiple ways to control transparency:
The opacity property affects an entire element and all its children: opacity: 0.5 makes everything 50% transparent. This is useful for hover effects and transitions but cannot be used to make only the background transparent.
Alpha channels in color values affect only the specific property they are applied to. background-color: rgb(255 0 0 / 50%) makes just the background semi-transparent while keeping text fully opaque. This is usually what you want for UI elements.
The transparent keyword is a valid color value equal to rgba(0, 0, 0, 0). It is commonly used in gradients: linear-gradient(to bottom, black, transparent).
The currentColor keyword inherits the current text color, which is useful for keeping SVG icons, borders, and shadows in sync with text color without hardcoding values.
CSS Custom Properties for Color Systems
CSS custom properties (variables) are the foundation of modern color systems. They let you define colors once and reuse them throughout your stylesheet:
:root { --color-primary: #3b82f6; --color-text: #1f2937; --color-bg: #ffffff; }
Use them with var(): color: var(--color-primary). The real power comes from combining them with color functions for dynamic themes:
:root { --hue-primary: 210; --color-primary: hsl(var(--hue-primary) 100% 50%); --color-primary-light: hsl(var(--hue-primary) 100% 90%); --color-primary-dark: hsl(var(--hue-primary) 100% 30%); }
For dark mode, simply override the variables: @media (prefers-color-scheme: dark) { :root { --color-text: #f9fafb; --color-bg: #111827; } }
Our color picker tool generates color values in all CSS formats instantly. Pick a color from any image and get the exact CSS code you need — whether it is HEX for quick use, HSL for theme systems, or oklch for perceptually uniform palettes.
