Sub topic 1
Color Keywords and Hex Notation
Prerequisites
Assume you know basic HTML, CSS selectors, the cascade, and the box model. You should also know how to link a stylesheet.
Color Keywords and Hex Notation
CSS accepts named colors such as tomato, navy, and transparent. Hexadecimal notation represents red, green, and blue channels with pairs of digits from 00 to ff.
.warning {
color: white;
background-color: #c0392b;
}
.muted {
color: #666;
}A three-digit hex value expands each digit: #369 means #336699. Use six digits when you need precise values.
rgb() and hsl(): Channels, Hue, and Alpha
rgb() describes red, green, and blue channels. hsl() describes hue, saturation, and lightness. Both can include alpha, which controls opacity from 0 to 1.
.card {
color: #1a1a1a;
background-color: rgb(255 255 255);
border: 1px solid hsl(220 15% 80%);
box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
}The slash separates color channels from alpha. Transparent backgrounds reveal whatever is behind the element.
Reusing Colors with Custom Properties and `currentColor`
Custom properties store reusable values. currentColor means the element's computed color, so related properties stay synchronized.
:root {
--ink: #1a1a1a;
--accent: #0b5cad;
}
.link {
color: var(--accent);
border-bottom: 1px solid currentColor;
}Contrast and Accessible Color Choices
Contrast measures how clearly foreground text differs from its background. WCAG commonly recommends at least 4.5:1 for normal text and 3:1 for large text. Check the actual text size, weight, and background with a contrast checker.
Modern Color: `oklch()` and `color-mix()`
oklch() describes lightness, chroma, and hue in a perceptual color space. Similar numeric changes often produce more even visual changes than RGB or HSL. color-mix() derives a color from two inputs.
:root {
--brand: oklch(55% 0.2 260);
--brand-light: color-mix(in oklch, var(--brand), white 30%);
}
.button {
background-color: var(--brand);
color: white;
}
.button:hover {
background-color: var(--brand-light);
}Exercise
Which notation should you use to make a border follow an element's text color?
Show answer
Use currentColor, for example border-color: currentColor.