Gathering detailed insights and metrics for postcss-better-units
Gathering detailed insights and metrics for postcss-better-units
Gathering detailed insights and metrics for postcss-better-units
Gathering detailed insights and metrics for postcss-better-units
A PostCSS plugin to define, transform, and manage custom or existing CSS units with ease.
npm install postcss-better-units
Typescript
Module System
Min. Node Version
Node Version
NPM Version
68
Supply Chain
98.7
Quality
77.1
Maintenance
100
Vulnerability
100
License
TypeScript (95.5%)
JavaScript (4.5%)
Total Downloads
189
Last Day
1
Last Week
6
Last Month
29
Last Year
189
MIT License
1 Stars
6 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Mar 27, 2025
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
postcss-better-units@1.0.1
Unpacked Size
13.53 kB
Size
4.28 kB
File Count
8
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 05, 2025
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-14.3%
6
Compared to previous week
Last Month
31.8%
29
Compared to previous month
Last Year
0%
189
Compared to previous year
1
1
16
A PostCSS plugin to define, transform, and manage custom or existing CSS units with ease.
Install package:
1pnpm add -D postcss-better-units
Note: You also need the postcss
package installed.
Configure PostCSS
to use plugin. For example with postcss.congig.ts
file:
1import type { Config } from 'postcss-load-config' 2import betterUnits from 'postcss-better-units' 3 4export default { 5 plugins: [ 6 betterUnits([{ 7 fromUnit: 'rpx', 8 transform: value => `${value / 16}rem`, 9 }]), 10 ], 11} satisfies Config
That's it! The configuration above will transform rpx
units to rem
in a 1/16 ratio:
1div { 2 width: 16rpx; 3 height: 5rpx; 4} 5/* becomes */ 6div { 7 width: 1rem; 8 height: 0.3125rpx; 9}
The plugin accepts an array of objects. Each object describes a unit transformation:
Property | Type | Description |
---|---|---|
fromUnit * | string | Unit to transform from. |
transform * | (number) => string | Transformer function. |
toUnit | string | Unit to transform into. This optional shorthand may be used instead of transform . |
toCssVar | --${string} | CSS variable to transform into. This optional shorthand may be used instead of transform |
preserve | boolean , 'before' , 'after' | Whether the original value should be preserved. true means 'after' . |
exclude | RegExp , (string, Declaration) => boolean | RegExp or function to exclude declarations from transfomation. |
Note: One (and only one) of transform
, toUnit
or toCssVar
should be defined.
1{ toUnit: '<TO_UNIT>' } 2// equivalent: 3{ transform: value => `${value}<TO_UNIT>` }
1{ toCssVar: '<TO_CSS_VAR>' } 2// equivalent: 3{ transform: value => `calc(${value} * var(<TO_CSS_VAR>))` }
Here are some simple examples to board you in. You also may check our tests to find more insights.
Imagine you want to use these new awesome units like dvh
. But browser support is still limited. In such cases, fallback to less awesome units like vh
may be needed:
1betterUnits([{ 2 fromUnit: 'dvh', 3 toUnit: 'vh', 4 preserve: 'after', 5}])
1main { 2 min-height: 100dvh; 3} 4/* becomes */ 5main { 6 min-height: 100vh; 7 min-height: 100dvh; 8}
Let's take the example above further. In some cases, a fallback isn't sufficient, and you might use a JavaScript polyfill to make these dvh
units work everywhere. Typically, polyfills set a CSS variable that can be used as the desired unit:
1betterUnits([{ 2 fromUnit: 'dvh', 3 toCssVar: '--dvh', 4 preserve: 'after', 5}])
1main { 2 min-height: 100dvh; 3} 4/* becomes */ 5main { 6 min-height: calc(100 * var(--dvh)); 7 min-height: 100dvh; 8}
If you are interested in such polyfill, check out this project for inspiration.
There is CSS draft for a syntax sugar called variable units
. You can use it right now:
1betterUnits([{ 2 fromUnit: '--fem', 3 toCssVar: '--fem', 4}])
1.fluid-type { 2 font-size: 1.2--fem; 3} 4/* becomes */ 5.fluid-type { 6 min-height: calc(1.2 * var(--fem)); 7}
Feel free to request features or report bugs, even if project seems abandoned. Feedback is always appreciated.
No vulnerabilities found.
No security vulnerabilities found.