Coder Social home page Coder Social logo

postcss-rem-to-pixel's Introduction

postcss-rem-to-pixel NPM version

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

Install

$ npm install postcss-rem-to-pixel --save-dev

Usage

Sometimes you need to include a third-party css file that uses rems. Great pracitice! Unless you can't afford to change your body font-size just for some vendor. This script converts every rem value to a px value from the properties you choose using a default font size of 16px.

Input/Output

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

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

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

Example

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

fs.writeFile('main-px.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,
    minRemValue: 0
}
  • rootValue (Number) The root element font size.
  • unitPrecision (Number) The decimal precision px units are allowed to use, floored (rounding down on half).
  • propList (Array) The properties that can change from rem to px.
    • 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 rem.
    • 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 rem to be converted in media queries.
  • minRemValue (Number) Set the minimum rem value to replace.

Use with gulp-postcss and autoprefixer

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var remToPx = require('postcss-rem-to-pixel');

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

    var processors = [
        autoprefixer({
            browsers: 'last 1 version'
        }),
        remToPx({
            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 rem unit declaration.

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

// `Rem` or `REM` is ignored by `postcss-rem-to-pixel` but still accepted by browsers
.ignore {
    border: 1Rem solid; // ignored
    border-width: 2REM; // ignored
}

postcss-rem-to-pixel's People

Contributors

cuth avatar jesstech avatar kud avatar spacedawwwg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

postcss-rem-to-pixel's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.