Responsive CSS unit without the use of media queries.
⚠️ Usage of Dart Sass is mandatory.
@use "sass-unit-graph" as *;
:root {
--font-size-base: #{unit-graph(
(320px,16px),
(1920px,24px)
)};
}
body {
// font size of 16px at viewport width of 320px
// font size of 18.24px at viewport width of 768px
// font size of 24px at viewport width of 1920px
font-size: var(--font-size-base);
}
Examples available on the GitHub Page.
npm i -D sass sass-unit-graph
@use "sass-unit-graph" as *;
All browsers with support for CSS calc(), min(), max() and vw are supported, which includes:
- Edge (79+)
- Chrome (79+)
- Firefox (75+)
- Safari (11.1+)
- Android Browser (92+)
The unit-graph()
Sass function returns an expression based
on at least two points.
It is possible to use any CSS calc operations.
@use "sass-unit-graph/unit" as unit;
:root {
--custom-prop: #{unit-graph(
(320px,16px),
(1920px,24px)
)};
}
// Multiplication
font-size: calc(var(--custom-prop) * 2);
// Division
font-size: calc(var(--custom-prop) / 2);
// Addition
font-size: calc(var(--custom-prop) + 10%);
// Subtraction
font-size: calc(var(--custom-prop) - 10%);
It is also possible to add or subtract multiple graph expressions:
@use "sass-unit-graph/unit" as unit;
:root {
--custom-prop-1: #{unit-graph(
(320px,16px),
(1920px,24px)
)};
--custom-prop-2: #{unit-graph(
(320px,8px),
(1920px,12px)
)};
}
body {
font-size: calc(var(--custom-prop-1) + var(--custom-prop-2));
}
Extrapolation will appen below the first points X value, and above the last point X value.
unit-graph(
// Extrapolation
(320px,16px),
// Value is linear between two points
(1920px,24px)
// Extrapolation
)
It is possible to draw an horizontal line to get a constant Y value.
unit-graph(
// Extrapolation
(0px, 16px),
// Fixed value of 16px until 320px
(320px, 16px),
// Value is linear between two points
(1920px, 24px),
// Fixed value of 24px above 1920px
(2000px, 24px)
// Extrapolation
)
Creation of steps is possible by creating sharp line.
unit-graph(
// Extrapolation
(0px, 16px),
// Fixed value of 16px until ~768px
(767.99px, 16px),
// Very sharp ascending line, user won't notice
(768px, 24px),
// Fixed value of 24px above 768px
(1920px, 24px)
// Extrapolation
)
In case of repeatition, usage of CSS custom properties is strongly
encouraged since complexe expressions might be quite long.
Although very good, the precision of float numbers will be limited by Dart Sass compiler, which affects the precision of the generated expressions.
unit-graph()
accepts all absolute CSS units (cm, mm, in, px, pt, pc),
although the output will always be in pixels.
This is made possible by Sass's support of real-world unit calculations, as detailed in the Sass documentation of numeric units.
Configure Dart Sass includePaths
option to resolve to your node_modules
folder.
Based on my previous work sass-linear-expression, which is own by Sigmund.
Special thanks to my front-end collegues who inspired me to create better tooling.