
HEX codes are the most common way to write colors in CSS. This guide shows where you can use them, the shorthand and transparency forms, and how to organize colors with CSS variables.
HEX Syntax
A HEX color starts with # followed by hexadecimal digits:
#RRGGBB— six digits, e.g.#6C5CE7#RGB— three-digit shorthand, e.g.#FFF=#FFFFFF#RRGGBBAA— eight digits with transparency, e.g.#6C5CE780(≈50% opacity)#RGBA— four-digit shorthand with transparency
HEX codes are case-insensitive: #6c5ce7 works too.
Where to Use HEX Colors
body {
color: #111827; /* text */
background-color: #F8FAFC; /* page background */
}
.card {
background: #FFFFFF;
border: 1px solid #E2E8F0; /* border */
box-shadow: 0 4px 16px #1118270F; /* shadow with alpha */
}
a { color: #4F46E5; }
a:hover { color: #6C5CE7; }HEX colors also work in gradients, outlines, SVG fill and stroke, and the accent-color property for checkboxes and radio buttons.
Shorthand Rules
Three-digit shorthand only works when each pair has two identical digits: #AABBCC can become #ABC, but #6C5CE7 has no shorthand.
Transparency With HEX
The last two digits of an 8-digit code set opacity from 00 (transparent) to FF (solid). Common values:
| Opacity | Hex |
|---|---|
| 100% | FF |
| 80% | CC |
| 50% | 80 |
| 25% | 40 |
| 10% | 1A |
Our Color Picker generates these automatically when you lower the opacity slider.
Organize Colors With CSS Variables
Define your palette once and reuse it everywhere:
:root {
--color-primary: #6C5CE7;
--color-primary-dark: #4F46E5;
--color-text: #111827;
--color-muted: #64748B;
--color-bg: #F8FAFC;
}
.button {
background: var(--color-primary);
color: #FFFFFF;
}
.button:hover { background: var(--color-primary-dark); }Changing a brand color later becomes a one-line edit.
Best Practices
- Keep a single source of truth (variables) instead of scattering codes.
- Use consistent letter case across your codebase.
- Check text and background pairs with the Contrast Checker.
- Need RGB or HSL for a specific property? Convert instantly with the Color Converter.
How HEX Codes Actually Work
A HEX color code is simply a way of writing three numbers — the amounts of red, green and blue light — in base 16, also called hexadecimal. In everyday life we count in base 10 using the digits 0 to 9. Hexadecimal uses sixteen symbols: 0 to 9 and then A, B, C, D, E and F, where A means 10 and F means 15.
Each color channel uses two hexadecimal digits, which can represent any value from 00 (zero) to FF (255). So a six-digit code like #3A86FF breaks down into three pairs:
- 3A for red: 3 × 16 + 10 = 58
- 86 for green: 8 × 16 + 6 = 134
- FF for blue: 15 × 16 + 15 = 255
That means #3A86FF is exactly the same color as rgb(58, 134, 255). HEX is not a different color model; it is just a shorter way to write RGB values. Because each channel has 256 possible values, HEX can describe 256 × 256 × 256 = 16,777,216 different colors.
Reading HEX codes at a glance
With a little practice you can guess a color just by looking at its code:
- Pairs close to
00mean very little of that channel; pairs close toFFmean a lot. - When all three pairs are equal, such as
#777777, the color is a gray. - When the first pair is highest, the color leans red; when the middle pair is highest, it leans green; when the last pair is highest, it leans blue.
- High values in all pairs, such as
#F5EEE6, give light tints. Low values in all pairs, such as#1E1B2E, give dark shades.
Case Sensitivity and Formatting Conventions
HEX codes in CSS are not case-sensitive. #ff5733, #FF5733 and #Ff5733 all produce the same color. However, consistency makes code easier to read and search. Most teams pick one style and stick to it.
- Lowercase is common in modern codebases and is the default output of many formatting tools.
- Uppercase is popular in design tools and documentation because it is easier to read in screenshots.
Whichever style you use, always include the hash symbol (#). Without it, the browser will not recognize the value as a color and will ignore the declaration.
HEX vs RGB vs HSL: When to Use Each
CSS accepts several color formats, and each has strengths:
- HEX is compact, widely recognized and easy to copy from design tools. It is ideal for fixed brand colors.
- RGB and RGBA are easier to read as numbers and were traditionally used when transparency was needed.
- HSL describes colors by hue, saturation and lightness, making it much easier to create lighter or darker variations of the same hue by changing a single value.
In practice, many developers store brand colors as HEX and use HSL when they need to generate shades programmatically. All formats render the same final colors, so you can mix them freely in one stylesheet.
Applying HEX Colors to Common CSS Properties
Text and backgrounds
body {
color: #1F2937;
background-color: #F9FAFB;
}
h1 { color: #111827; }
.muted { color: #6B7280; }
Borders and outlines
.card {
border: 1px solid #E5E7EB;
border-radius: 12px;
}
button:focus-visible {
outline: 3px solid #2563EB;
outline-offset: 2px;
}
Shadows
Box shadows look more natural when they use a dark color with transparency instead of pure black. The 8-digit HEX format is perfect for this:
.card { box-shadow: 0 10px 25px #0F172A1A; }
The last two digits, 1A, set the shadow to about 10% opacity.
Gradients
.hero { background: linear-gradient(135deg, #6366F1, #EC4899); }
SVG icons
HEX codes also work inside inline SVG through the fill and stroke properties:
.icon { fill: #10B981; stroke: #065F46; }
Building a Complete Color System with HEX
A professional website rarely uses random colors. Instead, it relies on a small, well-organized color system. A typical system contains four groups:
- Brand colors: one primary and one or two secondary colors that represent the company.
- Neutral scale: eight to ten grays from almost white to almost black, used for text, borders and backgrounds.
- Semantic colors: fixed colors for success, warning, error and information messages.
- Shades of each brand color: lighter and darker steps used for hover states, backgrounds and badges.
Here is an example using CSS custom properties:
:root {
/* Brand */
--primary-50: #EFF6FF;
--primary-100: #DBEAFE;
--primary-500: #3B82F6;
--primary-600: #2563EB;
--primary-700: #1D4ED8;
/* Neutrals */
--gray-50: #F9FAFB;
--gray-200: #E5E7EB;
--gray-500: #6B7280;
--gray-900: #111827;
/* Semantic */
--success: #16A34A;
--warning: #D97706;
--error: #DC2626;
--info: #0284C7;
}
Numbering shades from 50 (lightest) to 900 (darkest) is a popular convention because it makes it easy to see how light or dark each color is just from its name. Designers and developers can then talk about “primary 600” instead of copying codes back and forth.
Theming and Dark Mode with HEX Variables
When colors are stored in variables, adding a dark theme becomes simple. You keep the same variable names and change their values inside a media query or a theme class:
:root {
--bg: #FFFFFF;
--text: #111827;
--surface: #F3F4F6;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0B1120;
--text: #E5E7EB;
--surface: #1E293B;
}
}
body { background: var(--bg); color: var(--text); }
.card { background: var(--surface); }
Every component that uses the variables updates automatically. This approach avoids duplicating hundreds of rules for dark mode and keeps the stylesheet easy to maintain.
Tips for choosing dark mode HEX values
- Avoid pure black
#000000for large backgrounds; very dark blue-grays like#0F172Afeel softer. - Avoid pure white text on dark backgrounds; light grays such as
#E5E7EBreduce glare. - Use slightly lighter, less saturated brand colors in dark mode so they do not look neon.
Checking Contrast with HEX Colors
Choosing attractive colors is only half the job. Text must also be readable for everyone, including people with low vision or color blindness. The Web Content Accessibility Guidelines (WCAG) define minimum contrast ratios:
- 4.5:1 for normal body text.
- 3:1 for large text (about 24px regular or 19px bold) and for important interface elements such as icons and input borders.
- 7:1 for the stricter AAA level.
Some examples: #6B7280 gray on white reaches about 4.8:1 and passes for body text. #9CA3AF on white is only about 2.5:1 and fails. #2563EB blue on white is about 5.2:1 and passes, which is why it is a popular link and button color.
Common HEX Mistakes and How to Fix Them
- Missing the hash.
color: FF5733;is invalid. Writecolor: #FF5733;. - Wrong length. HEX codes must have 3, 4, 6 or 8 digits. A five-digit code like
#FF573is ignored by the browser. - Using the letter O instead of zero.
#OOOOOOis invalid because O is not a hexadecimal digit. - Invalid characters. Only 0–9 and A–F are allowed. Letters like G or Z will break the color.
- Too many similar colors. Codebases often collect dozens of almost identical grays like
#333,#323232and#343434. Consolidate them into a single variable. - Hard-coding colors everywhere. Repeating the same HEX code in many files makes changes slow and error-prone. Use variables instead.
Finding the HEX Code of Any Color
There are several easy ways to get the HEX value of a color you see:
- Browser developer tools: right-click an element, choose Inspect and click the color swatch next to any color property. The built-in picker shows the HEX code and lets you sample any pixel on the page.
- Design tools: Figma, Canva, Photoshop and similar tools show HEX codes in their color panels.
- Online color pickers: choose a color visually and copy its HEX, RGB and HSL values instantly.
- Image color pickers: upload a photo or screenshot and click any point to get its exact HEX code.
Named Colors vs HEX Codes
CSS also supports around 140 named colors such as red, tomato, steelblue and rebeccapurple. Named colors are easy to remember and handy for quick prototypes, but they have limits. There are only a small number of them, many look dated, and they rarely match an exact brand color. A brand blue might be #1D4ED8, while the nearest named color, royalblue, is #4169E1, which looks noticeably different.
For production websites, HEX codes are the better choice because they give you precise control over every color. You can still use named colors like transparent and currentColor, which have special behavior: transparent creates an invisible color, and currentColor reuses the element’s text color, which is very useful for icons and borders that should always match the text.
Converting HEX to Other Formats
Sometimes you need a HEX color in another format, for example RGB for a canvas script, HSL to create lighter variations, or CMYK for print. Converting by hand is possible but slow:
- Split the code into three pairs.
- Convert each pair from hexadecimal to decimal to get the RGB values.
- Use the RGB values in formulas to calculate HSL or CMYK.
For example, #10B981 becomes rgb(16, 185, 129) and approximately hsl(160, 84%, 39%). Because these calculations are easy to get wrong, most designers use a color converter to get every format at once and copy the one they need.
A note on print
HEX describes colors for screens, which use light. Printers use ink, so a HEX color converted to CMYK may look slightly different on paper, especially bright blues, greens and oranges. When color accuracy in print matters, request a printed proof before a large run.
HEX Color Cheat Sheet
#000000black — rgb(0, 0, 0)#FFFFFFwhite — rgb(255, 255, 255)#FF0000pure red — rgb(255, 0, 0)#00FF00pure green — rgb(0, 255, 0)#0000FFpure blue — rgb(0, 0, 255)#808080medium gray — rgb(128, 128, 128)#FFA500orange — rgb(255, 165, 0)#800080purple — rgb(128, 0, 128)#008080teal — rgb(0, 128, 128)#000080navy — rgb(0, 0, 128)
Keep this list handy as a reference point. Once you know these basic values, it becomes much easier to understand and adjust any HEX code you meet.
Organizing HEX Colors in Larger Projects
As a website grows, color management becomes a real maintenance task. A small brochure site might use eight colors, while a large web application can easily reference more than a hundred. A few habits keep things under control.
Name colors by purpose, not appearance
Variable names like --blue or --light-gray become confusing when the design changes. If the brand moves from blue to green, a variable called --blue that now holds green is misleading. Purpose-based names such as --color-primary, --color-text-muted and --color-border stay accurate no matter which HEX value they hold.
Use two layers of variables
Many design systems use a base layer of raw colors and a second layer of semantic tokens that point to them:
:root {
--blue-600: #2563EB;
--gray-900: #111827;
--color-primary: var(--blue-600);
--color-text: var(--gray-900);
}
Components only use the semantic tokens. To change the theme, you update the mapping instead of searching for every place a color was used.
Document your palette
Keep a simple page or document listing every color, its HEX code, its variable name and where it should be used. This helps new team members, freelancers and clients apply colors correctly and prevents the slow creep of near-duplicate shades.
Audit regularly
Every few months, search your stylesheets for HEX codes that are not stored in variables. Replace them with the closest existing token, or add a new token if the color is truly needed. Regular audits keep your design consistent and your CSS smaller.
With these practices, HEX colors stop being scattered strings of characters and become a clear, reliable part of your design system.
Testing Your HEX Colors Before Launch
Before you publish a new design, take a few minutes to test how your HEX colors behave in real conditions. Open the page on a phone, a laptop and, if possible, an older monitor, because screens differ in brightness and color calibration. Check the site outdoors on a phone, where glare reduces contrast and pale colors can disappear.
Next, test with a color blindness simulator to see whether important information, such as error messages or chart lines, can still be understood. Finally, run every text and background pair through a contrast checker. Fixing a problem at this stage takes minutes, while fixing it after launch often means updating many pages and components.
Frequently Asked Questions
Do I need the # symbol in CSS?
Yes. In CSS, HEX colors must start with #.
Is 8-digit HEX supported?
Yes, in all modern browsers. For very old browsers, use rgba() instead.
Should I use HEX or named colors?
HEX gives you exact control over 16.7 million colors; named colors are limited to about 140 options. See our HTML Color Codes reference for the named list.