Hex Color Codes: The Complete Guide for Developers (2026)

Published on • 15 min read

Hex color codes are the backbone of color on the web. Whether you are writing CSS, designing a UI, or debugging a front-end issue, you will encounter hex codes like #FF5733, #3b82f6, or #000000 every single day. Yet many developers treat them as opaque magic strings — copy-pasted from a color picker without ever understanding what the characters actually mean.

This guide changes that. We will cover everything from the fundamentals of hexadecimal notation to alpha transparency, conversion formulas, CSS usage patterns, and common mistakes. By the end, you will be able to read, write, and manipulate hex color codes with confidence.

Need to convert colors right now?

Use our free browser-based tools — no signup required.

Hex ↔ RGB Converter Color Picker

Table of Contents

  1. What Are Hex Color Codes?
  2. How Hex Color Codes Work
  3. Shorthand Hex: #RGB vs #RRGGBB
  4. Hex with Alpha Transparency (#RRGGBBAA)
  5. 20 Most Popular Hex Colors
  6. Color Categories with Hex Codes
  7. Converting Hex to RGB
  8. Converting RGB to Hex
  9. Hex in CSS: Practical Examples
  10. Hex vs RGB vs HSL: When to Use Each
  11. Tips for Working with Hex Colors
  12. Common Hex Color Code Mistakes

1. What Are Hex Color Codes?

A hex color code (also called an HTML color code or hexadecimal color) is a six-character string that represents a color using the RGB color model. It is prefixed with a hash symbol (#) and consists of three pairs of hexadecimal digits, each pair specifying the intensity of red, green, or blue light.

For example, #FF0000 is pure red, #00FF00 is pure green, and #0000FF is pure blue. Mix them together and you can represent over 16.7 million colors (256 × 256 × 256 = 16,777,216).

Hex color codes were first introduced in HTML 3.2 (1997) and have been the default color format for CSS ever since. Every browser, design tool, and front-end framework supports them. They are compact, unambiguous, and easy to copy-paste — which is why they remain the most widely used color notation in web development, even as alternatives like rgb() and hsl() have gained popularity.

2. How Hex Color Codes Work

To understand hex color codes, you need to understand two things: the RGB color model and the hexadecimal (base-16) number system.

The RGB Color Model

Computer screens produce color by mixing red, green, and blue light at varying intensities. Each channel ranges from 0 (no light, fully off) to 255 (maximum intensity, fully on). By combining these three channels, you can produce any visible color:

Hexadecimal (Base-16) Numbering

Instead of writing each channel as a decimal number (0–255), hex color codes use base-16 notation. In base-16, the digits are:

Hexadecimal Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Each hex digit represents a value from 0 to 15. Two hex digits together represent a value from 00 (0) to FF (255) — exactly the range needed for one color channel. Here is how the conversion works:

Hex to Decimal Formula Value = (first digit × 16) + second digit

Example: FF = (15 × 16) + 15 = 240 + 15 = 255
Example: 3B = (3 × 16) + 11 = 48 + 11 = 59
Example: 00 = (0 × 16) + 0 = 0

The #RRGGBB Structure

A hex color code is structured as three consecutive two-digit hex values, each controlling one color channel:

# RRRed GGGreen BBBlue

Let us decode a real color: #3B82F6 (the primary blue used across this site):

So #3B82F6 is rgb(59, 130, 246) — a vivid blue with low red, moderate green, and high blue. The hex notation is case-insensitive: #3b82f6 and #3B82F6 produce the exact same color.

Pro Tip: Use our Hex to RGB Converter to instantly decode any hex color code into its RGB components and preview the color in real time.

3. Shorthand Hex: #RGB vs #RRGGBB

CSS supports a three-character shorthand for hex colors where each digit is repeated. This only works when both digits in each pair are identical:

Shorthand Expansion Rule #RGB → #RRGGBB

#F00 → #FF0000 (red)
#0F0 → #00FF00 (green)
#00F → #0000FF (blue)
#FFF → #FFFFFF (white)
#000 → #000000 (black)
#369 → #336699 (steel blue)

Each shorthand digit is duplicated: 3 becomes 33, 6 becomes 66, 9 becomes 99. This gives you 4,096 possible shorthand colors (16 × 16 × 16) out of the full 16.7 million.

When to Use Shorthand

Common Mistake: Trying to shorten #112233 to #123 is correct, but trying to shorten #1A2B3C to #123 is wrong — that would expand to #112233, a completely different color.

4. Hex with Alpha Transparency (#RRGGBBAA)

Modern CSS (supported in all major browsers since 2017) allows an eight-digit hex color with a fourth pair for the alpha channel (transparency):

# RRRed GGGreen BBBlue AAAlpha

The alpha channel ranges from 00 (fully transparent) to FF (fully opaque). Some common alpha values:

Alpha HexDecimalOpacityCommon Use
FF255100%Fully opaque (default)
CC20480%Slightly transparent
8012850%Half transparent
406425%Mostly transparent
1A2610%Subtle overlays
0000%Fully transparent

Examples in practice:

/* Black at 50% opacity */
background-color: #00000080;

/* Blue at 20% opacity — great for hover states */
background-color: #3B82F633;

/* White at 10% opacity — subtle glass effect */
background-color: #FFFFFF1A;

There is also a four-digit shorthand (#RGBA) that works the same way as three-digit shorthand:

Shorthand Alpha Examples #F008 → #FF000088 (red at ~53% opacity)
#0003 → #00000033 (black at 20% opacity)
#FFF0 → #FFFFFF00 (fully transparent white)
Browser Support: Eight-digit hex colors are supported in Chrome 62+, Firefox 49+, Safari 10+, and Edge 79+. That covers over 98% of users as of 2026. For older browsers, use rgba() as a fallback.

These are the most commonly used CSS named colors. Every browser recognizes these names, but most developers use the hex codes directly for precision and consistency.

Swatch Name Hex Code RGB
White #FFFFFF rgb(255, 255, 255)
Black #000000 rgb(0, 0, 0)
Red #FF0000 rgb(255, 0, 0)
Lime #00FF00 rgb(0, 255, 0)
Blue #0000FF rgb(0, 0, 255)
Yellow #FFFF00 rgb(255, 255, 0)
Cyan / Aqua #00FFFF rgb(0, 255, 255)
Magenta / Fuchsia #FF00FF rgb(255, 0, 255)
Gray #808080 rgb(128, 128, 128)
Silver #C0C0C0 rgb(192, 192, 192)
Maroon #800000 rgb(128, 0, 0)
Olive #808000 rgb(128, 128, 0)
Green #008000 rgb(0, 128, 0)
Purple #800080 rgb(128, 0, 128)
Teal #008080 rgb(0, 128, 128)
Navy #000080 rgb(0, 0, 128)
Orange #FFA500 rgb(255, 165, 0)
Pink #FFC0CB rgb(255, 192, 203)
Brown #A52A2A rgb(165, 42, 42)
Tomato #FF6347 rgb(255, 99, 71)
Quick Reference: Need to explore more colors or generate harmonious palettes? Try our Color Palette Generator to create beautiful, accessible color combinations instantly.

6. Color Categories with Hex Codes

Here is a curated collection of hex color codes organized by color family. These are popular choices in modern web design and UI frameworks like Tailwind CSS, Material Design, and Bootstrap.

Reds

Rose 100#FEE2E2
Red 500#EF4444
Red 600#DC2626
Red 700#B91C1C
Red 800#991B1B
Coral Red#FF6B6B

Blues

Blue 100#DBEAFE
Blue 500#3B82F6
Blue 600#2563EB
Blue 700#1D4ED8
Dark Navy#1E3A5F
Cyan 500#06B6D4

Greens

Emerald 100#D1FAE5
Emerald 500#10B981
Green 500#22C55E
Green 600#16A34A
Emerald 800#065F46
Lime 500#84CC16

Purples

Violet 100#EDE9FE
Violet 500#8B5CF6
Purple 500#A855F7
Violet 600#7C3AED
Violet 700#6D28D9
Pink 500#EC4899

Oranges & Yellows

Amber 100#FEF3C7
Amber 500#F59E0B
Orange 500#F97316
Orange 600#EA580C
Yellow 500#EAB308
Burnt Orange#FF5733

Neutrals

Gray 50#F9FAFB
Gray 200#E5E7EB
Gray 400#9CA3AF
Gray 500#6B7280
Gray 700#374151
Gray 900#111827

7. Converting Hex to RGB

Converting a hex color code to RGB is straightforward. Split the six-character string into three pairs and convert each pair from base-16 to base-10.

The Formula

Hex to RGB Conversion Given hex color #RRGGBB:

Red   = (R1 × 16) + R2
Green = (G1 × 16) + G2
Blue  = (B1 × 16) + B2

Where each letter/digit maps to: A=10, B=11, C=12, D=13, E=14, F=15

Step-by-Step Example

Convert #FF5733 to RGB:

  1. Split into pairs: FF, 57, 33
  2. Red (FF): F=15, F=15 → (15 × 16) + 15 = 240 + 15 = 255
  3. Green (57): 5=5, 7=7 → (5 × 16) + 7 = 80 + 7 = 87
  4. Blue (33): 3=3, 3=3 → (3 × 16) + 3 = 48 + 3 = 51
  5. Result: rgb(255, 87, 51)

JavaScript Implementation

function hexToRgb(hex) {
  // Remove the hash if present
  hex = hex.replace(/^#/, '');

  // Handle shorthand (#RGB)
  if (hex.length === 3) {
    hex = hex.split('').map(c => c + c).join('');
  }

  const r = parseInt(hex.substring(0, 2), 16);
  const g = parseInt(hex.substring(2, 4), 16);
  const b = parseInt(hex.substring(4, 6), 16);

  return { r, g, b };
}

// Usage
hexToRgb('#FF5733');  // { r: 255, g: 87, b: 51 }
hexToRgb('#3B82F6');  // { r: 59, g: 130, b: 246 }
hexToRgb('#000');     // { r: 0, g: 0, b: 0 }

Skip the Math

Convert hex to RGB instantly with our free tool.

Open Hex ↔ RGB Converter

8. Converting RGB to Hex

The reverse process converts each decimal channel value (0–255) into a two-digit hexadecimal string, then concatenates them with a # prefix.

The Formula

RGB to Hex Conversion Given rgb(R, G, B) where each value is 0-255:

Hex = "#" + toHex(R) + toHex(G) + toHex(B)

toHex(n):
  First digit = Math.floor(n / 16) → convert to hex char
  Second digit = n % 16 → convert to hex char

Step-by-Step Example

Convert rgb(59, 130, 246) to hex:

  1. Red (59): 59 ÷ 16 = 3 remainder 11 → 3 and B → 3B
  2. Green (130): 130 ÷ 16 = 8 remainder 2 → 8 and 2 → 82
  3. Blue (246): 246 ÷ 16 = 15 remainder 6 → F and 6 → F6
  4. Result: #3B82F6

JavaScript Implementation

function rgbToHex(r, g, b) {
  // Ensure values are in range
  r = Math.max(0, Math.min(255, r));
  g = Math.max(0, Math.min(255, g));
  b = Math.max(0, Math.min(255, b));

  return '#' + [r, g, b]
    .map(c => c.toString(16).padStart(2, '0'))
    .join('')
    .toUpperCase();
}

// Usage
rgbToHex(255, 87, 51);   // "#FF5733"
rgbToHex(59, 130, 246);  // "#3B82F6"
rgbToHex(0, 0, 0);       // "#000000"
Quick Trick: In JavaScript, n.toString(16) converts any integer to a hex string. Use .padStart(2, '0') to ensure single-digit values like 5 become 05 instead of just 5.

9. Hex Colors in CSS: Practical Examples

Hex color codes work everywhere CSS accepts a color value. Here are the most common use cases with practical examples you can copy directly into your stylesheets.

Text Color

/* Headings in near-white */
h1, h2, h3 {
  color: #E4E4E7;
}

/* Body text in muted gray */
p {
  color: #9CA3AF;
}

/* Links in brand blue */
a {
  color: #3B82F6;
}

/* Error messages in red */
.error {
  color: #EF4444;
}

/* Success messages in green */
.success {
  color: #10B981;
}

Background Color

/* Dark page background */
body {
  background-color: #0F1117;
}

/* Card surface */
.card {
  background-color: #1A1D27;
}

/* Highlighted row */
.highlight {
  background-color: #3B82F61A; /* Blue at 10% opacity */
}

Borders

/* Subtle divider line */
.divider {
  border-bottom: 1px solid #374151;
}

/* Input field with focus ring */
input:focus {
  border-color: #3B82F6;
  outline: 2px solid #3B82F640;
}

/* Alert box with colored border */
.alert-warning {
  border-left: 4px solid #F59E0B;
}

CSS Gradients

/* Linear gradient: blue to purple */
.hero-banner {
  background: linear-gradient(135deg, #3B82F6, #8B5CF6);
}

/* Radial gradient with transparency */
.glow-effect {
  background: radial-gradient(circle, #3B82F640, transparent);
}

/* Multi-stop gradient */
.rainbow-bar {
  background: linear-gradient(
    90deg,
    #EF4444,
    #F59E0B,
    #22C55E,
    #3B82F6,
    #8B5CF6
  );
}
Visual Gradient Builder: Creating gradients by hand is tedious. Use our CSS Gradient Generator to build, preview, and copy gradient CSS code with a visual editor.

Box Shadows

/* Soft shadow with transparent black */
.card {
  box-shadow: 0 4px 12px #00000040;
}

/* Colored glow effect */
.button-primary {
  box-shadow: 0 0 20px #3B82F660;
}

/* Layered shadows for depth */
.modal {
  box-shadow:
    0 2px 4px #0000001A,
    0 8px 24px #00000033;
}

10. Hex vs RGB vs HSL: When to Use Each

CSS gives you three main ways to specify colors. Each has strengths and ideal use cases:

Feature Hex (#RRGGBB) RGB (rgb()) HSL (hsl())
Syntax #3B82F6 rgb(59 130 246) hsl(217 91% 60%)
Readability Low — requires decoding Medium — familiar 0-255 High — intuitive hue/sat/light
Compactness 7 chars (most compact) ~18 chars ~18 chars
Alpha support #3B82F680 rgb(59 130 246 / 50%) hsl(217 91% 60% / 50%)
Design tool output Figma, Sketch, Photoshop Browser DevTools Some design systems
Color manipulation Difficult Moderate Easy (adjust hue/lightness)
Copy-paste friendly Excellent Good Good
Browser support Universal (since 1997) Universal Universal

When to Use Hex

When to Use RGB

When to Use HSL

Modern CSS Tip: CSS now supports oklch() and oklab() color spaces, which provide even more perceptually uniform color manipulation. These are supported in all major browsers as of 2025. However, hex remains the dominant format for static color definitions.

11. Tips for Working with Hex Colors

1. Use CSS Custom Properties for Your Palette

Define your hex colors once as CSS variables, then reference them throughout your stylesheet. This makes theme changes trivial:

:root {
  --color-bg: #0F1117;
  --color-surface: #1A1D27;
  --color-primary: #3B82F6;
  --color-accent: #10B981;
  --color-text: #E4E4E7;
  --color-muted: #9CA3AF;
  --color-danger: #EF4444;
  --color-warning: #F59E0B;
}

body { background: var(--color-bg); color: var(--color-text); }
a { color: var(--color-primary); }
.btn-primary { background: var(--color-primary); }

2. Keep a Hex Cheat Sheet

Memorize a few key values and you can eyeball any hex code:

3. Check Color Contrast for Accessibility

WCAG 2.1 requires a minimum 4.5:1 contrast ratio for normal text and 3:1 for large text. Common accessible combinations:

4. Use Browser DevTools to Inspect Colors

All major browsers let you click the color swatch in the CSS inspector to open a color picker. You can toggle between hex, RGB, and HSL formats instantly. In Chrome, hold Shift and click the swatch to cycle through formats.

5. Darken and Lighten Hex Colors Programmatically

A simple technique: convert to RGB, adjust each channel by a percentage, convert back to hex:

function adjustBrightness(hex, percent) {
  hex = hex.replace(/^#/, '');
  const num = parseInt(hex, 16);
  const r = Math.min(255, Math.max(0, (num >> 16) + Math.round(2.55 * percent)));
  const g = Math.min(255, Math.max(0, ((num >> 8) & 0x00FF) + Math.round(2.55 * percent)));
  const b = Math.min(255, Math.max(0, (num & 0x0000FF) + Math.round(2.55 * percent)));
  return '#' + (0x1000000 + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}

adjustBrightness('#3B82F6', -20);  // Darken by 20%
adjustBrightness('#3B82F6', 20);   // Lighten by 20%

6. Use Semantic Color Names in Your Code

Never use raw hex codes scattered throughout your CSS. Instead, give them meaningful names:

/* Bad: What does this color mean? */
.alert { background: #FEE2E2; border-color: #EF4444; color: #991B1B; }

/* Good: Intent is clear */
.alert {
  background: var(--color-danger-light);
  border-color: var(--color-danger);
  color: var(--color-danger-dark);
}

12. Common Hex Color Code Mistakes

Even experienced developers make these mistakes. Here is how to avoid them:

Mistake 1: Forgetting the Hash Symbol

Wrong: color: FF5733;
Right: color: #FF5733;

Without the #, the browser does not recognize it as a hex color. In CSS, this will silently fail. In JavaScript, parseInt('FF5733', 16) works without the hash, but CSS requires it.

Mistake 2: Using Invalid Hex Characters

Wrong: #GG5733 or #ZZZZZZ
Right: Only 0-9 and A-F are valid hex digits.

This is an easy typo to make, especially when typing quickly. Your editor's syntax highlighting should catch this, but not all tools do.

Mistake 3: Wrong Number of Characters

Wrong: #FF573 (5 chars) or #FF57331 (7 chars)
Right: Valid lengths are 3, 4, 6, or 8 characters (after the #).

CSS will ignore any hex code with an invalid length. Double-check your codes when copy-pasting.

Mistake 4: Confusing Shorthand Expansion

Wrong assumption: #123456 can be shortened to #135
Reality: #135 expands to #113355, not #123456.

Shorthand only works when each pair has identical digits: #112233#123.

Mistake 5: Case Sensitivity in JavaScript

Gotcha: CSS treats #ff5733 and #FF5733 identically, but string comparison in JavaScript does not:

'#ff5733' === '#FF5733'false

Always normalize to lowercase (or uppercase) before comparing hex color strings in code.

Mistake 6: Ignoring Alpha Channel Order

CSS hex: Alpha goes at the end#RRGGBBAA
Android/iOS: Alpha often goes at the start#AARRGGBB

If you copy a color from a mobile design tool, the alpha position may be different from CSS. Always verify.

Mistake 7: Not Testing on Different Monitors

The hex code #1A1D27 might look like pure black on a cheap monitor but show its subtle blue undertone on a calibrated display. When choosing dark theme colors, test on multiple devices and ensure sufficient contrast for accessibility.

Free Color Tools for Developers

Put your hex color knowledge into practice with our browser-based tools.

Hex ↔ RGB Converter Color Picker Palette Generator Gradient Generator

Conclusion

Hex color codes are one of those fundamental web development concepts that every developer encounters but few truly understand. Now you know exactly how the #RRGGBB format works, why FF equals 255, how to convert between hex and RGB in your head (or with our converter tool), and how to avoid the most common pitfalls.

The key takeaways are:

Whether you are building a dark theme like this site, creating a design system, or debugging a CSS issue at 2 AM, a solid understanding of hex color codes will make you a more confident and efficient developer.