A CSS post-processor that supports multiple rules to converts px to rem based on postcss-pxtorem.
npm install postcss-pxtorem-multiThis is a fork based on postcss-pxtorem. The difference is postcss-pxtorem-multi supports multiple rules.
``shell`
$ npm install postcss-pxtorem-multi --save-dev
Type: Object | Null `
Default:js`
{
include: null,
exclude: null,
rootValue: 16,
unitPrecision: 5,
propList: ['font', 'font-size', 'line-height', 'letter-spacing'],
selectorBlackList: [],
replace: true,
mediaQuery: false,
minPixelValue: 0,
rules: []
}include
- (String|RegExp|Arrayexclude
- (String|RegExp|ArrayrootValue
- (Number) The root element font size.unitPrecision
- (Number) The decimal numbers to allow the REM units to grow to.propList
- (Array) The properties that can change from px to rem.
- Values need to be exact matches.
- Use wildcard to enable all properties. Example: ['']
- Use at the start or end of a word. (['position*'] will match background-position-y)!
- Use to not match a property. Example: ['*', '!letter-spacing']['', '!font']
- Combine the "not" prefix with the other prefixes. Example: selectorBlackList
- (Array) The selectors to ignore and leave as px.['body']
- If value is string, it checks to see if selector contains the string.
- will match .body-class[/^body$/]
- If value is regexp, it checks to see if the selector matches the regexp.
- will match body but not .bodyreplace
- (Boolean) replaces rules containing rems instead of adding fallbacks.mediaQuery
- (Boolean) Allow px to be converted in media queries.minPixelValue
- (Number) Set the minimum pixel value to replace.rules
- : (Object) Supporting all the above parameters.
`js
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var pxtorem = require('postcss-pxtorem-multi');
gulp.task('css', function () {
var processors = [
pxtorem({
rootValue: 16,
rules: [
{
include: 'common.css',
rootValue: 32,
}
]
})
];
return gulp.src(['build/css/*/.css'])
.pipe(postcss(processors))
.pipe(gulp.dest('build/css'));
});
`
or .postcssrc.js
`js
module.exports = {
plugins: {
"postcss-pxtorem-multi": {
rootValue: 75,
propList: ['*'],
rules: [
{
include: ['/node_modules/vant/'],
rootValue: 37.5,
propList: ['*'],
}
]
}
}
}
`$3
Currently, the easiest way to have a single property ignored is to use a capital in the pixel unit declaration.`css
// px is converted to rem
.convert {
font-size: 16px; // converted to 1rem
}//
Px or PX is ignored by postcss-pxtorem-multi but still accepted by browsers
.ignore {
border: 1Px solid; // ignored
border-width: 2PX; // ignored
}
``