← Back to CSS Course | Chapter 2: Colors & Backgrounds | Lesson 3 of 21

CSS HEX Colors

A hex color packs the same red/green/blue mixing idea as RGB into a compact six-character code, which is why it's the format you'll see copied out of most design tools.
Syntax
css
selector {
  color: #rrggbb;
  color: #rgb;
  color: #rrggbbaa;
}

Hex Color Syntax

A hex color starts with # followed by six hexadecimal digits (0-9 and A-F), grouped in pairs for red, green, and blue -- #FF0000 is pure red because FF (255) is the maximum red value and the other two channels are 00. Hex digits are case-insensitive, so #ff0000 and #FF0000 are identical.

Note:
  • Accepted values:
  • #rrggbb — six hex digits, e.g. #ff0000
  • #rgb — shorthand, e.g. #f00
  • #rrggbbaa — with alpha channel
  • #rgba — short form with alpha
  • Digits are 0-9 and a-f, not case sensitive

Example: Hex Color Syntax

css
<style>
.red { background: #FF0000; }
.lower { background: #ff0000; }
</style>
<div class="red">#FF0000</div>
<div class="lower">#ff0000 — identical color</div>

The 3-Digit Shorthand

When each channel's two digits are identical, CSS allows a 3-digit shorthand: #F00 expands to #FF0000. This only works when both digits of a channel match, so a color like #FF5733 has no valid 3-digit shorthand since none of its channel pairs repeat.

Note:
  • Accepted values:
  • #rgb — 3-digit shorthand; each digit is doubled (#f60 = #ff6600)
  • #rrggbb — 6-digit red, green, blue pairs, 00 to ff
  • #rgba — 4-digit shorthand including alpha
  • #rrggbbaa — 8-digit form; aa is alpha, 00 transparent to ff opaque

Example: The 3-Digit Shorthand

css
<style>
.short { background: #F00; }
.long { background: #FF0000; }
</style>
<div class="short">#F00</div>
<div class="long">#FF0000 — same color</div>

Adding Alpha with 8-Digit Hex

Modern CSS supports an optional 2-digit alpha channel appended to a hex color: #FF0000FF is fully opaque red, #FF000080 is roughly 50% transparent red. The alpha digits use the same 00-FF range as the color channels, where FF is fully opaque and 00 is fully transparent.

Note:
  • Accepted values:
  • #rgb — 3-digit shorthand; each digit is doubled (#f60 = #ff6600)
  • #rrggbb — 6-digit red, green, blue pairs, 00 to ff
  • #rgba — 4-digit shorthand including alpha
  • #rrggbbaa — 8-digit form; aa is alpha, 00 transparent to ff opaque

Example: Adding Alpha with 8-Digit Hex

css
<style>
.opaque { background: #FF0000FF; }
.half { background: #FF000080; }
</style>
<div class="opaque">Fully opaque red</div>
<div class="half">~50% transparent red</div>

Reading Hex Values at a Glance

With practice, the first two digits (red), middle two (green), and last two (blue) can be read directly to estimate a color -- high red and low green/blue means a shade of red, roughly equal high values across all three means a light gray or white, and equal low values mean a dark gray or black.

Example: Reading Hex Values at a Glance

css
<style>
.red-shade { background: #CC3333; }
.gray { background: #AAAAAA; }
</style>
<div class="red-shade">High red, low green/blue = red shade</div>
<div class="gray">Equal channels = gray</div>

Hex vs RGB in Practice

Hex codes are the most common format in design handoffs and CSS variables because they're compact and copy-paste friendly, while rgb()/rgba() are often preferred when a value needs to be split into separate numbers for animation or calculation.

Both describe the exact same color space, so converting between them never changes the actual color.

Example: Hex vs RGB in Practice

css
<style>
.hex { background: #1a73e8; }
.rgb { background: rgb(26, 115, 232); }
</style>
<div class="hex">#1a73e8</div>
<div class="rgb">rgb(26, 115, 232) — same color</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the # at the start, as in ff0000, which makes the declaration invalid.
  2. Using a value with the wrong number of digits, such as 5 or 7 characters, which is invalid.
  3. Using letters outside a to f, such as #gg0000, which is invalid.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.