Work with colors, gradients, and background properties to create beautiful designs.
CSS colors allow you to specify the color of text, backgrounds, borders, and other elements. CSS supports multiple color formats and provides powerful tools for creating gradients and effects.
CSS provides 148 predefined color names.
/* Basic colors */
.red { color: red; }
.blue { color: blue; }
.green { color: green; }
/* Extended colors */
.crimson { color: crimson; }
.teal { color: teal; }
.orange { color: orange; }
/* Gray scale */
.black { color: black; }
.gray { color: gray; }
.white { color: white; }
Six-digit hex codes representing RGB values.
/* Full hex codes */
.red { color: #ff0000; }
.green { color: #00ff00; }
.blue { color: #0000ff; }
/* Short hex codes (when digits repeat) */
.red { color: #f00; }
.green { color: #0f0; }
.blue { color: #00f; }
/* Common colors */
.black { color: #000000; }
.white { color: #ffffff; }
.gray { color: #808080; }
Red, Green, Blue values from 0-255.
/* RGB values */
.red { color: rgb(255, 0, 0); }
.green { color: rgb(0, 255, 0); }
.blue { color: rgb(0, 0, 255); }
/* RGBA with alpha (transparency) */
.transparent-red {
color: rgba(255, 0, 0, 0.5); /* 50% transparent */
}
.semi-transparent {
background-color: rgba(0, 0, 0, 0.7); /* 70% opaque black */
}
Hue, Saturation, Lightness values.
/* HSL values */
.red { color: hsl(0, 100%, 50%); }
.green { color: hsl(120, 100%, 50%); }
.blue { color: hsl(240, 100%, 50%); }
/* HSLA with alpha */
.transparent-blue {
color: hsla(240, 100%, 50%, 0.5);
}
/* Different lightness values */
.light-red { color: hsl(0, 100%, 75%); }
.dark-red { color: hsl(0, 100%, 25%); }
Create smooth color transitions in a straight line.
/* Basic linear gradient */
.gradient {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}
/* With direction */
.top-to-bottom {
background: linear-gradient(to bottom, red, blue);
}
.left-to-right {
background: linear-gradient(to right, red, blue);
}
/* With angle */
.angled {
background: linear-gradient(45deg, red, blue);
}
/* Multiple color stops */
.multi-color {
background: linear-gradient(to right, red, yellow, green, blue);
}
/* With transparency */
.transparent-gradient {
background: linear-gradient(to right, rgba(255,0,0,0.8), rgba(0,0,255,0.8));
}
Create circular or elliptical color transitions.
/* Basic radial gradient */
.radial {
background: radial-gradient(circle, #ff6b6b, #4ecdc4);
}
/* Elliptical gradient */
.elliptical {
background: radial-gradient(ellipse, red, blue);
}
/* With position */
.positioned {
background: radial-gradient(circle at top left, red, blue);
}
/* Multiple color stops */
.multi-radial {
background: radial-gradient(circle, red, yellow, green, blue);
}
Set solid background colors for elements.
/* Basic background color */
.element {
background-color: #f0f0f0;
}
/* With transparency */
.transparent-bg {
background-color: rgba(0, 0, 0, 0.5);
}
/* Using different color formats */
.hex-bg { background-color: #ff0000; }
.rgb-bg { background-color: rgb(255, 0, 0); }
.hsl-bg { background-color: hsl(0, 100%, 50%); }
Use images as backgrounds.
/* Basic background image */
.element {
background-image: url('image.jpg');
}
/* Background image with color fallback */
.element {
background-color: #f0f0f0;
background-image: url('image.jpg');
}
/* Multiple background images */
.layered {
background-image:
url('overlay.png'),
url('background.jpg');
}
Control how background images are displayed.
/* Background size */
.cover {
background-size: cover; /* Cover entire element */
}
.contain {
background-size: contain; /* Fit within element */
}
.specific {
background-size: 200px 100px; /* Specific dimensions */
}
/* Background position */
.top-left {
background-position: top left;
}
.center {
background-position: center;
}
.custom {
background-position: 20% 50%;
}
/* Background repeat */
.no-repeat {
background-repeat: no-repeat;
}
.repeat-x {
background-repeat: repeat-x;
}
.repeat-y {
background-repeat: repeat-y;
}
Combine multiple background properties.
/* Complete background shorthand */
.element {
background: #f0f0f0 url('image.jpg') no-repeat center/cover;
}
/* Breaking it down:
- #f0f0f0: background-color
- url('image.jpg'): background-image
- no-repeat: background-repeat
- center: background-position
- cover: background-size
*/
/* Multiple backgrounds */
.layered {
background:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
url('image.jpg') no-repeat center/cover;
}
/* Define color palette */
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
--success-color: #28a745;
--danger-color: #dc3545;
--warning-color: #ffc107;
--info-color: #17a2b8;
--light-color: #f8f9fa;
--dark-color: #343a40;
}
/* Use variables */
.button {
background-color: var(--primary-color);
color: white;
}
.success {
background-color: var(--success-color);
color: white;
}