Choose Colors in Modern CSS

In Link Your First Stylesheet, you gave the work day notice its first colors by name: honeydew behind the page, darkslategray for the text, and darkgreen for the heading. Named colors are a quick start, but CSS has fewer than 150 of them, and the garden’s own green isn’t one. It’s the green on the garden’s printed flyers, and Rosa would like the website to match.

In this lesson, you’ll write that exact green, read the color formats you’ll meet in other people’s stylesheets, and build a small family of greens whose shades step evenly from light to dark. You’ll also make a color partly see-through. The flyer’s green comes as a hex code, so start there.

Write the Garden’s Green as a Hex Code

Rosa’s flyer gives its green as #006633, and no named color matches it. A hex color is a # followed by three pairs of hexadecimal digits drawn from 0–9 and a–f, one pair each for red, green, and blue.

Each pair runs from 00 to ff, so 0 to 255, and says how much of that light the screen mixes in. In #006633, that’s no red, 102 of green, and 51 of blue. Because the screen mixes light, #ffffff is white and #000000 is black.

Open events/events.css. It still holds the comment, the body rule, and the h1 rule from the first lesson, so the notice, the page you built in Structuring Web Pages With HTML, looks like this in the live preview:

The work day notice on a honeydew background, with a dark green main heading, dark slate gray text, and two blue links.
The Notice Before Its New Colors

Change the heading’s color from darkgreen to the flyer’s green:

Language: CSS Filename: events/events.css
h1 {
  color: #006633;
}

Save, and the heading shifts only a little, because darkgreen was close. Compared with the screenshot above, the green is a touch lighter and bluer. It’s now the garden’s exact green, the one on the flyers:

The work day notice on honeydew, with the main heading now in the garden's green, slightly lighter than before, dark slate gray text, and two blue links.
The Heading in the Garden's Own Green

You can write the same three numbers in decimal with the rgb() function: rgb(0 102 51). Replace the hex code with it and save. Nothing changes on the page, which confirms that both describe the same color. In older stylesheets, you’ll see commas, rgb(0, 102, 51), and short hex codes such as #063, where each digit is doubled to give #006633. Both still work.

CSS has more than one way to write the same green because each format describes a color differently. That starts to matter as soon as you want a lighter or darker version of it.

Build a Family of Greens With oklch()

The notice needs more than one green. The job headings and the body text should belong to the same family as the main heading, each a step darker. Hex and rgb() don’t help here, because neither has a number for lightness. To darken #006633, you’d have to work out new red, green, and blue values that together keep the same shade of green.

The hsl() function describes a color the way people talk about it. Its first value is the hue, an angle on a color wheel: 0 is red, 120 is green, and 240 is blue.

Then comes the saturation, from 0 percent for gray to 100 percent for full color, and the lightness, from 0 percent for black to 100 percent for white. The garden’s green is hsl(150 100% 20%), the same color as #006633. To darken it, you’d lower only the lightness, which looks like exactly the control you need.

Watch out, though: hsl() lightness is calculated from the red, green, and blue numbers, and it doesn’t track how light a color looks. Both hsl(60 100% 50%) and hsl(240 100% 50%) say 50 percent, yet the first is a yellow almost as light as white and the second is a dark blue.

A palette built by changing only the hue would look uneven, as the widget below demonstrates. It paints every hue at one fixed lightness, once as hsl(H 100% 50%) and once in oklch(), whose three numbers you’ll meet in a moment.

The same hue number lands on a different color in each row, because oklch() has its own color wheel. So compare how light the two rows look, and ignore which color each cell is. Drag the Hue slider or click a strip to move along the wheel, then check Show as gray to keep only how light each color looks:

Interactive diagram — enable JavaScript to view.

In gray, the hsl() strip jumps from almost white at hue 60 to a dark gray at 240, while every oklch() swatch turns into the same gray. That even lightness is what a palette needs.

The oklch() function takes three values too. L is the lightness as people see it, from 0 for black to 1 for white. C is the chroma, how vivid the color is: 0 is gray, and screen colors rarely go above about 0.3. H is the hue angle, on a different wheel from the one hsl() uses.

Written in oklch(), the garden’s green is oklch(0.447 0.116 152.7), the same color as #006633. It works in every current browser.

Now build the family: keep the hue, and step the lightness down by 0.1 for each shade. Lower the chroma as you go, too, because very dark colors can’t be very colorful on a screen. Change the body color, update h1, and add an h2 rule:

Language: CSS Filename: events/events.css
body {
  background-color: honeydew;
  color: oklch(0.247 0.05 152.7);
  font-family: system-ui, sans-serif;
}

