Hexadecimal color codes (hex codes) are the most common way to specify colors in web development. This guide explains how hex codes work, provides a complete reference, and shows you how to use them effectively in CSS, HTML, and design.
Table of Contents
- What Are Hex Color Codes?
- How Hex Color Codes Work
- Hex Code Format and Structure
- Common Hex Color Codes
- Hex vs RGB vs HSL
- Using Hex Codes in CSS
- Converting Between Formats
- Color Accessibility
What Are Hex Color Codes?
A hex color code is a six-digit code that represents a color in the RGB (Red, Green, Blue) color space. Each color is defined by the intensity of its red, green, and blue components, expressed in hexadecimal notation.
Example: #FF5733
FF= Red component (255 in decimal)57= Green component (87 in decimal)33= Blue component (51 in decimal)
Where Hex Codes Are Used
- CSS stylesheets
- HTML color attributes
- Design tools (Figma, Sketch, Adobe XD)
- Image editing software
- Brand style guides
- Color palettes
Quick Fact: Hex codes always start with a hash symbol (#) and are case-insensitive. Both #FF0000 and #ff0000 represent the same red color.
How Hex Color Codes Work
Hexadecimal Number System
Hexadecimal is a base-16 number system using digits 0-9 and letters A-F:
| Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
RGB Components
Each hex code has three two-digit components:
Format: #RRGGBB
- RR - Red intensity (00 to FF)
- GG - Green intensity (00 to FF)
- BB - Blue intensity (00 to FF)
Each component ranges from 00 (no color) to FF (full intensity):
00= 0 in decimal (no intensity)80= 128 in decimal (half intensity)FF= 255 in decimal (full intensity)
Pure Color Examples
/* Pure Red */
#FF0000
↓↓ = FF (255) red
↓↓ = 00 (0) green
↓↓ = 00 (0) blue
/* Pure Green */
#00FF00
↓↓ = 00 (0) red
↓↓ = FF (255) green
↓↓ = 00 (0) blue
/* Pure Blue */
#0000FF
↓↓ = 00 (0) red
↓↓ = 00 (0) green
↓↓ = FF (255) blue
View red (#FF0000) | View green (#00FF00) | View blue (#0000FF)
Hex Code Format and Structure
Full 6-Digit Format
Standard format: #RRGGBB
Examples:
#FF5733- Bright orange-red#3498DB- Bright blue#2ECC71- Bright green#9B59B6- Purple#F39C12- Orange
Shorthand 3-Digit Format
When both digits of each component are identical, use shorthand:
/* Full format → Shorthand */
#FF0000 → #F00 (red)
#00FF00 → #0F0 (green)
#0000FF → #00F (blue)
#FFFFFF → #FFF (white)
#000000 → #000 (black)
#CCCCCC → #CCC (gray)
The browser automatically expands shorthand:
#F00becomes#FF0000#C9Cbecomes#CC99CC#123becomes#112233
8-Digit Format with Alpha (Transparency)
Modern browsers support 8-digit hex codes for transparency:
Format: #RRGGBBAA
The last two digits (AA) represent the alpha channel:
00= Fully transparent (0%)80= 50% transparentFF= Fully opaque (100%)
/* Red with different opacity levels */
#FF000080 /* 50% opacity */
#FF000033 /* 20% opacity */
#FF0000FF /* 100% opacity (same as #FF0000) */
Browser Support: 8-digit hex codes work in all modern browsers (Chrome 62+, Firefox 49+, Safari 10+, Edge 79+). For older browsers, use rgba() instead.
Common Hex Color Codes
Basic Colors
| Color | Hex Code | RGB | Preview |
|---|---|---|---|
| Black | #000000 | rgb(0, 0, 0) | View |
| White | #FFFFFF | rgb(255, 255, 255) | View |
| Red | #FF0000 | rgb(255, 0, 0) | View |
| Green | #00FF00 | rgb(0, 255, 0) | View |
| Blue | #0000FF | rgb(0, 0, 255) | View |
| Yellow | #FFFF00 | rgb(255, 255, 0) | View |
| Cyan | #00FFFF | rgb(0, 255, 255) | View |
| Magenta | #FF00FF | rgb(255, 0, 255) | View |
Popular Web Colors
| Color Name | Hex Code | Common Use |
|---|---|---|
| Tomato | #FF6347 | Error messages, warnings |
| Coral | #FF7F50 | Call-to-action buttons |
| Orange | #FFA500 | Highlights, notifications |
| Gold | #FFD700 | Premium features, badges |
| Turquoise | #40E0D0 | Links, accents |
| Sky Blue | #87CEEB | Info messages |
| Royal Blue | #4169E1 | Primary buttons |
| Purple | #800080 | Premium content |
Neutral Grays (UI Essentials)
| Name | Hex Code | Lightness | Use Case |
|---|---|---|---|
| Pure Black | #000000 | 0% | Text on light backgrounds |
| Dark Gray | #333333 | 20% | Body text |
| Medium Gray | #666666 | 40% | Secondary text |
| Gray | #999999 | 60% | Disabled text |
| Light Gray | #CCCCCC | 80% | Borders, dividers |
| Very Light Gray | #F0F0F0 | 94% | Subtle backgrounds |
| Almost White | #FAFAFA | 98% | Page backgrounds |
| Pure White | #FFFFFF | 100% | White backgrounds |
UI Design Tip: Avoid pure black (#000000) for text. Use #1A1A1A or #212121 instead for a softer, more readable appearance.
Popular Brand Colors
| Brand | Color | Hex Code |
|---|---|---|
| Blue | #1877F2 | |
| Blue | #1DA1F2 | |
| Purple/Pink | #E4405F | |
| Blue | #0A66C2 | |
| YouTube | Red | #FF0000 |
| Green | #25D366 | |
| Spotify | Green | #1DB954 |
| Netflix | Red | #E50914 |
Modern UI Color Palettes
Tailwind CSS Defaults:
| Color | Hex Code | Color | Hex Code |
|---|---|---|---|
| Red-500 | #EF4444 | Blue-500 | #3B82F6 |
| Orange-500 | #F97316 | Indigo-500 | #6366F1 |
| Yellow-500 | #EAB308 | Purple-500 | #A855F7 |
| Green-500 | #22C55E | Pink-500 | #EC4899 |
| Cyan-500 | #06B6D4 | Gray-500 | #6B7280 |
Hex vs RGB vs HSL
| feature | hex | rgb | hsl |
|---|---|---|---|
| Format | #RRGGBB | rgb(r, g, b) | hsl(h, s%, l%) |
| Readability | Medium | Good | Best |
| Length | 7 characters | 13-17 characters | 15-20 characters |
| Transparency | 8 digits (#RRGGBBAA) | rgba() function | hsla() function |
| Easy to Adjust | Hard | Medium | Easy |
| Common Use | Most popular | JavaScript | Design systems |
When to Use Each Format
Use Hex (#RRGGBB) for:
- Static colors in CSS
- Design systems and style guides
- Sharing colors between designers and developers
- When space is limited
Use RGB (rgb()) for:
- Dynamic colors in JavaScript
- Colors with transparency (rgba)
- Programmatic color manipulation
- Mathematical color operations
Use HSL (hsl()) for:
- Creating color palettes
- Building theme systems
- Color variations (lighter/darker shades)
- More intuitive color adjustments
/* Same color in different formats */
/* Hex */
color: #3498DB;
/* RGB */
color: rgb(52, 152, 219);
/* HSL */
color: hsl(204, 70%, 53%);
/* With transparency */
color: #3498DB80; /* 50% opacity */
color: rgba(52, 152, 219, 0.5);
color: hsla(204, 70%, 53%, 0.5);
Using Hex Codes in CSS
Basic CSS Properties
/* Text color */
.text {
color: #333333;
}
/* Background color */
.box {
background-color: #FFFFFF;
}
/* Border color */
.card {
border: 2px solid #E0E0E0;
}
/* Box shadow */
.button {
box-shadow: 0 2px 4px #00000026;
}
/* Gradient backgrounds */
.hero {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
/* Multiple properties */
.link {
color: #3B82F6;
background-color: #EFF6FF;
border-bottom: 2px solid #3B82F6;
}
CSS Variables (Custom Properties)
:root {
/* Brand colors */
--color-primary: #3B82F6;
--color-primary-dark: #2563EB;
--color-primary-light: #60A5FA;
/* Neutral colors */
--color-text: #1F2937;
--color-text-light: #6B7280;
--color-background: #FFFFFF;
--color-border: #E5E7EB;
/* Semantic colors */
--color-success: #10B981;
--color-warning: #F59E0B;
--color-error: #EF4444;
--color-info: #3B82F6;
}
/* Usage */
.button-primary {
background-color: var(--color-primary);
color: var(--color-background);
}
.button-primary:hover {
background-color: var(--color-primary-dark);
}
Dark Mode with CSS Variables
:root {
--bg-primary: #FFFFFF;
--bg-secondary: #F3F4F6;
--text-primary: #111827;
--text-secondary: #6B7280;
}
[data-theme="dark"] {
--bg-primary: #111827;
--bg-secondary: #1F2937;
--text-primary: #F9FAFB;
--text-secondary: #D1D5DB;
}
body {
background-color: var(--bg-primary);
color: var(--text-primary);
transition: background-color 0.3s, color 0.3s;
}
Converting Between Formats
Manual Conversion: Hex to RGB
- Take the first two hex digits (RR) → convert to decimal
- Take the middle two digits (GG) → convert to decimal
- Take the last two digits (BB) → convert to decimal
Example: #FF5733
FF(hex) = 255 (decimal) = Red57(hex) = 87 (decimal) = Green33(hex) = 51 (decimal) = Blue- Result:
rgb(255, 87, 51)
Manual Conversion: RGB to Hex
- Convert each RGB value (0-255) to hexadecimal
- Combine with # prefix
Example: rgb(52, 152, 219)
- 52 (decimal) = 34 (hex)
- 152 (decimal) = 98 (hex)
- 219 (decimal) = DB (hex)
- Result:
#3498DB
JavaScript Conversion Functions
// Hex to RGB
function hexToRgb(hex) {
const result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
console.log(hexToRgb('#3498DB'));
// { r: 52, g: 152, b: 219 }
// RGB to Hex
function rgbToHex(r, g, b) {
return "#" + [r, g, b]
.map(x => x.toString(16).padStart(2, '0'))
.join('')
.toUpperCase();
}
console.log(rgbToHex(52, 152, 219));
// "#3498DB"
// Lighten color by percentage
function lightenColor(hex, percent) {
const rgb = hexToRgb(hex);
const r = Math.min(255, Math.floor(rgb.r + (255 - rgb.r) * percent / 100));
const g = Math.min(255, Math.floor(rgb.g + (255 - rgb.g) * percent / 100));
const b = Math.min(255, Math.floor(rgb.b + (255 - rgb.b) * percent / 100));
return rgbToHex(r, g, b);
}
console.log(lightenColor('#3498DB', 20));
// Returns a lighter blue
// Darken color by percentage
function darkenColor(hex, percent) {
const rgb = hexToRgb(hex);
const r = Math.max(0, Math.floor(rgb.r * (100 - percent) / 100));
const g = Math.max(0, Math.floor(rgb.g * (100 - percent) / 100));
const b = Math.max(0, Math.floor(rgb.b * (100 - percent) / 100));
return rgbToHex(r, g, b);
}
console.log(darkenColor('#3498DB', 20));
// Returns a darker blue
Color Accessibility
WCAG Contrast Requirements
The Web Content Accessibility Guidelines (WCAG) specify minimum contrast ratios between text and background colors:
Level AA (Minimum - Required):
- 4.5:1 for normal text (under 24px or 19px bold)
- 3:1 for large text (24px+ or 19px+ bold)
- 3:1 for UI components and graphics
Level AAA (Enhanced - Recommended):
- 7:1 for normal text
- 4.5:1 for large text
Accessibility Matters: 1 in 12 men and 1 in 200 women have some form of color blindness. Always ensure sufficient contrast and never rely solely on color to convey information.
Good Color Contrast Examples
| text Color | background | ratio | rating |
|---|---|---|---|
| #000000 | #FFFFFF | 21:1 | AAA ✓✓✓ |
| #333333 | #FFFFFF | 12.6:1 | AAA ✓✓✓ |
| #666666 | #FFFFFF | 5.7:1 | AA ✓✓ |
| #999999 | #FFFFFF | 2.8:1 | Fail ✗ |
| #FFFFFF | #2196F3 | 2.4:1 | Fail ✗ |
| #FFFFFF | #1565C0 | 4.7:1 | AA ✓✓ |
Calculating Contrast Ratio
// Calculate relative luminance
function getLuminance(hex) {
const rgb = hexToRgb(hex);
const [r, g, b] = [rgb.r, rgb.g, rgb.b].map(val => {
val = val / 255;
return val <= 0.03928
? val / 12.92
: Math.pow((val + 0.055) / 1.055, 2.4);
});
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
// Calculate contrast ratio
function getContrastRatio(hex1, hex2) {
const lum1 = getLuminance(hex1);
const lum2 = getLuminance(hex2);
const brightest = Math.max(lum1, lum2);
const darkest = Math.min(lum1, lum2);
return (brightest + 0.05) / (darkest + 0.05);
}
// Check if contrast meets WCAG standards
function meetsWCAG(textColor, bgColor, largeText = false) {
const ratio = getContrastRatio(textColor, bgColor);
const minRatio = largeText ? 3 : 4.5;
return {
ratio: ratio.toFixed(2),
passesAA: ratio >= minRatio,
passesAAA: ratio >= (largeText ? 4.5 : 7)
};
}
console.log(meetsWCAG('#333333', '#FFFFFF'));
// { ratio: '12.63', passesAA: true, passesAAA: true }
console.log(meetsWCAG('#999999', '#FFFFFF'));
// { ratio: '2.85', passesAA: false, passesAAA: false }
Accessible Color Pairs
Light Backgrounds:
| Background | Good Text Colors |
|---|---|
#FFFFFF | #000000, #212121, #424242, #616161 |
#F5F5F5 | #000000, #212121, #424242, #616161 |
#E3F2FD | #0D47A1, #1565C0, #1976D2 |
Dark Backgrounds:
| Background | Good Text Colors |
|---|---|
#000000 | #FFFFFF, #F5F5F5, #EEEEEE |
#212121 | #FFFFFF, #F5F5F5, #E0E0E0 |
#1976D2 | #FFFFFF, #E3F2FD |
Common Mistakes to Avoid
1. Poor Contrast
/* ❌ Bad - Low contrast (2.8:1) */
.text-bad {
color: #999999;
background: #FFFFFF;
}
/* ✅ Good - High contrast (12.6:1) */
.text-good {
color: #333333;
background: #FFFFFF;
}
2. Too Many Colors
/* ❌ Bad - Color chaos */
:root {
--color-1: #FF6347;
--color-2: #4169E1;
--color-3: #32CD32;
--color-4: #FFD700;
--color-5: #FF1493;
/* ... 10 more colors */
}
/* ✅ Good - Consistent palette */
:root {
--primary: #3B82F6;
--secondary: #6366F1;
--success: #10B981;
--warning: #F59E0B;
--error: #EF4444;
--gray: #6B7280;
}
3. Using Pure Black
/* ❌ Bad - Too harsh */
.text-harsh {
color: #000000;
}
/* ✅ Good - Softer, more readable */
.text-soft {
color: #1A1A1A;
}
4. Forgetting Color Blindness
Don't rely on color alone:
<!-- ❌ Bad - Only color indicates error -->
<input class="error">
<!-- ✅ Good - Icon + text + color -->
<div class="error-field">
<input>
<span class="error-icon">⚠️</span>
<span class="error-text">Invalid email</span>
</div>
Quick Reference Guide
Creating Shades
Lighten a color: Increase all RGB values proportionally
Darken a color: Decrease all RGB values proportionally
Example - Blue shades:
#00008B (Darkest)
#0000CD (Dark)
#0000FF (Medium)
#4169E1 (Light)
#87CEEB (Lightest)
Color Relationships
Complementary (Opposite on color wheel):
- Red
#FF0000← → Cyan#00FFFF - Green
#00FF00← → Magenta#FF00FF - Blue
#0000FF← → Yellow#FFFF00
Analogous (Adjacent on color wheel):
- Blue
#0000FF - Blue-Purple
#8000FF - Purple
#FF00FF
Summary
Hex color codes are the foundation of web color specification. Understanding how they work helps you choose and communicate colors effectively, create accessible color schemes, and build consistent design systems.
Key Takeaways:
✅ Hex codes represent RGB colors in hexadecimal format (#RRGGBB)
✅ Each component ranges from 00 (0) to FF (255)
✅ Shorthand format (#RGB) works when digits repeat
✅ Always check color contrast for accessibility (4.5:1 minimum)
✅ Use CSS variables for maintainable color systems
✅ Test colors on different screens and for color blindness