DEV Community

Jane Ori
Jane Ori

Posted on

6

CSS-Only Type Grinding: Casting Tokens (sm|md|etc) into Useful CSS Values

Ready for brand new CSS magic?

Gif of a person wiggling their fingers, looking right at the camera, and saying the word

If you're writing a sharable component or library and want to provide your dev users an API that has CSS options like:

--size: sm; | --size: md; | --size: lg;

it's actually possible to do in CSS, without involving JS, a build system, or Style/Container Queries thanks to a new technique I've named "Type Grinding."

Type Grinding allows your design tokens (keywords, or โ€œ<custom-ident>โ€ values) written in your CSS to be transformed into any other values - like width, padding, color, etc - without relying on anything outside of CSS.

gif of css token values changing other properties without js

The current global user support is 72.77% according to caniuse - specifically chromium based browsers.


How does it work?!

The company I work for, Bitovi, is super supportive and allowed me to drop what I was working on to write an article about this instead! So over on the company blog, I'll be destructuring the CSS involved here to teach how it works, bring awareness to what's possible, and hopefully empower you to do it yourself!

Please take a look - I would LOVE to see what you do with it!

Screenshot of the Bitovi Article's Header

Happy to answer questions below, on Twitter, or in the comments at the full blog post!

Thank you! ๐Ÿ‘ฝ๐Ÿ’œ

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools canโ€™t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video