Hex Color Codes: The Complete Guide for Developers (2026)
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 PickerTable of Contents
- What Are Hex Color Codes?
- How Hex Color Codes Work
- Shorthand Hex: #RGB vs #RRGGBB
- Hex with Alpha Transparency (#RRGGBBAA)
- 20 Most Popular Hex Colors
- Color Categories with Hex Codes
- Converting Hex to RGB
- Converting RGB to Hex
- Hex in CSS: Practical Examples
- Hex vs RGB vs HSL: When to Use Each
- Tips for Working with Hex Colors
- 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:
- Red (255) + Green (0) + Blue (0) = pure red
- Red (255) + Green (255) + Blue (0) = yellow
- Red (0) + Green (0) + Blue (0) = black
- Red (255) + Green (255) + Blue (255) = white
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:
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:
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:
Let us decode a real color: #3B82F6 (the primary blue used across this site):
- Red:
3B= (3 × 16) + 11 = 59 - Green:
82= (8 × 16) + 2 = 130 - Blue:
F6= (15 × 16) + 6 = 246
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.
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:
#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
- Use it for simple colors like black (
#000), white (#FFF), or primary colors (#F00,#0F0,#00F). - Use it in quick prototypes or when file size matters (saves 3 bytes per color declaration).
- Avoid it when the color does not have matching digit pairs —
#3B82F6cannot be shortened. - Avoid it in design systems where consistency and readability matter. Six digits are always clearer.
#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):
The alpha channel ranges from 00 (fully transparent) to FF (fully opaque). Some common alpha values:
| Alpha Hex | Decimal | Opacity | Common Use |
|---|---|---|---|
FF | 255 | 100% | Fully opaque (default) |
CC | 204 | 80% | Slightly transparent |
80 | 128 | 50% | Half transparent |
40 | 64 | 25% | Mostly transparent |
1A | 26 | 10% | Subtle overlays |
00 | 0 | 0% | 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:
#0003 → #00000033 (black at 20% opacity)
#FFF0 → #FFFFFF00 (fully transparent white)
rgba() as a fallback.
5. The 20 Most Popular Hex Colors
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) |
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
Blues
Greens
Purples
Oranges & Yellows
Neutrals
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
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:
- Split into pairs: FF, 57, 33
- Red (FF): F=15, F=15 → (15 × 16) + 15 = 240 + 15 = 255
- Green (57): 5=5, 7=7 → (5 × 16) + 7 = 80 + 7 = 87
- Blue (33): 3=3, 3=3 → (3 × 16) + 3 = 48 + 3 = 51
- 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 }
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
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:
- Red (59): 59 ÷ 16 = 3 remainder 11 → 3 and B →
3B - Green (130): 130 ÷ 16 = 8 remainder 2 → 8 and 2 →
82 - Blue (246): 246 ÷ 16 = 15 remainder 6 → F and 6 →
F6 - 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"
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
);
}
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
- Design handoff: When copying colors from Figma, Sketch, or Photoshop (they output hex by default).
- CSS variables: Hex is compact and clean for defining design tokens like
--color-primary: #3B82F6;. - Quick prototyping: Faster to type
#000thanrgb(0,0,0). - Existing codebases: Most legacy CSS uses hex — stay consistent with the codebase.
When to Use RGB
- JavaScript calculations: When you need to manipulate individual channel values programmatically.
- Alpha transparency:
rgb(59 130 246 / 50%)is more readable than#3B82F680. - Accessibility audits: Contrast ratio formulas work directly with RGB values.
When to Use HSL
- Creating color palettes: Keep hue constant, vary saturation and lightness to generate consistent shades.
- Theming: Adjust lightness for dark/light mode:
hsl(217 91% 60%)→hsl(217 91% 80%). - Design systems: HSL maps naturally to how humans think about color (hue, vivid/muted, light/dark).
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:
00= 0 (minimum) andFF= 255 (maximum)80= 128 (middle) — useful for 50% gray:#808080- Equal R, G, B values always produce a shade of gray
- High first pair = reddish, high second = greenish, high third = bluish
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:
#FFFFFFon#000000— 21:1 ratio (maximum)#E4E4E7on#0F1117— 15.2:1 ratio (excellent)#9CA3AFon#0F1117— 7.8:1 ratio (good)#3B82F6on#0F1117— 5.3:1 ratio (passes AA)
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
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
#GG5733 or #ZZZZZZRight: 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
#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
#123456 can be shortened to #135Reality:
#135 expands to #113355, not #123456.Shorthand only works when each pair has identical digits:
#112233 → #123.
Mistake 5: Case Sensitivity in JavaScript
#ff5733 and #FF5733 identically, but string comparison in JavaScript does not:'#ff5733' === '#FF5733' → falseAlways normalize to lowercase (or uppercase) before comparing hex color strings in code.
Mistake 6: Ignoring Alpha Channel Order
#RRGGBBAAAndroid/iOS: Alpha often goes at the start —
#AARRGGBBIf 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 GeneratorConclusion
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:
- Hex codes encode RGB values in base-16 notation, giving you 16.7 million possible colors.
- Shorthand (
#RGB) saves bytes but only works for colors with repeating digit pairs. - Eight-digit hex (
#RRGGBBAA) adds alpha transparency with full browser support. - Use CSS custom properties to manage your color palette centrally.
- Always test color contrast for accessibility (4.5:1 minimum for text).
- Choose hex for design handoff and static values; use HSL when you need to generate color variations programmatically.
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.