Color Scheme Studio
← All Color 101 guides

Color 101 / Digital colour

Short HEX color codes in CSS

A shorter code can describe exactly the same color. Check the pairs before removing digits.

Expand by repeating each digit

Read #2A6 one character at a time. The red pair becomes 22, the green pair AA, and the blue pair 66. The result is #22AA66. Both produce the same opaque sRGB green.

Repeating the whole string would give #2A62A6, a violet color. To avoid that error, write each channel pair on its own before joining them.

Compare three codes

  • #2A6 → #22AA66Each digit repeats in place.
  • #2A68 → #22AA6688The fourth digit expands into alpha.
  • #2A6B70 → #2A6B70The channel pairs differ, so the six-digit form stays.

In the second sample, the alpha pair 88 equals 136 out of 255, or roughly 53% opacity. The sample is drawn over the page background. The green channels stay the same, but the result looks lighter over white and darker over a dark background.

Check the pairs before shortening

Split the full code into red, green and blue pairs. A pair can be compressed only if its two characters match. #AA44CC becomes #A4C. For #AB44CC, the red pair is AB, so retain the full code. Changing it to #A4C would change the red channel from 171 to 170.

The same rule covers the fourth pair in an eight-digit CSS value. #AA44CCFF can become #A4CF. Its final F expands to FF, meaning fully opaque. In CSS the alpha pair comes after the blue pair. Check the target format before moving that value into an environment that may use a different channel order.

When sharing swatches with a teammate, keep the longer form if it makes the channels easier to identify. Use shorthand when the destination accepts CSS and the pairs match exactly.

Source

W3C CSS Color Module Level 4 — hexadecimal notation