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 way to handle responsive typography without writing endless breakpoints.
The thing is designers usually give us static numbers. They design a mobile frame with an 18px body font. Then they design a desktop frame with a 20px body font. As developers we have to figure out what happens in between those two sizes.
Instead of stepping through fixed sizes we can use the CSS clamp() function. This lets the browser calculate the font size dynamically based on the viewport width. It needs a minimum size, a preferred scaling rate, and a maximum size. The math looks a bit scary at first. But we can automate it completely using a simple Node script.
The basic token structure
First we need to get our raw numbers out of Figma. You can use a basic export script to pull your typography variables into a JSON file. We want to structure this data so every text style has a minimum and maximum value.
Here is what our raw tokens.json file looks like.
{ "typography": { "heading-1": { "min": 32, "max": 48 }, "heading-2": { "min": 24, "max": 32 }, "body-large": { "min": 18, "max": 20 }, "body-base": { "min": 16, "max": 16 } } } Notice that body-base has the same minimum and maximum value. Our script needs to handle this gracefully. If the values are identical we should just output a static rem value instead of a complex clamp function.
The fluid math explained
Before we write the script we should understand what it actually needs to calculate.
We need to define our viewport boundaries. Let us say our mobile design starts at 320px. Our desktop design stops scaling at 1200px. Anything below 320px will use the minimum font size. Anything above 1200px will use the maximum font size. The space in between is where the fluid scaling happens.
We also need to convert all our pixel values to rem units. This is crucial for accessibility. If a user changes their browser default font size our typography must respect that choice. We assume a standard base of 16px for these calculations.
The trickiest part is calculating the slope. The slope is just the rate at which the font grows between the minimum and maximum viewports. We calculate this by subtracting the minimum font size from the maximum font size. Then we divide that by the difference between the maximum viewport and the minimum viewport.
Writing the transformation script
Now we can write a short Node script to do all this heavy lifting. Create a file called build-typography.js in your project. We will read the JSON file, loop through the typography tokens, and generate a new CSS file.
Here is the complete script.
const fs = require('fs'); // Viewport boundaries in pixels const MIN_VIEWPORT = 320; const MAX_VIEWPORT = 1200; const REM_BASE = 16; function round(value) { return Math.round(value * 1000) / 1000; } function pxToRem(px) { return round(px / REM_BASE); } function generateClamp(minPx, maxPx) { // If sizes are identical we just return a static rem value if (minPx === maxPx) { return `${pxToRem(minPx)}rem`; } const minRem = pxToRem(minPx); const maxRem = pxToRem(maxPx); const slope = (maxPx - minPx) / (MAX_VIEWPORT - MIN_VIEWPORT); const intersection = round(minRem - (slope * pxToRem(MIN_VIEWPORT))); const slopeVw = round(slope * 100); return `clamp(${minRem}rem, ${intersection}rem + ${slopeVw}vw, ${maxRem}rem)`; } // Read the raw tokens const rawData = fs.readFileSync('./tokens.json', 'utf8'); const tokens = JSON.parse(rawData); let cssOutput = ':root {\n'; // Loop through each typography token for (const [name, values] of Object.entries(tokens.typography)) { const clampValue = generateClamp(values.min, values.max); cssOutput += ` --font-${name}: ${clampValue};\n`; } cssOutput += '}\n'; // Write the final CSS file fs.writeFileSync('./typography.css', cssOutput); console.log('Fluid typography generated successfully.'); This script does exactly what we need. It takes our simple pixel values and turns them into mathematically perfect fluid scaling rules. You run it once in your terminal with node build-typography.js and it handles the rest.
The final CSS output
When you run the script you get a clean CSS file. It contains all your typography variables ready to use. You never have to write a typography media query again.
Here is what the generated typography.css file looks like.
:root { --font-heading-1: clamp(2rem, 1.418rem + 1.818vw, 3rem); --font-heading-2: clamp(1.5rem, 1.209rem + 0.909vw, 2rem); --font-body-large: clamp(1.125rem, 1.052rem + 0.227vw, 1.25rem); --font-body-base: 1rem; } Now you can just apply these variables directly to your elements.
h1 { font-size: var(--font-heading-1); line-height: 1.2; } h2 { font-size: var(--font-heading-2); line-height: 1.3; } p { font-size: var(--font-body-base); line-height: 1.5; } The browser does all the work. If you resize the window the h1 element smoothly transitions from 32px on mobile all the way up to 48px on a large monitor. The text never jumps abruptly at specific breakpoints. It just scales beautifully.
This approach saves so much time. You stop arguing about breakpoint specifics in PR reviews. You stop writing custom utility classes just to nudge a font size down on small screens. The math handles it perfectly every single time.
Writing and maintaining custom transformation scripts is honestly a bit tedious. I got tired of updating my local Node scripts every time a designer added a new token type or wanted to tweak the viewport boundaries. So I built a plugin called Design System Sync. It handles all this formatting automatically. It exports your Figma variables directly to GitHub or Bitbucket as ready to use CSS variables including complex math outputs. You can check it out on Netlify or grab it directly from the Figma Community. The free tier gives you five exports a month so you can try it with your own typography scales.
