Sub topic 1
Color Keywords and Hex Notation
Prerequisites
This chapter assumes you already know the following:
- Basic HTML structure, such as elements, classes, and attributes.
- CSS selectors and how the cascade picks a winning rule.
- The box model: margin, padding, and border.
- How to link or embed a stylesheet.
You do not need prior knowledge of color theory. By the end of this chapter you will be able to write colors as named keywords and as hex values, read a hex value by eye, and avoid the most common mistakes.
How CSS describes a color
CSS uses color values in many properties. The most common are color (text), background-color, border-color, and outline-color. Shorthand properties such as border and background also accept a color.
Every color value on screen is made of the same ingredients. A screen mixes red, green, and blue light in different amounts. Each amount is called a channel. A color is therefore three channel values, and sometimes a fourth value for transparency.
CSS gives you several notations for the same idea. This chapter covers the two simplest ones: keywords and hex. The next chapter covers rgb() and hsl().
| Notation | Example | Typical use |
|---|---|---|
| Named keyword | `rebeccapurple` | Quick prototypes, simple demos |
| Hex, 6 digits | `#663399` | Colors copied from design tools |
| Hex, 3 digits | `#639` | Short form of some 6-digit colors |
| Hex with alpha | `#66339980` | Semi-transparent colors |
Named color keywords
A keyword is a word that CSS defines for a specific color. CSS has about 148 named colors, such as red, tomato, navy, gold, and rebeccapurple. Keywords are case-insensitive, so Red and red mean the same thing. The convention is lowercase.
.warning {
color: darkred;
background-color: lightyellow;
border: 2px solid goldenrod;
}Keywords are easy to read, but they have limits:
- The list is fixed. You cannot ask for "a slightly darker
tomato". - Some names are misleading.
lightgrayis lighter thangray, anddarkgrayis also lighter thangray. This is a historical quirk. - Some names have two spellings.
grayandgreyare identical, as aredarkgrayanddarkgrey. - Names do not map to a design system. A brand blue is rarely any of the named blues.
For these reasons, production stylesheets usually use keywords only for a few special cases, such as white, black, and transparent.
Special keywords
Three keywords are not ordinary named colors.
transparentis a fully see-through color. It is equivalent to black with zero opacity. Use it when a property needs a color value but you want nothing visible.currentColormeans "use the element's current text color". It is covered in a later chapter on theming.- System colors such as
Canvas,CanvasText, andLinkTextcome from the user's browser or operating system. They adapt to settings such as forced-colors (high-contrast) mode.
.ghost-button {
color: #0b5cad;
background-color: transparent;
border: 1px solid #0b5cad;
}Global keywords
Every CSS property also accepts the global keywords inherit, initial, unset, and revert. For color properties, inherit copies the parent's value. initial resets to the property's starting value, which is a dark text color for color and transparent for background-color.
Hex notation
Hex notation writes a color as a # followed by hexadecimal digits. Hexadecimal is a base-16 number system. It uses the digits 0 to 9 and the letters a to f. The letter a stands for 10, and f stands for 15.
A two-digit hex number can hold 256 values, from 00 (0) to ff (255). That matches the range of one color channel, which is why each channel takes exactly two hex digits.
Reading a 6-digit hex color
A 6-digit hex color has the form #RRGGBB. The first pair is red, the second is green, and the third is blue.
Take #1a1a1a from a typical text color:
- Red is
1a, which is 1 × 16 + 10 = 26. - Green is
1a, which is 26. - Blue is
1a, which is 26.
All three channels are equal and low, so the result is a very dark gray. Equal channels always give a shade of gray, from #000000 (black) to #ffffff (white).
Some other examples:
| Hex | Red | Green | Blue | Result |
|---|---|---|---|---|
| `#ff0000` | 255 | 0 | 0 | Pure red |
| `#00ff00` | 0 | 255 | 0 | Pure green |
| `#0000ff` | 0 | 0 | 255 | Pure blue |
| `#ffff00` | 255 | 255 | 0 | Yellow (red plus green) |
| `#0b5cad` | 11 | 92 | 173 | A medium blue |
You can read a hex color roughly without converting. Look at the first digit of each pair. A high first digit (c to f) means that channel is strong. A low first digit (0 to 3) means it is weak. In #0b5cad, red starts with 0, so it is nearly absent. Blue starts with a, so it dominates.
The 3-digit shorthand
If each pair in a 6-digit color has two identical digits, you can write one digit per channel. CSS doubles each digit.
#fffexpands to#ffffff.#369expands to#336699.#f60expands to#ff6600.
The shorthand only works when every pair repeats. You cannot shorten #1a2b3c, because none of its pairs repeat. Do not pad or guess: #1a2 means #11aa22, which is a different color from anything you may have intended.
Hex with alpha
Alpha is the opacity of a color. A value of 1 (or ff) is fully opaque. A value of 0 (or 00) is fully transparent. Hex supports alpha by adding a fourth channel at the end, which gives two more forms:
#RRGGBBAA, with 8 digits.#RGBA, with 4 digits, where each digit is doubled.
.overlay {
background-color: #00000080; /* black at about 50% opacity */
}
.tint {
background-color: #f006; /* #ff000066, red at 40% opacity */
}The alpha pair is a 0 to 255 scale, not a percentage. 80 is 128 out of 255, which is about 50%. 66 is 102 out of 255, which is 40%. Because of this, it is hard to pick a round percentage in hex. Most authors switch to rgb() or hsl() with a slash and a decimal alpha, such as rgb(0 0 0 / 0.5), when they need transparency. The next chapter covers that syntax.
Alpha in a color is different from the opacity property. A transparent background color affects only that background. The opacity property fades the whole element, including its text and children.
Valid and invalid hex values
The browser accepts only these lengths after the #: 3, 4, 6, or 8 digits. Anything else is invalid.
.valid-1 { color: #abc; }
.valid-2 { color: #AABBCC; }
.valid-3 { color: #aabbcc80; }
.invalid-1 { color: #ab; } /* 2 digits */
.invalid-2 { color: #abcde; } /* 5 digits */
.invalid-3 { color: aabbcc; } /* missing # */
.invalid-4 { color: #ggg; } /* g is not a hex digit */When the browser meets an invalid value, it ignores that one declaration, as it does for any invalid CSS value. It does not show an error. The property then falls back to the value from another rule or to its inherited or initial value. This is why a missing # produces a color that silently does nothing.
Hex digits are case-insensitive. #FFF and #fff are the same. Pick one style per project, and most teams choose lowercase.
Choosing between keywords and hex
Use this checklist to decide.
- If the color comes from a design file or brand guide, use the exact hex value.
- If you need
white,black, ortransparent, use the keyword. It is shorter and clear. - If you need to explore or sketch quickly, keywords are fine.
- If you need transparency, prefer
rgb()orhsl()with an alpha value, because hex alpha is hard to read. - If you plan to adjust lightness or hue, hex is a poor fit, because you cannot see the relationship between values. Later chapters cover
hsl()andoklch()for this.
Reusing a color: a preview
Writing the same hex value in many rules makes a later change tedious. Here is a small example that previews the technique from a later chapter.
:root {
--text: #1a1a1a;
--accent: #0b5cad;
}
body {
color: var(--text);
}
a {
color: var(--accent);
}The hex values appear once, and every rule refers to them by name. Changing --accent updates every link.
How the pieces connect
Common mistakes
- Leaving out the
#. The value is invalid and ignored. - Using 5 or 7 digits. Only 3, 4, 6, and 8 are valid.
- Shortening a color that does not repeat.
#1a2b3ccannot become a 3-digit value. - Reading the 8-digit alpha pair as a percentage.
80is about 50%, not 80%. - Assuming
darkgrayis darker thangray. It is lighter. - Setting a text color without checking it against its background. Contrast is covered in a later chapter.
Summary
Keywords give you a small fixed palette of readable names, plus the special values transparent and currentColor. Hex gives you any of about 16.7 million opaque colors in a compact form, with optional alpha in the 4-digit and 8-digit forms. Each pair of hex digits is one channel on a 0 to 255 scale. Invalid values are silently dropped, so check the developer tools when a color does not appear.
Exercise
What color does #f80 expand to, and which channel is strongest? Then write the 8-digit hex form of #336699 at about 50% opacity.
Show answer
#f80 expands to #ff8800: red is 255, green is 136, and blue is 0. Red is the strongest channel, with green about half as strong, so the color is orange. At about 50% opacity, #336699 becomes #33669980, because 80 is 128 out of 255.
Exercise
A developer writes .note { color: 1a1a1a; } and the text stays the default color. Name the cause and give the fix. Also say why #12345 would fail.
Show answer
The value is missing the leading #, so the browser treats the declaration as invalid and ignores it. The fix is color: #1a1a1a;. The value #12345 has 5 digits, and hex colors must have 3, 4, 6, or 8 digits.
Exercise
You need a 40% opaque red overlay and want the value to be easy to read in a code review. Which notation would you choose instead of hex, and why?
Show answer
Use rgb(255 0 0 / 0.4). The alpha is a plain decimal, so a reviewer sees 40% directly. The hex form #ff000066 needs a conversion from the 0 to 255 scale, because 66 is 102 out of 255.