·6 min read·Blog

CSS Color Codes Explained: HEX, RGB, HSL, and When to Use Each

CSS has four main ways to specify the same color — and each has a different use case. Here's when to use HEX, RGB, RGBA, HSL, and HSLA, and how to convert between them free.

HEX color codes

HEX (hexadecimal) colors are the most widely used format in CSS — the familiar #FF6B6B format. Each pair of hex digits represents one color channel: Red, Green, Blue.

color: #FF6B6B;   /* Red=FF (255), Green=6B (107), Blue=6B (107) */
color: #000000;   /* Black */
color: #FFFFFF;   /* White */
color: #000;      /* Shorthand for #000000 */
color: #F60;      /* Shorthand for #FF6600 */

The 3-digit shorthand works when both digits of each channel are the same: #F60 expands to #FF6600. If they're different (like #F67 expanding to #FF6677), the shorthand doesn't exist.

When to use HEX:Design-to-code handoffs (Figma, Sketch export hex by default), brand color specifications in style guides, any context where you're copying a color from a tool.

Convert HEX to RGB and HSL with the free color picker.

RGB and RGBA

RGB specifies colors as three numbers from 0–255 — the same values as HEX, just in decimal:

color: rgb(255, 107, 107);      /* Same as #FF6B6B */
color: rgb(0, 0, 0);           /* Black */
color: rgb(255, 255, 255);     /* White */

/* RGBA adds a fourth value: alpha (0=transparent, 1=opaque) */
color: rgba(255, 107, 107, 0.5);   /* 50% transparent red */
color: rgba(0, 0, 0, 0.8);        /* 80% opaque black overlay */

When to use RGB/RGBA: Any time you need transparency — semi-transparent overlays, shadows with opacity, dimmed backgrounds. The alpha channel (rgba) is the reason to choose this format over HEX.

Note: Modern CSS also supports 8-digit HEX for transparency: #FF6B6B80 (the last two digits are the alpha). But RGBA is more readable.

HSL and HSLA

HSL stands for Hue, Saturation, Lightness — a more intuitive way to think about colors:

/* hsl(hue, saturation, lightness) */
color: hsl(0, 100%, 50%);     /* Pure red */
color: hsl(120, 100%, 50%);   /* Pure green */
color: hsl(240, 100%, 50%);   /* Pure blue */

/* Adjusting lightness: easy to create shades */
color: hsl(0, 100%, 70%);     /* Lighter red */
color: hsl(0, 100%, 30%);     /* Darker red */

/* HSLA adds transparency */
color: hsla(0, 100%, 50%, 0.5);  /* 50% transparent red */

Hue is a degree on the color wheel (0–360): 0/360 = red, 120 = green, 240 = blue. Saturation is how vivid the color is (0% = grey, 100% = full color). Lightness is how light or dark it is (0% = black, 100% = white, 50% = the "pure" color).

When to use HSL: When you want to programmatically create color variations — lighter/darker versions of the same hue, color palettes from a single base color, hover states and focus rings that are predictably related to the base color.

The practical guide: which format for what

SituationFormat to useWhy
Static brand colors in CSS variablesHEXStandard format from design tools
Semi-transparent overlay or shadowRGBA or HSLAAlpha channel support
Generating a color palette from one base hueHSLLightness/saturation easy to manipulate
Hover/focus states (slightly darker/lighter)HSLAdjust lightness from a variable
Copying a color from Chrome DevToolsAny (DevTools shows all formats)Click the color swatch to cycle formats
CSS custom properties for themingHSL channel valuesAllows opacity override via hsl(var(--h) var(--s) var(--l) / 50%)

CSS color keywords

CSS also supports 140+ named color keywords — red, blue, tomato, rebeccapurple, cornflowerblue. These are fine for quick prototyping but not for production — the actual RGB values behind keywords aren't always intuitive, and they don't convey the same precision as a specific hex code.

One special keyword: transparent = rgba(0,0,0,0). Use it for fully transparent colors (e.g., transparent button backgrounds, transparent borders for focus ring tricks).

Modern CSS: color-mix() and oklch

CSS is evolving with two new color capabilities in 2026:

  • color-mix(in srgb, red 50%, blue) — mix two colors in CSS without JavaScript. Supported in Chrome 111+, Firefox 113+, Safari 16.2+.
  • OKLCH — a perceptually uniform color space where equal steps in lightness look equally different to the human eye (unlike HSL, where equal steps in lightness can look uneven). Good for generating accessible color palettes.

Related tools


Written by Achraf A., founder of TheFreeAITools.

Browse by category

Not sure which tool you need? Start with a category.

Everything you can do — for free

No software to buy. No account to create. Just open a tool and get it done.

Work with images

Compress photos before sending them by email, resize pictures for social media, remove backgrounds, or pick the perfect color for a design project — all without installing any app.

Edit and format text

Count words and characters in an essay, compare two documents side by side, convert text to different formats, or generate placeholder text for a presentation.

Stay safe online

Create a strong unique password in one click, check how secure a password is, encode or decode data, and generate secure tokens — your data never leaves your device.

Calculate anything

BMI, loan repayments, unit conversions, date differences, and dozens of other everyday calculations — no spreadsheet or formula knowledge required.

The Free AI Tools is a free collection of 221+ online tools that work directly in your web browser — no download, no installation, no account required. Whether you need to compress an image for email, count words in an essay, generate a strong password, create a QR code for your business, or format JSON for development — you will find a simple, free tool here.

Every tool is privacy-first: your files, text, and data never leave your device. Tools cover image editing, text processing, developer utilities, security & encoding, SEO & web, design & CSS, and more.

☕ Support Us