OKLCH()

I was at the State Of The Browser event recently, which was great as always.

Manu gave a great talk about colour in CSS. A lot of it focused on OKLCH. I was already convinced of the benefits of this colour space after seeing a terrific talk by Anton Lovchikov a while back.

After Manu’s talk, someone mentioned that even though OKLCH is well supported in browsers now, it’s a shame that it isn’t (yet) in design tools like Figma. So designers are still handing over mock-ups with hex values.

I get the frustration, but in my experience it’s not that big a deal in practice. Here’s why: oklch() isn’t just a way of defining colours with lightness, chroma, and hue in CSS. It’s also a function. You can use the magical from keyword in this function to convert hex colours to l, c, and h:

--page-colour: oklch(from #49498D l c h);

So even if you’re being handed hex colour values, you can still use OKLCH in your CSS. Once you’re doing that, you can use all of the good stuff that comes with having those three values separated out—something that was theoretically possible with hsl, but problematic in practice.

Let’s say you want to encode something into your CSS like this: “if the user has specified that they prefer higher contrast, the background colour should be four times darker.”

@media (prefers-contrast: more) {
  --page-colour: oklch(from #49498D calc(l / 4) c h);
}

It’s really handy that you can use calc() within oklch()—functions within functions. And I haven’t even touched on the color-mix() function.

Hmmm …y’know, this is starting to sound an awful lot like functional programming:

Functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values.

Have you published a response to this? :

Responses

Marc Görtz

Nicht abgesprochen, aber wie bei Markus gibt es auch hier ein kleines Facelift:

Ich bin jetzt auf die Farbdefinition via oklch() umgestiegen. Das stand schon länger auf meiner Liste – in anderen Projekten hatte ich es sogar schon umgesetzt – aber Jeremy Keiths Beitrag hat mich dann doch dazu gebracht, auch hier endlich Hand anzulegen. Parallel dazu habe ich ein wenig am Kontrast geschraubt, wobei da aber noch Luft nach oben ist.

Apropos frische Ideen: Bei WebKit wurde kürzlich über Line Height Units geschrieben, und da konnte ich auch nicht widerstehen. Jetzt sind lh und rlh an vielen Stellen im Einsatz. Ein bisschen Feintuning steht hier noch an, aber der Grundstein ist gelegt.

Auch die Schrift habe ich ausgetauscht. Vorher war’s Inter – solide, aber auf Dauer doch etwas beliebig. Jetzt läuft hier Rubik, ebenfalls eine variable Schrift, aber deutlich kompakter mit fast 50% weniger Dateigröße. Nicht schlecht, oder?

Und dann wäre da noch ein kleiner Fünfzeiler, den ich ebenfalls bei Jeremy aufgeschnappt habe:

@media (prefers-reduced-motion: no-preference) { @view-transition { navigation: auto; } } 

Mein Einstieg in die View Transition API – mal sehen, wie sich das in den nächsten Wochen so anfühlt.

4 Shares

# Shared by Zach Leatherman :11ty: on Wednesday, April 16th, 2025 at 3:30pm

# Shared by Will Browar 👨🏻‍💻 on Wednesday, April 16th, 2025 at 3:31pm

# Shared by Mark Pospesel :kt_zg: on Wednesday, April 16th, 2025 at 3:58pm

# Shared by Josh Tumath on Wednesday, April 16th, 2025 at 11:23pm

10 Likes

# Liked by Daniel on Wednesday, April 16th, 2025 at 3:30pm

# Liked by Zach Leatherman :11ty: on Wednesday, April 16th, 2025 at 3:30pm

# Liked by Will Browar 👨🏻‍💻 on Wednesday, April 16th, 2025 at 3:30pm

# Liked by Geoff Holden on Wednesday, April 16th, 2025 at 3:31pm

# Liked by Jordi Sánchez on Wednesday, April 16th, 2025 at 5:33pm

# Liked by Johnny Taylor on Wednesday, April 16th, 2025 at 6:01pm

# Liked by strongest on Thursday, April 17th, 2025 at 12:41am

# Liked by Frank // Mottokrosh on Thursday, April 17th, 2025 at 8:54am

# Liked by Nick F on Thursday, April 17th, 2025 at 1:04pm

# Liked by hooleyhoop on Thursday, April 17th, 2025 at 1:04pm

Related posts

Making the Patterns Day website

The joy of getting hands-on with HTML and CSS.

Assumption

Separate your concerns.

SVGs in dark mode

Styling sheet music …and then unstyling it.

Dark mode revisited

Adding another theme to my stylesheet switcher.

Programming CSS to perform Sass colour functions

Combining custom properties, hsl(), and calc() to get cascading button styles.

Related links

Don’t judge a book by its cover

Some neat CSS from Tess that’s a great example of progressive enhancement; these book covers look good in all browsers, but they look even better in some.

Tagged with

Link colors and the rule of tincture

When you think of heraldry what comes to mind is probably knights in shining armor, damsels in distress, jousting, that sort of thing. Medieval stuff. But I prefer to think of it as one of the earliest design systems.

This totally checks out.

Tagged with

Modern CSS in Real Life - Chris Coyier

This is a terrrific presentation by Chris, going through some practical implementations of modern CSS: logical properties, viewport units, grid, subgrid, container queries, cascade layers, new colour spaces, and view transitions.

Tagged with

Building Dark Mode | Product Blog • Sentry

Robin makes a good point here about using dark mode thinking as a way to uncover any assumptions you might have unwittingly baked into your design:

Given its recent popularity, you might believe dark mode is a fad. But from a design perspective, dark mode is exceptionally useful. That’s because a big part of design is about building relationships between colors. And so implementing dark mode essentially forced everyone on the team to think long, hard, and consistently about our front-end design components. In short, dark mode helped our design system not only look good, but make sense.

So even if you don’t actually implement dark mode, acting as though it’s there will give you a solid base to build in.

I did something similar with the back end of Huffduffer and The Session—from day one, I built them as though the interface would be available in multiple languages. I never implemented multi-language support, but just the awareness of it saved me from baking in any shortcuts or assumptions, and enforced a good model/view/controller separation.

For most front-end codebases, the design of your color system shows you where your radioactive styles are. It shows you how things are tied together, and what depends on what.

Tagged with

CSS Stats

A handy tool for getting an overview of your site’s CSS:

CSS Stats provides analytics and visualizations for your stylesheets. This information can be used to improve consistency in your design, track performance of your app, and diagnose complex areas before it snowballs out of control.

Tagged with

Previously on this day

2 years ago I wrote Pickin’ dates on iOS

Mobile Safari doesn’t support the min and max attributes on date inputs.

6 years ago I wrote Podcasts

Four audio outings.

7 years ago I wrote Three more Patterns Day speakers

Three fantastic speakers have been added to the roster of this year’s unmissable one-day event dedicated to design systems, pattern libraries, style guides, and components.

11 years ago I wrote 100 words 025

Day twenty five.

15 years ago I wrote The Kindle connection

For all the faults with its digital books, this little device is proving its worth.

19 years ago I wrote Fake tales of San Francisco

The biggest small town in the West.

20 years ago I wrote Adactio, pour homme

The perfume of the movie of the website… you read it here first.

21 years ago I wrote Transparent liquid

Good design doesn’t draw attention to itself. Really good design is invisible.

22 years ago I wrote Revenge of the DOM

There’s a new article up at A List Apart called Let Them Eat Cake. It’s all about using JavaScript, or more accurately the Document Object Model, to hide and show content on demand.

23 years ago I wrote Hot days, crazy nights

It was a gorgeously hot sunny day today.

24 years ago I wrote Best domain name ever

www.We Made Out in a Tree and This Old Guy Sat and Watched Us.com

24 years ago I wrote Hard times

I’m flattered to be mentioned in the same sentence as Jeffrey Zeldman. Mind you, I am referred to as being "british and poetic", neither of which are quite true.