New in CSS: relative colors (#note)

Do you remember the good old Sass days? Using Sass, you could manipulate colors with helper functions such as darken or lighten. This wasn’t possible in pure CSS, but now it’s on its way!
Jim Nielsen’s post Dynamic Color Manipulatio…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Do you remember the good old Sass days? Using Sass, you could manipulate colors with helper functions such as darken or lighten. This wasn't possible in pure CSS, but now it's on its way!

Jim Nielsen's post Dynamic Color Manipulation with CSS Relative Colors explains how the new relative color syntax enables flexible color manipulations. Relative CSS colors are super new and only available in Safari Tech Preview (and even in Tech Preview, you have to toggle a development flag).

So be warned, the following is future music!

The new relative color syntax (Relative color specification) comes with a new from keyword.

.something {
  --color: #4488dd;
  /* transform a color value into `rgb` */
  background-color: rgb(from var(--color) r g b / .5);
}

Using from you can "destruct" color values into their rbg, hsl or lch components. And the best thing: from works for color definitions such as green, #445599 or rgb(100 200 0). You can soon transform hex values into rgb and hsl and back!

But how could you implement a darken function then? Pair things with calc. 🤯

.something {
  --color: red;
  /* transform `red` to `hsl` and manipulate its lightness */
  background: hsl(from var(--color) h s calc(l - 20%));
}

Transform a color value into a format that's easy to manipulate and call it a day!

I'm amazed by this CSS addition, and it shows that smart people write the specs. Developers asked for lighten, darken and various other CSS functions in the past, and we now got a solution that can do this but much more. Beautiful!👏

Head over to Jim's blog to learn more. It's a good one!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2021-11-22T23:00:00+00:00) New in CSS: relative colors (#note). Retrieved from https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/

MLA
" » New in CSS: relative colors (#note)." Stefan Judis | Sciencx - Monday November 22, 2021, https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/
HARVARD
Stefan Judis | Sciencx Monday November 22, 2021 » New in CSS: relative colors (#note)., viewed ,<https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/>
VANCOUVER
Stefan Judis | Sciencx - » New in CSS: relative colors (#note). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/
CHICAGO
" » New in CSS: relative colors (#note)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/
IEEE
" » New in CSS: relative colors (#note)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/. [Accessed: ]
rf:citation
» New in CSS: relative colors (#note) | Stefan Judis | Sciencx | https://www.scien.cx/2021/11/22/new-in-css-relative-colors-note/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.