postcss-plugin-px2units
A plugin for PostCSS that generates viewport units (vw/wh/rem) from pixel units.
Features
Use the Flex layout, Grid layout, viewport units rem ,vw and wh to adapt to multi-screen problem
Installation
$ npm i --save postcss-plugin-px2units
Usage
input and output
//option
{
viewportWidth: 3840, // 3840
viewportHeight: 2304, // 2304
}
////1/* 100px*100/1920 => vw */ ///* 100/1920 */
original
;;; const css = ;const options = replace: false;const processedCss = css; ;
with gulp
var gulp = ;var postcss = ;var px2units = ;gulp;
with webpack
;const px2unitsOpts = ......; module: loaders: test: /\.css$/ loader: 'style-loader!css-loader!postcss-loader' postcss:
atool-build
withwebpack.connfig.js
;; const px2unitsOpts = ...... ; webpackConfigpostcss; return webpackConfig;;
Configuration
Default:
unitToConvert: rem:"rx"vw:"wx"vh:"hx" viewportWidth: 1920 viewportHeight: 1080 rootValue:100 viewportwidthUnit:'vw' viewportheightUnit: 'vh' viewportfontUnit: 'rem' unitPrecision: 5 minPixelValue: 1 exclude:false mediaQuery: false replace:true
unitToConvert
(Object) unit lits to convert, by default, it is {rem:"rx",vw:"wx",vh:"hx"}viewportWidth
(Number) The width of the viewport.Default is 19.20. -viewportWidth = the width of UI designviewportHeight
(Number) The height of the viewport.Default is 10.80.-viewportHeight = the height of UI designrootValue
(Number) The root element font size. Default is 100. {html:100wx=100px*100/1920vw}viewportwidthUnit
(Number) a way to exclude some folder,eg. /(node_module)/.viewportheightUnit
(Number) Expected height units.unitPrecision
(Boolean/String) The decimal numbers to allow the REM or VW OR VH units to grow to.minPixelValue
(Number) Set the minimum pixel value to replace.exclude
(Boolean|Reg) a way to exclude some folder,eg. /(node_module)/.mediaQuery
(Boolean) Allow px to be converted in media queries.replace
(Boolean) replaces rules containing rems instead of adding fallbacks.
License
Thanks postcss-plugin-pxtorem and postcss-px-to-viewport