Skip to content
Hex Color Picker

Blog

Using Hex, RGB and HSL Colors in CSS and HTML

By the Hex Color Picker Team. Published . 8 minute read.

CSS gives you several ways to write the same color: a name such as dodgerblue, a hex code such as #1E90FF, a function such as rgb(30 144 255) or hsl(210 100% 56%), and newer functions for more advanced color work. All of them produce exactly the same blue when they describe the same color, so the choice is about readability and flexibility, not about how the color looks.

This guide shows every common syntax with working examples, explains how to add transparency, covers CSS variables and dark mode, and lists the HTML places where colors appear. If you need to convert a value between formats while you work, the hex to RGB and hex to HSL converters do it instantly.

Where colors are used in CSS

A color value can go in any property that takes a color. These are the ones you will use most:

.card {
  color: #141735;                 /* text */
  background-color: #EEF1F7;     /* fill behind the content */
  border: 2px solid #1E90FF;     /* border color */
  outline: 3px solid #BF2163;    /* focus ring */
  box-shadow: 0 2px 8px rgb(0 0 0 / 20%);
}

input[type="checkbox"] {
  accent-color: #1E90FF;         /* tints native form controls */
}

svg .icon {
  fill: #1E90FF;                 /* SVG fill and stroke accept colors too */
  stroke: #141735;
}

If a color value is invalid, the browser ignores that declaration entirely and falls back to whatever applied before, usually the inherited or default color. So a typo in a hex code does not produce a wrong color, it produces no change, which can be harder to notice.

Named colors

CSS has 148 color keywords, including both the gray and grey spellings for each grey. You can write them directly:

h1 { color: navy; }
.banner { background: coral; }

Names are quick for prototypes and readable in code, but they cover a tiny part of the color space and they mean one fixed value each. Browse the standard ones on the named colors pages. There are also two special keywords: transparent, which is a fully see-through color, and currentcolor, covered below.

Hex codes

Hex is the most common format for a precise color. CSS accepts four lengths:

.a { color: #1E90FF; }     /* 6 digits: red, green, blue */
.b { color: #19F; }        /* 3 digits: each digit is doubled, so #1199FF */
.c { color: #1E90FF80; }   /* 8 digits: the last pair is opacity, here about 50% */
.d { color: #19F8; }       /* 4 digits: the shorthand for 8 digits */

Letters can be upper or lower case. Any other length, such as five or seven digits, is invalid. If you want to understand what the digits mean, read how hex color codes work.

rgb() and rgba()

The rgb() function writes the same three numbers in decimal. There are two syntaxes, and both are valid today:

.legacy { color: rgb(30, 144, 255); }          /* commas */
.modern { color: rgb(30 144 255); }            /* spaces */
.alpha  { color: rgb(30 144 255 / 50%); }      /* space syntax with opacity after a slash */
.old    { color: rgba(30, 144, 255, 0.5); }    /* legacy name, still works */

rgba() is simply an older name for rgb() with an opacity value. In current CSS you can write opacity in any rgb() call, so rgba() is no longer needed, though you will see it in a lot of existing code. Opacity can be a number between 0 and 1 or a percentage.

Use rgb() when you want the channel numbers to be visible, for instance when they are generated by a script or when you want to add opacity.

hsl() and hsla()

hsl() takes a hue in degrees, then saturation and lightness as percentages:

.base  { color: hsl(210 100% 56%); }
.hover { color: hsl(210 100% 48%); }       /* a darker version: only lightness changed */
.tint  { color: hsl(210 100% 90%); }       /* a pale version */
.soft  { color: hsl(210 100% 56% / 40%); } /* with opacity */

The percent signs are required on saturation and lightness. The comma syntax hsl(210, 100%, 56%) works too. HSL shines when you derive related colors, because you change a single number to get a darker or lighter one. The guide to HSL for designers goes into this in more detail.

Transparency: alpha colors versus opacity

Two things are easy to confuse:

.overlay-a { background: rgb(0 0 0 / 50%); }  /* only the background is translucent; text on it stays solid */
.overlay-b { background: black; opacity: 0.5; } /* everything inside fades too */

For backgrounds and shadows, an alpha color is nearly always the better choice. It also matters for accessibility, because contrast is measured against the real blended color, which you can read about in the color contrast guide.

currentcolor

The keyword currentcolor means "use whatever this element's color is". It saves you from repeating a value:

.button {
  color: #1E90FF;
  border: 2px solid currentcolor;   /* border follows the text color */
}
.button svg { fill: currentcolor; } /* icon follows the text color */

This is especially handy for icons, because changing the text color then changes the icon too, with no extra rules.

CSS variables

Custom properties let you define a color once and use it everywhere, which is the single biggest improvement you can make to a stylesheet's color handling:

:root {
  --brand: #1E90FF;
  --text: #141735;
  --paper: #EEF1F7;
}

body { background: var(--paper); color: var(--text); }
a    { color: var(--brand); }
.button { background: var(--brand); }

Change --brand in one place and every use updates. You can also give var() a fallback in case the variable isn't defined: color: var(--brand, #1E90FF);.

Name variables by their role (--text, --surface, --accent) instead of by their value (--blue). Then a rebrand or a dark mode changes the value but not the names. The guide on building a website palette shows a full role-based set.

A useful trick is to keep the hue as its own variable, so one value re-themes everything:

:root { --h: 210; }
.button       { background: hsl(var(--h) 100% 56%); }
.button:hover { background: hsl(var(--h) 100% 48%); }

Change --h to 340 and every button, hover state and accent that uses it turns pink.

Dark mode with variables

Because colors are variables, dark mode is a matter of redefining a few values under a media query:

:root {
  color-scheme: light dark;
  --paper: #EEF1F7;
  --text: #141735;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0D0F21;
    --text: #ECEEFB;
  }
}

body { background: var(--paper); color: var(--text); }

The color-scheme property also tells the browser to use dark versions of built-in controls and scrollbars. Check contrast in both themes separately, since a pair that passes in one can fail in the other.

Newer color functions

Recent browsers support more color tools. They are worth knowing about, but check a browser support table before depending on them for something critical:

.mix   { background: color-mix(in srgb, #1E90FF 70%, white); }  /* blends two colors */
.ok    { background: oklch(70% 0.15 250); }                      /* lightness, chroma, hue */

color-mix() blends two colors in a chosen color space, which is a clean way to make tints and shades from a variable. oklch() describes a color using lightness that matches human perception better than HSL does, which makes scales more even. For everyday work, hex, rgb() and hsl() are still the safest choices, since they work everywhere. If you use a newer function, give older browsers a plain fallback on the line before it:

.button {
  background: #1E90FF;                          /* fallback */
  background: color-mix(in srgb, #1E90FF 90%, white);
}

Colors in HTML

CSS is where almost all color belongs, but a few HTML features take colors too:

In JavaScript, you assign colors as strings: element.style.color = "#1E90FF", or ctx.fillStyle = "rgb(30 144 255)" on a canvas. Any valid CSS color string works.

Which format should you pick?

A simple policy keeps a codebase tidy:

SituationUse
A fixed brand or design-token colorHex, stored in a CSS variable
A color with transparencyrgb(... / ...) or an 8-digit hex
Derived states (hover, pressed, tint)hsl() with a changed lightness, or color-mix()
A quick prototypeA named color
A converted value from a design toolWhatever the tool exports, then convert it to your standard

Whatever you choose, be consistent within a project. Mixed formats work, but they make searching for a color and auditing a palette harder than it needs to be.

Common mistakes

  1. Missing hash. color: 1E90FF; is invalid. In CSS, hex codes need the #.
  2. Wrong number of digits. Five or seven digits invalidate the declaration silently.
  3. Missing percent signs in `hsl()`. Write 56%, not 56.
  4. Using `opacity` when you meant an alpha color. Opacity fades the text and children too.
  5. Hardcoding the same hex in fifty places. Use a variable.
  6. Forgetting to test dark mode and hover states for contrast.
  7. Using a brand color as text without checking contrast. Many bright colors need to be darkened for text.

Frequently asked questions

Is hex or rgb() better in CSS?

Neither renders differently. Use hex for compact fixed values and rgb() when you want opacity or need to read the channels.

Does CSS support CMYK?

No. Browsers work in RGB, so CSS has no cmyk() value you can use. Convert CMYK to RGB or hex first with the CMYK to hex converter or CMYK to RGB converter.

Can I use hex codes with transparency?

Yes. Eight-digit hex codes add an opacity pair, for example #1E90FF80 for about 50%.

What is the difference between rgba() and rgb()?

None in current CSS. rgba() is the older name for rgb() with opacity, and both accept an opacity value.

How do I make a color darker in CSS?

Use hsl() and lower the lightness, or use color-mix() with black. Both give predictable results without recalculating RGB channels.

Where is the official definition?

The CSS Color Module is published by the W3C at www.w3.org/TR/css-color-4.

Try it yourself

More guides