h1 {
  color: oklch(0.447 0.116 152.7);
}

h2 {
  color: oklch(0.347 0.08 152.7);
}

Save, and the notice is in three greens, one step apart. The main heading has the garden’s green, the job headings are a shade darker, and the text is darker still, on the same honeydew background. Compared with the last screenshot, the job headings and the text have turned green:

The work day notice on a honeydew background, with a green main heading, darker green job headings, very dark green text, and two blue links.
The Notice in Three Shades of the Garden's Green

The three shades look about evenly spaced because their L values are. That’s the recipe, and you can check it by using it yourself on another hue before you move on.

Try a blue family for the notice: keep the hue at 250, a slate blue, and the chroma at 0.06 for all three shades. At that chroma, every L from 0.25 to 0.8 fits the screen. Choose three L values in that range, one each for h1, h2, and body, so that each is the same step below the one before and the body text is the darkest. Put them into the three rules and save.

Check your family two ways. In numbers, subtract the h2 L from the h1 L and the body L from the h2 L, and the two differences should be equal. On the page, the main heading, the job headings, and the text should step evenly from light to dark, as the greens did. Here’s one answer, with L values of 0.5, 0.4, and 0.3:

The work day notice on honeydew in three slate blues, the main heading lightest, the job headings darker, and the text darkest, with two blue links.
A Blue Family Built With the Same Recipe

Whether the text is dark enough to read is a question for the next lesson. For now, put the three greens back, because the rest of this lesson builds on them. To add more shades, such as a lighter green for a background, keep stepping L by 0.1 and stay within its range of 0 to 1. Lower the chroma again as you near white, because very light colors can’t be very colorful on a screen either.

When a shade asks for more color than the screen can show, the browser swaps in the closest color it can, and the steps stop looking even. Check each new shade by eye.

The two links at the bottom of the notice are still the browser’s blue, or purple once visited, and their color is the only one on the page that isn’t the garden’s. Rosa would like them in the garden’s green, with an underline soft enough that it doesn’t compete with the words.

A softer line is a see-through one. The space-separated rgb(), hsl(), and oklch() forms take a fourth value, the alpha, after a slash: 0 is fully transparent and 1, or 100 percent, is fully opaque. The older comma form takes it after a third comma instead, as in rgb(0, 102, 51, 40%). A hex code takes it as a fourth pair, so #00663366 is the garden’s green at 40 percent. The keyword transparent is a color you can’t see at all.

You’ll use alpha on the links’ underlines. Here are the links now, in the browser’s blue with a solid underline the same color as the text:

The end of the work day notice, with the two links in the browser's blue and a solid blue underline under each.
The Links With the Browser's Solid Blue Underline

Add a rule for links at the end of events.css:

Language: CSS Filename: events/events.css
a {
  color: oklch(0.447 0.116 152.7);
  text-decoration-color: oklch(0.447 0.116 152.7 / 40%);
}

The text-decoration-color property sets the underline’s color. The browser’s default link rule draws that line with text-decoration: underline. Save, and the links turn green, with a pale green line under them where the honeydew shows through:

The end of the work day notice, with the two links in the garden's green and a faint, see-through green underline under each.
Green Links With See-Through Underlines

The rule has a weakness, though. You’ve typed the same green twice. Change the links’ color later, forget the second line, and the underline no longer matches. CSS has a keyword for this element’s own text color, currentColor. It’s already the underline’s default color, which is why the browser’s underline was blue like the link.

To make a see-through version of it, use color-mix(), which mixes two colors in your chosen color space, a system for representing colors. Mixing 40 percent of currentColor with transparent gives the text’s color at 40 percent. Replace the second declaration:

Language: CSS Filename: events/events.css
a {
  color: oklch(0.447 0.116 152.7);
  text-decoration-color: color-mix(
    in oklch, currentColor 40%, transparent
  );
}

That’s all you need to know about color-mix() for now. Save, and the page looks the same, but you’ve written the green only once. To confirm that the underline follows, change the link’s color to the darker oklch(0.347 0.08 152.7) and save. The underline darkens with the text. Set the link’s color back to the garden’s green, and the links match the rest of the notice, as in the screenshot above.

Keep transparency for decoration like this. See-through text lets the background show through, and that makes it harder to read.

Want to practice reading and writing color formats?

Your events.css now gives the notice the garden’s colors: the honeydew background from the first lesson, very dark green text, the garden’s green for the main heading and the links, and a darker green for the job headings. Those greens look fine to you, but you haven’t yet checked whether everyone can read them. In the next lesson, Check Text Contrast in DevTools, you’ll measure exactly that.

Become a Member to join the conversation.