Colors & Backgrounds
Every way to define color in CSS and the full background toolkit — from simple hex codes to advanced gradients, blend modes, and multi-layer compositions.
Named Colors
CSS provides 148 named colors (plus transparent and currentColor). They are easy to remember but offer limited palette control.
.box { color: tomato; } .bg { background-color: cornflowerblue; } .bdr { border-color: mediumseagreen; } .hid { color: transparent; }
Hex Colors
Hexadecimal notation expresses colors as #RRGGBB. A shorthand 3-digit form exists, and an 8-digit form adds alpha transparency.
/* 6-digit hex */ .red { color: #ff0000; } .brand { color: #646cff; } /* 3-digit shorthand (#RGB expands to #RRGGBB) */ .white { color: #fff; } /* same as #ffffff */ .cyan { color: #0ff; } /* same as #00ffff */ /* 8-digit with alpha (#RRGGBBAA) */ .semi { color: #646cff80; } /* 50% opacity */ .light { color: #646cff33; } /* 20% opacity */
00 = fully transparent, ff = fully opaque. Common values: 80 = 50%, cc = 80%, 33 = 20%, 66 = 40%.
rgb() / rgba()
Specify colors with Red, Green, Blue channels (0–255 or 0%–100%), plus an optional alpha channel (0–1).
/* Modern syntax (no commas needed) */ .a { color: rgb(100 108 255); } .b { color: rgb(100 108 255 / 0.5); } /* Legacy syntax (commas) */ .c { color: rgb(100, 108, 255); } .d { color: rgba(100, 108, 255, 0.5); } /* Percentage values */ .e { color: rgb(39% 42% 100%); }
G: 0, B: 0
G: 180, B: 0
B: 255
B: 0 = Yellow
B: 255 = Cyan
B: 255 = Magenta
Alpha Transparency
hsl() / hsla()
Hue-Saturation-Lightness is more intuitive for humans. Hue is a degree on the color wheel (0–360), Saturation is the intensity (0%–100%), and Lightness is brightness (0%–100%).
/* Modern syntax */ .a { color: hsl(235 100% 70%); } .b { color: hsl(235 100% 70% / 0.5); } /* Legacy syntax */ .c { color: hsl(235, 100%, 70%); } .d { color: hsla(235, 100%, 70%, 0.5); } /* Hue values: 0=red, 120=green, 240=blue */
Hue rotates around the color wheel at full saturation (100%) and medium lightness (50%):
Saturation Scale (Hue: 235, Lightness: 50%)
Lightness Scale (Hue: 235, Saturation: 100%)
With HSL, creating color variants is trivial: keep the hue, adjust saturation and lightness. Dark variant? Lower lightness. Muted? Lower saturation. This makes building consistent color palettes much easier than with hex or RGB.
currentColor
The currentColor keyword refers to the element's computed color value. It lets borders, shadows, and backgrounds automatically match the text color.
.btn { color: #646cff; border: 2px solid currentColor; box-shadow: 0 2px 8px currentColor; } /* Change color once, everything updates */ .btn:hover { color: #10b981; }
opacity vs Alpha Channel
Both control transparency, but they behave differently.
/* opacity affects the ENTIRE element and its children */ .card { opacity: 0.5; } /* Alpha only affects that specific color */ .card { background: rgba(100, 108, 255, 0.5); }
Text is ALSO faded! Everything becomes transparent.
Text stays crisp! Only the background is translucent.
opacity makes the entire element and all its children transparent. You cannot make a child "more opaque" than its parent. Use alpha channels (rgba/hsla) on individual properties instead when you only want to fade the background.
background-color
Sets a solid color behind the element's content. Accepts any color value.
.solid { background-color: #646cff; } .trans { background-color: rgba(100, 108, 255, 0.2); } .named { background-color: cornflowerblue; } .hsl { background-color: hsl(235, 100%, 70%); }
background-image
Sets one or more images (or gradients) as the background. Images layer on top of background-color.
.photo { background-image: url("hero.jpg"); } .gradient { background-image: linear-gradient(135deg, #646cff, #10b981); } /* Multiple backgrounds (first listed is on top) */ .layered { background-image: linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,.5)), url("hero.jpg"); }
background-size
Controls how a background image is scaled inside its container.
.cover { background-size: cover; } /* fill container, may crop */ .contain { background-size: contain; } /* fit inside, may leave gaps */ .exact { background-size: 200px 100px; } .pct { background-size: 50% auto; }
background-position
Sets where the background image is placed within the element.
.a { background-position: center; } .b { background-position: top right; } .c { background-position: 50% 25%; } .d { background-position: 20px 40px; } .e { background-position: bottom 10px right 20px; }
background-repeat
Controls whether and how a background image tiles.
.a { background-repeat: repeat; } /* default: tile both axes */ .b { background-repeat: no-repeat; } /* show once */ .c { background-repeat: repeat-x; } /* horizontal only */ .d { background-repeat: repeat-y; } /* vertical only */ .e { background-repeat: space; } /* even spacing, no clipping */ .f { background-repeat: round; } /* stretch to fill evenly */
background-attachment
Controls whether the background scrolls with the content or stays fixed in the viewport.
.scroll { background-attachment: scroll; } /* default */ .fixed { background-attachment: fixed; } /* parallax effect */ .local { background-attachment: local; } /* scrolls with content */
Setting background-attachment: fixed creates a simple parallax scrolling effect. The background stays in place while the content scrolls over it. Note: this is disabled on most mobile browsers for performance reasons.
background-clip
Defines how far the background extends within the element. The text value creates gradient text effects.
.a { background-clip: border-box; } /* default */ .b { background-clip: padding-box; } .c { background-clip: content-box; } /* Gradient text */ .gradient-text { background: linear-gradient(135deg, #646cff, #10b981); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; color: transparent; }
Gradient Text Effect
Rainbow gradient clipped to text
linear-gradient()
Creates a smooth color transition along a straight line. You can control the angle and add multiple color stops.
/* Direction keywords */ .a { background: linear-gradient(to right, #646cff, #10b981); } .b { background: linear-gradient(to bottom right, #646cff, #10b981); } /* Angle (0deg = to top, 90deg = to right) */ .c { background: linear-gradient(135deg, #646cff, #10b981); } /* Multiple stops with positions */ .d { background: linear-gradient( 90deg, #ef4444 0%, #f59e0b 25%, #10b981 50%, #646cff 75%, #a855f7 100% ); } /* Hard stop (no transition) */ .e { background: linear-gradient( 90deg, #646cff 50%, #10b981 50% ); }
Multi-stop & Hard Stops
radial-gradient()
Creates a circular or elliptical gradient radiating outward from a center point.
/* Basic circle */ .a { background: radial-gradient(circle, #646cff, #0a0a1a); } /* Ellipse (default) */ .b { background: radial-gradient(ellipse, #646cff, #0a0a1a); } /* Positioned */ .c { background: radial-gradient(circle at top left, #646cff, #0a0a1a); } /* Sized */ .d { background: radial-gradient(circle 100px, #646cff, #0a0a1a); }
conic-gradient()
Creates a gradient that rotates around a center point, like a color wheel or pie chart.
/* Color wheel */ .wheel { background: conic-gradient( red, yellow, lime, aqua, blue, magenta, red ); border-radius: 50%; } /* Pie chart */ .pie { background: conic-gradient( #646cff 0% 40%, #10b981 40% 70%, #f59e0b 70% 100% ); border-radius: 50%; }
Color Wheel
Pie Chart (40/30/30)
from 90deg
Square Quadrants
Repeating Gradients
The repeating- variants of each gradient type tile automatically.
.stripes { background: repeating-linear-gradient( 45deg, #646cff, #646cff 10px, transparent 10px, transparent 20px ); } .rings { background: repeating-radial-gradient( circle, #646cff, #646cff 5px, transparent 5px, transparent 15px ); } .starburst { background: repeating-conic-gradient( #646cff 0deg 15deg, transparent 15deg 30deg ); }
Gradient Patterns (Stripes)
By combining hard color stops and repeating gradients, you can create pure-CSS patterns with zero images.
/* Horizontal stripes */ .h-stripes { background: repeating-linear-gradient( 0deg, #646cff 0px, #646cff 10px, transparent 10px, transparent 20px ); } /* Checkerboard */ .check { background-color: #1a1a2e; background-image: linear-gradient(45deg, #646cff33 25%, transparent 25%), linear-gradient(-45deg, #646cff33 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #646cff33 75%), linear-gradient(-45deg, transparent 75%, #646cff33 75%); background-size: 20px 20px; background-position: 0 0, 0 10px, 10px -10px, -10px 0px; }
Horizontal Stripes
Vertical Stripes
Diagonal Stripes
Checkerboard
Grid Lines
Warning Stripes
mix-blend-mode
Controls how an element's colors blend with the content behind it, similar to Photoshop blending modes.
.overlay-text { mix-blend-mode: multiply; } /* Common values */ .a { mix-blend-mode: normal; } .b { mix-blend-mode: multiply; } /* darken */ .c { mix-blend-mode: screen; } /* lighten */ .d { mix-blend-mode: overlay; } /* contrast */ .e { mix-blend-mode: difference; } /* invert */ .f { mix-blend-mode: color-dodge; }
Overlapping Circles
Multiple Backgrounds
CSS lets you stack multiple backgrounds on a single element. Each layer can have its own image, position, size, and repeat. The first listed layer renders on top.
.layered { background: /* Layer 1 (top): semi-transparent overlay */ linear-gradient(rgba(0,0,0,.3), rgba(0,0,0,.7)), /* Layer 2: decorative gradient */ radial-gradient(circle at 30% 70%, #646cff44, transparent 50%), /* Layer 3 (bottom): base color */ linear-gradient(135deg, #0a0a1a, #1a1a3e); }
Common Gotchas
The background shorthand resets background-color, background-image, background-position, background-size, and others to their defaults. If you set background-color first, then use background: url(...), the color is wiped. Set background-color after the shorthand, or include it in the shorthand.
8-digit hex colors (#646cff80) are supported in all modern browsers, but not in IE11. If you need IE support, use rgba() instead.
Subtle gradients across large areas can show visible color bands. Mitigate this by adding a faint noise texture overlay or slightly increasing the color range of your gradient.
Most mobile browsers disable background-attachment: fixed for performance. The background will behave as scroll instead. Use JavaScript-based parallax for mobile if needed.
Setting opacity to anything less than 1 creates a new stacking context. This can affect z-index ordering of child elements and cause unexpected layering issues.
Pro Tips
Define your palette with HSL in custom properties: --hue: 235; --primary: hsl(var(--hue), 100%, 70%);. Changing --hue shifts your entire color scheme at once. This is the foundation of modern CSS theming.
Instead of hard-coding a separate hover color, adjust lightness: background: hsl(235, 100%, 60%) to background: hsl(235, 100%, 50%). Same hue, deeper shade.
You cannot directly apply a gradient to border-color. Instead, use border-image: linear-gradient(...) 1 or the background-clip padding-box trick with a pseudo-element.
CSS gradients are generated by the browser and are resolution-independent. They are cheaper than image files and look sharp on Retina displays. Prefer gradients over raster images whenever possible.
The new color-mix() function lets you blend two colors: color-mix(in srgb, #646cff, white 30%) mixes 30% white into your blue. Great for programmatic tints and shades without manually computing hex values.
Always ensure a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (WCAG AA). Use browser DevTools or online checkers to verify your color combinations.