Sub topic 1
Color Keywords and Hex Notation
Prerequisites
This chapter assumes you can:
- Read basic HTML structure.
- Write CSS selectors and understand how the cascade picks a winning rule.
- Use the box model (margin, padding, border).
- Link or embed a stylesheet.
You do not need prior knowledge of color theory. Every color value in this chapter is a way to tell the browser which red, green, and blue light to show on a pixel.
Where color values are used
Many CSS properties accept a color. The most common are color (text), background-color, border-color, and outline-color. All of them accept the same value types, so what you learn here applies everywhere.
This chapter covers the two simplest notations: color keywords and hexadecimal (hex) codes. Later chapters cover rgb(), hsl(), and oklch().
Color keywords
A color keyword is a plain English name that CSS defines. CSS has 148 of them, such as red, tomato, rebeccapurple, and slategray. Keywords are not case-sensitive, so Red and red are the same color.
.alert {
color: white;
background-color: crimson;
border: 2px solid darkred;
}Keywords are readable and good for quick prototypes. Their drawback is that you get only the names CSS provides. You cannot ask for "a slightly lighter crimson".
Three special keywords behave differently:
| Keyword | Meaning |
|---|---|
| `transparent` | Fully see-through. Useful for borders and backgrounds you want invisible. |
| `currentcolor` | The element's current `color` value. A later chapter covers it in detail. |
| `inherit` | Not a color, but a CSS-wide keyword that copies the parent's value. |
Hex notation
A hex color starts with # followed by hexadecimal digits. Hexadecimal is a base-16 number system. Its digits are 0-9 and a-f, where a is 10 and f is 15. Two hex digits cover 0 to 255, which is the range of one color channel.
The six-digit form #rrggbb gives each channel two digits: red, green, and blue.
00 means none of that channel and ff means the maximum. So #000000 is black, #ffffff is white, and #ff0000 is pure red.
Shorthand and alpha forms
Hex has four valid lengths:
| Form | Example | Meaning |
|---|---|---|
| `#rgb` | `#f60` | Each digit is doubled, so this is `#ff6600`. |
| `#rrggbb` | `#ff6600` | Full opaque color. |
| `#rgba` | `#f608` | Shorthand with alpha (opacity). |
| `#rrggbbaa` | `#ff660080` | Full color with alpha. `00` is transparent, `ff` is opaque. |
The shorthand works only when every pair has two identical digits. #1a8cff has no shorthand.
.card {
color: #1a1a1a;
background-color: #fff;
border: 1px solid #ccd0d9;
box-shadow: 0 2px 8px #00000026;
}In that shadow, 26 in hex is 38 out of 255, which is about 15% opacity.
Common errors
- Missing
#.color: 1a1a1a;is invalid. The browser ignores the declaration and uses the inherited or earlier value, with no error message. - Wrong digit count. Hex codes with 5 or 7 digits are invalid and are ignored the same way.
- Letter O for zero.
#ff0000works.#ffOOOOdoes not. - Reading alpha as part of the color. In
#ff660080, the last pair is opacity, not a fourth color channel.
When to use each
Use keywords for demos and for obvious values like white or transparent. Use hex when you copy colors from a design tool, since most tools export hex. Hex is compact, but you cannot easily tell what a code looks like or how to make it lighter. The next chapter covers rgb() and hsl(), which are easier to adjust by hand.
Exercise
What color does #0f08 produce, and what are its full eight-digit form and opacity?
Show answer
It is a four-digit shorthand, so each digit is doubled: #00ff0088. That is green (red 00, green ff, blue 00) with alpha 88 hex, which is 136 out of 255, or about 53% opaque.
Exercise
A teammate writes background-color: ff6600; and the background stays unchanged. What is wrong, and what happens to the invalid line?
Show answer
The value is missing the leading #. It should be #ff6600. The browser discards the invalid declaration and falls back to the value from the cascade, so the background looks unchanged and no error is shown.