Generating fluid CSS typography clamps from static Figma variables
I opened a pull request on Thursday and saw 42 lines of fresh CSS. It was entirely media queries. A developer was trying to make our heading elements scale down gracefully for mobile screens. They had hardcoded a pixel value for desktop. Then they added another block for tablet. Then another block for mobile. Multiply that by six heading levels and it gets out of hand quickly. We needed a better…
A developer submitted a pull request on Thursday, adding 42 lines of media queries to handle responsive typography. The issue was that heading elements were hardcoded to specific pixel values for desktop screens, leading to a tedious process of creating breakpoints for each heading level. The solution lies in using the CSS clamp() function, which allows the browser to calculate font sizes dynamically based on viewport width.
This approach eliminates the need for manual breakpoint creation. To implement this, designers provide static numbers for font sizes at mobile and desktop breakpoints. The developer must then convert these numbers into a format that can be easily manipulated in code. The process involves exporting typography variables from Figma, organizing them in a JSON file with minimum and maximum values, and then using a Node script to generate a CSS file with fluid typography rules.
The script reads the JSON file, determines viewport boundaries, converts pixel values to rem units for scalability, calculates the slope for font size adjustment, and finally outputs a CSS file containing the fluid typography rules.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to