2025-08-05 — By Siddharth Jain · 7 min read
CSS (Cascading Style Sheets) is the language that brings color, spacing, and layout to your web pages. Understanding how to use colors—and more advanced effects like gradients—opens up endless creative opportunities.
This guide covers:
You can set colors in CSS for backgrounds, text, borders, and almost any element. Here are the main color types:
CSS includes 140+ named colors that work everywhere:
Examples: red
, blue
, teal
, hotpink
, black
, white
.
h1 {
color: teal;
}
A 6-digit code for specifying exact shades:
Format: #RRGGBB
div {
background: #4caf50; /* a green shade */
}
Mix red, green, and blue with optional alpha (transparency):
p {
color: rgb(255, 0, 0); /* pure red */
background: rgba(0, 0, 0, 0.5); /* half-transparent black */
}
Set color using hue (angle), saturation (%), and lightness (%):
span {
color: hsl(200, 80%, 50%); /* vibrant blue */
background: hsla(120, 60%, 70%, 0.8); /* semi-transparent green */
}
Gradients let you blend multiple hues smoothly—no need for image backgrounds.
linear-gradient()
: Top to Bottom (or Any Angle)Creates a color fade along a straight line.
Syntax:
background: linear-gradient(direction, color1, color2, ...);
Examples:
.button {
background: linear-gradient(to right, #ff9800, #ff5e62); /* orange to pink */
}
.divider {
background: linear-gradient(90deg, #1e90ff 0%, #00fa9a 100%);
}
to right
, to bottom
, angle in degrees (e.g., 135deg
)radial-gradient()
: Circular or EllipticalColor radiates out from a center point.
Syntax:
background: radial-gradient(shape size at position, color1, color2, ...);
Examples:
.hero {
background: radial-gradient(circle at center, #fff700, #f64f59 70%);
}
.ellipse-bg {
background: radial-gradient(ellipse at top left, #009ffd, #2a2a72);
}
circle
, ellipse
) and origin (at center
, at bottom right
).background: repeating-linear-gradient(
45deg,
#222,
#222 10px,
#444 10px,
#444 20px
);
How:
Just apply to any CSS background:
(or even border-image
), for example:
.header {
background: linear-gradient(to bottom, #333, #666, #ccc);
}
Beautiful Gradient Header This area uses a CSS linear gradient and white text.
Color Type | Example Syntax | Where To Use |
---|---|---|
Named | color: red; | Quick, simple styling |
HEX | background: #f86254; | Precise web colors |
RGB/A | color: rgba(0,128,255,0.7); | Transparency/animations |
HSL/A | color: hsl(340, 100%, 45%); | Programmatic color themes |
Linear Gradient | linear-gradient(to right, c1, c2) | Buttons, cards, backgrounds |
Radial Gradient | radial-gradient(circle, c1, c2) | Hero sections, overlays |
color:
, background:
, and gradient functions for backgrounds, buttons, or any UI element.