How Hex Color Codes Work

Every hex color you write in CSS is a compact encoding of three numbers. The format is #RRGGBB, where RR, GG, and BB are two-digit hexadecimal values representing the red, green, and blue channels respectively. Each channel runs from 00 (decimal 0, no color) to FF (decimal 255, full intensity).

Example: #1E90FF (Dodger Blue)

Two hex digits per channel means 256 possible values (0–255), giving 256 × 256 × 256 = 16,777,216 possible colors.

Shorthand Hex (#RGB and #RGBA)

When both digits of each channel are the same, you can use three-digit shorthand. #FF5500 becomes #F50, and #FFFFFF becomes #FFF. The browser doubles each digit on parsing. This shorthand only covers 4,096 colors but saves characters in large stylesheets.

Four-digit shorthand #RGBA works the same way for 8-digit hex — #F50A expands to #FF5500AA.

Adding Transparency: 8-Digit Hex (#RRGGBBAA)

Modern CSS supports an 8-digit hex format where the last two digits control the alpha (opacity) channel:

Example: #FF573380 is the orange #FF5733 at roughly 50% opacity. Browser support is universal in all modern browsers since 2016.

An alternative is rgba(255, 87, 51, 0.5) — functionally identical, but more readable when the opacity value is computed dynamically in JavaScript.

Quick Reference: Common Hex Values

HexRGBColor
#FFFFFF255, 255, 255White
#0000000, 0, 0Black
#FF0000255, 0, 0Pure Red
#00FF000, 255, 0Pure Green
#0000FF0, 0, 255Pure Blue
#808080128, 128, 128Mid Gray
#000000800, 0, 0, 0.550% Black Overlay

Converting RGB to Hex Manually

Divide each RGB value by 16 to get the two hex digits. For example, RGB 255, 87, 51:

In JavaScript: color.toString(16).padStart(2, '0') converts a single channel. Our Binary & Number Systems converter lets you convert any decimal value to hex instantly.

Hex vs. HSL: Which to Use When

Hex is great for static colors in design systems — it is compact, widely supported, and familiar to designers and developers alike. HSL (Hue, Saturation, Lightness) shines when you need to generate color palettes programmatically, because lightness is a single number you can increment to create tints and shades. In practice, most teams store brand colors as hex in CSS custom properties and derive variants with HSL calculations or a preprocessor.

Using Hex Colors in CSS Custom Properties

CSS variables (custom properties) let you define your hex palette once and reference it everywhere:

:root {
  --color-primary: #6366F1;
  --color-primary-light: #818CF8;
  --color-danger: #EF4444;
  --color-text: #1E293B;
  --color-bg: #F8FAFC;
}

.button {
  background: var(--color-primary);
  color: #ffffff;
}

This pattern means you change a color in one place and it updates everywhere — no global find-and-replace needed.

Practical Developer Tips

Frequently Asked Questions

What does a hex color code like #FF5733 mean?

Each pair of hex digits represents one color channel: #RRGGBB. #FF5733 means red=FF (255), green=57 (87), blue=33 (51). Each channel runs from 00 (0) to FF (255), giving 256 possible values per channel and over 16 million possible colors in total.

How do I add transparency to a hex color?

Use 8-digit hex (#RRGGBBAA) where the last two digits are the alpha channel. FF is fully opaque, 00 is fully transparent, 80 is approximately 50% transparent. Example: #FF573380 is the same orange as #FF5733 but at ~50% opacity.

When should I use hex vs. rgb() vs. hsl() in CSS?

Use hex for static design tokens — it is compact and widely recognized. Use rgb()/rgba() when you need to animate or compute opacity in JavaScript. Use hsl() when programmatically generating palette shades, because lightness is a single number you can increment.

What is shorthand hex?

Three-digit hex (#RGB) is shorthand where each digit is doubled by the browser. #F53 expands to #FF5533. It only works when both digits of each channel are identical, covering 4,096 colors. It saves characters and is valid CSS.

Convert between decimal, hex, binary, and octal instantly.

⚡ Binary & Number Systems Converter — Free

Accuracy note: Conversion factors on SwiftConvertHub are sourced from NIST and IEC standards. Results are accurate for general use. For safety-critical or professional applications, verify results independently. Full disclaimer →

👤
Written by
Victor A. Calvo S.

Victor A. Calvo S. is a software engineer and digital entrepreneur who builds practical, free tools for developers, students, and professionals worldwide. He is the creator of SwiftConvertHub, InstantLinkHub, and Feexio. All conversion factors are cross-referenced against NIST and IEC standards. Learn more →