Skip to content
Tutorials Center
My learning

Topics

Lesson 58 of 122 ยท CSS and JavaScript in HTML

Colors

Write colors with names, hex codes, rgb() and hsl(), add transparency with alpha, reuse colors with currentColor and check text contrast for readability.

Color sets the mood of a page, groups related things and draws the eye to what matters. In HTML pages, colors are always set with CSS โ€” the old color and bgcolor attributes are obsolete.

CSS gives you several ways to write the same color. In this lesson you will learn the four you will see everywhere โ€” named colors, hex codes, rgb() and hsl() โ€” plus transparency, the handy currentColor keyword and how to make sure your text stays readable.

Where colors go

The examples use the style attribute to keep things short. These properties accept a color:

Property Colorsโ€ฆ
color the text (and, by default, underlines and borders).
background-color the element's background.
border-color (or the border shorthand) the border.
outline-color the focus outline.
accent-color checkboxes, radio buttons and range sliders.

Named colors

CSS knows about 140 color names, from the obvious (red, blue, white) to the poetic (papayawhip, cornflowerblue, rebeccapurple). Names are case-insensitive, though lowercase is the convention.

Named colors

Names are easy to read and great for quick experiments. Real designs, though, need exact shades, so most code uses the numeric formats below.

Two special keywords are worth knowing: transparent (fully see-through) and currentColor (covered later in this lesson).

How screens make color

Your screen builds every color by mixing red, green and blue light. Each channel ranges from off to full brightness. All three off is black; all three full is white; equal amounts of each give gray. Every numeric color format is just a different way of writing those three amounts.

Hex colors

A hex color starts with # and is followed by three pairs of hexadecimal digits โ€” one pair each for red, green and blue:

TEXT
#  1e  90  ff
   โ”‚   โ”‚   โ””โ”€ blue:  ff = 255 (full)
   โ”‚   โ””โ”€โ”€โ”€โ”€โ”€ green: 90 = 144
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ red:   1e = 30

Hexadecimal counts from 0 to 9 and then a to f, so each pair goes from 00 (0) to ff (255).

  • 6 digits โ€” #1e90ff, the most common form.
  • 3 digits โ€” a shorthand where each digit is doubled: #f80 means #ff8800. Only works when both digits of every pair are the same.
  • 8 digits โ€” the last pair is the alpha (opacity): #1e90ff80 is that blue at about 50% opacity.
  • 4 digits โ€” the short form with alpha: #f808.

Hex codes are compact and design tools export them everywhere, which is why you will see them so often.

rgb()

The rgb() function writes the same three channels in ordinary decimal numbers from 0 to 255:

CSS
color: rgb(30 144 255);          /* same as #1e90ff */
color: rgb(30 144 255 / 50%);    /* the same at 50% opacity */

The modern syntax separates values with spaces and puts the alpha after a slash. You will also meet the older comma syntax, rgb(30, 144, 255) and rgba(30, 144, 255, 0.5). Both still work in every browser; rgba() is now simply an alias of rgb().

hsl(): colors the way people think

RGB is how screens think. HSL is closer to how people describe color:

  • Hue โ€” the position on the color wheel, in degrees: 0 red, 120 green, 240 blue, back to 360 red.
  • Saturation โ€” how vivid it is: 0% is gray, 100% is fully colorful.
  • Lightness โ€” 0% is black, 50% is the pure color, 100% is white.
CSS
color: hsl(210 80% 45%);           /* a strong blue */
color: hsl(210 80% 45% / 0.3);     /* the same, 30% opaque */

The big advantage: building a palette is easy. Keep the hue, and change only the lightness to get a family of tints and shades.

A palette from one hue

Newer color formats. Modern browsers also support oklch(), lab(), color-mix() and wide-gamut colors with color(). oklch() is especially popular because equal lightness values look equally light, which HSL does not guarantee. Once you are comfortable with the formats here, it is worth exploring.

Transparency and alpha

Every numeric format can carry an alpha value, from fully transparent (0 or 0%) to fully opaque (1 or 100%). A semi-transparent color lets whatever is behind it show through:

Alpha transparency

Do not confuse color alpha with the opacity property. opacity: 0.5 fades the whole element, including its text and children. An alpha color only affects that one color, so the text stays crisp.

Older syntax: rgba(), hsla() and commas

Before the slash syntax existed, alpha needed its own function names: rgba(30, 144, 255, 0.5) and hsla(0, 100%, 50%, 0.5) (note the A for alpha, and commas between the values). Both still work, and you will meet them in older code. In new code write rgb() and hsl() with an optional / alpha, as above. For example, hsl(0 100% 50% / 0.5) and hsla(0, 100%, 50%, 0.5) are the same translucent red.

currentColor

currentColor means "whatever the color property is on this element". It keeps related colors in sync automatically:

currentColor

Each badge sets only color, and the border follows. It is also very useful for inline SVG icons: fill="currentColor" makes an icon match the surrounding text.

Contrast: make it readable

A beautiful color scheme is useless if people cannot read the text. Contrast is the difference in brightness between the text and its background. The Web Content Accessibility Guidelines (WCAG) set minimum contrast ratios:

Text Minimum ratio (level AA)
Normal body text 4.5 : 1
Large text (about 24px, or 18.66px bold) 3 : 1
Icons and the borders of form controls 3 : 1

Black on white is 21:1, the maximum. Light gray on white often fails. Browser DevTools show the contrast ratio when you click a color in the Styles panel, so checking takes seconds.

Good and poor contrast

Never use color alone to carry meaning. About 1 in 12 men and 1 in 200 women have some form of color vision deficiency. "Fields in red are required" fails them. Add text, an icon or a pattern as well, such as an asterisk and the word "required". See Accessibility Basics.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these is the same color as #ff6600?

    The 3-digit shorthand doubles each digit: #f60 becomes #ff6600.
  2. 2. In hsl(30 90% 50%), which number would you change to make a darker version of the same color?

    Lowering the lightness moves the color toward black while keeping the same hue.
  3. 3. You want a box with a see-through background but fully solid text. What should you use?

    opacity fades everything inside the element, including the text. An alpha color only affects the background.
  4. 4. What is the WCAG AA minimum contrast ratio for normal-sized body text?

    Normal text needs at least 4.5:1. Large text needs 3:1, and 21:1 is the maximum possible (black on white).
  5. 5. A button has color: navy and border: 2px solid currentColor. What color is the border?

    currentColor takes the element's own color value, so the border is navy.

Summary

  • Set colors with CSS properties such as color, background-color and border-color.
  • Named colors are readable; hex (#rrggbb, #rgb, #rrggbbaa) and rgb() describe red, green and blue amounts.
  • hsl() uses hue, saturation and lightness, which makes palettes easy to build.
  • Add alpha with 4/8-digit hex or / alpha in rgb()/hsl(); opacity fades the whole element.
  • currentColor reuses the text color; always check contrast and never rely on color alone.

Next lesson: Adding CSS to HTML โ€” inline styles, <style> blocks and external stylesheets.