Colors & Backgrounds
color, background, gradients, opacity
Skills taught: css.colors, css.backgrounds, css.gradients
Colour formats
p {
/* Named — 140+ browser colour keywords */
color: teal;
/* Hex — #RRGGBB or shorthand #RGB */
color: #4fd1c5;
color: #4dc; /* same as #44ddcc */
/* RGB — 0-255 per channel */
color: rgb(79, 209, 197);
/* RGBA — with alpha transparency (0=invisible, 1=solid) */
color: rgba(79, 209, 197, 0.5);
/* HSL — hue(0-360) saturation% lightness% */
color: hsl(174, 56%, 57%);
/* OKLCH — modern perceptual colour space */
color: oklch(75% 0.12 194);
}
Background properties
.card {
background-color: #1a1f2b;
/* Background image */
background-image: url('texture.png');
background-size: cover; /* or contain, or 100% 200px */
background-position: center;
background-repeat: no-repeat;
/* Shorthand */
background: #1a1f2b url('texture.png') no-repeat center / cover;
}
Gradients
.hero {
/* Linear gradient — from one colour to another */
background: linear-gradient(135deg, #0e1116, #1a1f2b);
/* Linear with multiple stops */
background: linear-gradient(to right, #4fd1c5, #7c3aed, #e34c26);
/* Radial gradient */
background: radial-gradient(circle at center, #1a1f2b, #0e1116);
}
/* Gradient text — requires background-clip */
.gradient-text {
background: linear-gradient(to right, #4fd1c5, #7c3aed);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
Opacity and transparency
/* opacity affects the ENTIRE element including children */
.overlay { opacity: 0.8; }
/* rgba/hsla affects only that colour — children stay solid */
.glass {
background: rgba(26, 31, 43, 0.7);
backdrop-filter: blur(8px); /* frosted glass effect */
}
Frequently Asked Questions
Which colour format should I use — hex, rgb, or hsl?
Hex (#4fd1c5) for static brand colours — it's compact and universally supported. hsl() when you need to programmatically lighten/darken. rgba() when you need transparency. oklch() for the best perceptual consistency in modern browsers.
What is backdrop-filter?
backdrop-filter applies a filter (like blur, brightness) to the content BEHIND an element — creating a frosted-glass effect. It requires the element to have a semi-transparent background to show the effect.
How do I pick accessible colour combinations?
Use a contrast checker like WebAIM Contrast Checker. WCAG AA requires a minimum contrast ratio of 4.5:1 for normal text and 3:1 for large text against the background.