All Projects → cuth → Postcss Pxtorem

cuth / Postcss Pxtorem

Licence: mit
Convert pixel units to rem (root em) units using PostCSS

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to Postcss Pxtorem

Postcss Px To Viewport
A plugin for PostCSS that generates viewport units (vw, vh, vmin, vmax) from pixel units. The best choice to create a scalable interface on different displays by one design size.
Stars: ✭ 1,997 (+25.44%)
Mutual labels:  postcss, rem-units, pixel-units
Gridsome Plugin Tailwindcss
A Tailwind plugin for Gridsome.
Stars: ✭ 100 (-93.72%)
Mutual labels:  postcss
Postcss Modules Example
How to use postcss-modules plugin
Stars: ✭ 78 (-95.1%)
Mutual labels:  postcss
React Boilerplate
This project is deprecated. Please use CRA instead.
Stars: ✭ 88 (-94.47%)
Mutual labels:  postcss
Cessie
Transpile your CSS bundle to support CSS variables, calc, and future CSS for legacy browsers.
Stars: ✭ 81 (-94.91%)
Mutual labels:  postcss
Postcss Font Family System Ui
PostCSS plugin to transform W3C CSS font-family: system-ui to a practical font-family list
Stars: ✭ 91 (-94.28%)
Mutual labels:  postcss
Postcss Prefix Selector
Prefix all CSS rules with a selector
Stars: ✭ 75 (-95.29%)
Mutual labels:  postcss
Css In React
🍭 CSS in React - Learn the best CSS in JS frameworks by example
Stars: ✭ 101 (-93.66%)
Mutual labels:  postcss
Ember Cli Postcss
🔥 A Postcss integration for ember-cli
Stars: ✭ 97 (-93.91%)
Mutual labels:  postcss
Postcss Foft Classes
A postcss plugin to automatically add classes for various font loading strategies.
Stars: ✭ 87 (-94.54%)
Mutual labels:  postcss
Postcss Grid Kiss
A PostCSS plugin to keep CSS grids stupidly simple
Stars: ✭ 1,270 (-20.23%)
Mutual labels:  postcss
React Base
atSistemas React/Redux Isomorphic Platform
Stars: ✭ 82 (-94.85%)
Mutual labels:  postcss
Postcss Less
PostCSS Syntax for parsing LESS
Stars: ✭ 93 (-94.16%)
Mutual labels:  postcss
Postcss Elm Tailwind
put some tailwind in your elm
Stars: ✭ 80 (-94.97%)
Mutual labels:  postcss
Rtlcss
Framework for transforming Cascading Style Sheets (CSS) from Left-To-Right (LTR) to Right-To-Left (RTL)
Stars: ✭ 1,363 (-14.38%)
Mutual labels:  postcss
Postcss Sass
A Sass parser for PostCSS, using gonzales-pe. https://www.npmjs.com/package/postcss-sass
Stars: ✭ 76 (-95.23%)
Mutual labels:  postcss
Minna Ui
😸 A fast, friendly, and fun web UI kit for everyone.
Stars: ✭ 86 (-94.6%)
Mutual labels:  postcss
Uncss
Remove unused styles from CSS
Stars: ✭ 9,170 (+476.01%)
Mutual labels:  postcss
Webpack4.x
webpack4.x详细配置步骤
Stars: ✭ 103 (-93.53%)
Mutual labels:  postcss
Wordpressify
🎈 A build system designed to automate your WordPress development workflow.
Stars: ✭ 1,374 (-13.69%)
Mutual labels:  postcss

postcss-pxtorem NPM version

A plugin for PostCSS that generates rem units from pixel units.

Install

$ npm install postcss postcss-pxtorem --save-dev

Usage

Pixels are the easiest unit to use (opinion). The only issue with them is that they don't let browsers change the default font size of 16. This script converts every px value to a rem from the properties you choose to allow the browser to set the font size.

Input/Output

With the default settings, only font related properties are targeted.

// input
h1 {
    margin: 0 0 20px;
    font-size: 32px;
    line-height: 1.2;
    letter-spacing: 1px;
}

// output
h1 {
    margin: 0 0 20px;
    font-size: 2rem;
    line-height: 1.2;
    letter-spacing: 0.0625rem;
}

Example

var fs = require('fs');
var postcss = require('postcss');
var pxtorem = require('postcss-pxtorem');
var css = fs.readFileSync('main.css', 'utf8');
var options = {
    replace: false
};
var processedCss = postcss(pxtorem(options)).process(css).css;

fs.writeFile('main-rem.css', processedCss, function (err) {
  if (err) {
    throw err;
  }
  console.log('Rem file written.');
});

options

Type: Object | Null
Default:

{
    rootValue: 16,
    unitPrecision: 5,
    propList: ['font', 'font-size', 'line-height', 'letter-spacing'],
    selectorBlackList: [],
    replace: true,
    mediaQuery: false,
    minPixelValue: 0,
    exclude: /node_modules/i
}
  • rootValue (Number | Function) Represents the root element font size or returns the root element font size based on the input parameter
  • 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']
    • Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']
  • selectorBlackList (Array) The selectors to ignore and leave as px.
    • If value is string, it checks to see if selector contains the string.
      • ['body'] will match .body-class
    • If value is regexp, it checks to see if the selector matches the regexp.
      • [/^body$/] will match body but not .body
  • replace (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.
  • exclude (String, Regexp, Function) The file path to ignore and leave as px.
    • If value is string, it checks to see if file path contains the string.
      • 'exclude' will match \project\postcss-pxtorem\exclude\path
    • If value is regexp, it checks to see if file path matches the regexp.
      • /exclude/i will match \project\postcss-pxtorem\exclude\path
    • If value is function, you can use exclude function to return a true and the file will be ignored.
      • the callback will pass the file path as a parameter, it should returns a Boolean result.
      • function (file) { return file.indexOf('exclude') !== -1; }

Use with gulp-postcss and autoprefixer

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var pxtorem = require('postcss-pxtorem');

gulp.task('css', function () {

    var processors = [
        autoprefixer({
            browsers: 'last 1 version'
        }),
        pxtorem({
            replace: false
        })
    ];

    return gulp.src(['build/css/**/*.css'])
        .pipe(postcss(processors))
        .pipe(gulp.dest('build/css'));
});

A message about ignoring properties

Currently, the easiest way to have a single property ignored is to use a capital in the pixel unit declaration.

// `px` is converted to `rem`
.convert {
    font-size: 16px; // converted to 1rem
}

// `Px` or `PX` is ignored by `postcss-pxtorem` but still accepted by browsers
.ignore {
    border: 1Px solid; // ignored
    border-width: 2PX; // ignored
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].