A stylus plugin to convert px to relative length units (vw / rem)
I maintain a Postcss Plugin for more common usage. Please visit postcss-px-to-relative-unit
npm install stylus-px-to-relative-unit
// webpack.config.js
const pxToUnit = require('stylus-px-to-relative-unit')
...
{
loader: 'stylus-loader',
options: {
use: [pxToUnit({
// Options
})],
}
},
...
pxToUnit({
targetUnit: 'vw',
ignoreThreshold: 1,
viewportWidth: 375,
viewportHeight: 667,
htmlFontSize: 37.5
})
Options | Default | Description |
---|---|---|
targetUnit | 'vw' | target relative length unit. Support 'vw', 'rem' and 'vw&rem' |
ignoreThreshold | 1 | px values less than this threshold won't be converted |
viewportWidth | 375 | base viewport width (for targetUnit: 'vw' ) |
viewportHeight | 667 | base viewport height (for targetUnit: 'vw', currently useless) |
htmlFontSize | 37.5 | base html font-size (for targetUnit: 'rem') |
needConvert | true |
If you want to use unit vw and also worry about browser support, you can use 'vw&rem' mode. For example:
// Input
.test
border 3.75px solid #fff
// Output
.test {
border: 0.1rem solid #fff;
border: 1vw solid #fff;
}
For browser doesn't support vw, it will automatically use rem to layout.
Notice: If you need to limit max/min width of the layout, this mode is not suit for you
For special files don't want to be converted, add 'need-convert = false' on top of each stylus files.
// Input
need-convert = false
.test
width 3.75px
// Output
.test {
width: 3.75px;
}
This plugin will not convert px arguments in css functions like 'translate()', but provides some global stylus functions for users to convert px value manually.
// Input
.test
transform translate(px2vw(3.75px), px2vw(3.75px))
// Output
.test {
transform: translate(1vw, 1vw);
}
// Input
.test
transform translate(px2rem(3.75px), px2rem(3.75px))
// Output
.test {
transform: translate(0.1rem, 0.1rem);
}
For arguments don't want to be converted, wrap them with quote.
// Input
.test
padding '3.75px' 3.75px
// Output
.test {
padding: 3.75px 1vw;
}
- Add 'needConvert' option
- Optimize output in 'vw&rem' mode
- Support option targetUnit 'vw&rem'
Inspired by jaywcjlove/stylus-px2rem
This project is licensed under the MIT License - see the LICENSE file for details