Most cell and cross-platform net builders have encountered the issue in some unspecified time in the future: you want your net app to scale neatly to the machine display screen measurement, no matter which of the various 1000’s of units there are on the market, however your items solely have one design structure width, primarily mobile. Or maybe it’s essential to create a per-device stylesheet for a really scalable cross-platform UI, and wish to use a scaled model of an unique to base it upon. In both case, changing all the things by hand is laborious and inconvenient.

Grappling with this downside in one in all our tasks, the app, our main frontend developer Dmitriy Karpunin got here up with the concept of utilizing a post-processor to transform CSS values from one set of items to a different, and that’s how postcss-px-to-viewport, a plug-in for PostCSS that converts px values to viewport items (vw, vh, and so on.), was born.

For instance, in the event you specify a notional horizontal decision of 640 pixels and also you specify a horizontal distance of 32px, the plug-in will convert that to 5vw within the output, as a result of 32 pixels is 5% of the 640 pixel viewport width.

The plug-in additionally has loads of choices for customisation, to regulate precisely which values to transform, and the place, and primarily based upon which notional decision.

Set up

Set up is simple with npm:

$ npm set up postcss-px-to-viewport --save-dev

or with yarn, in the event you want:

$ yarn add -D postcss-px-to-viewport


To configure the plug-in, add a ‘postcss-px-to-viewport‘ object to your postcss.config.js as proven under:

module.exports = {
  plugins: {
    'postcss-px-to-viewport': {
       unitToConvert: 'px',
       viewportWidth: 320,
       unitPrecision: 5,
       propList: ['*'],
       viewportUnit: 'vw',
       fontViewportUnit: 'vw',
       selectorBlackList: [],
       minPixelValue: 1,
       mediaQuery: false,
       change: true,
       exclude: [],
       panorama: false,
       landscapeUnit: 'vw',
       landscapeWidth: 568

The choices listed above are proven with their default values, and any that you do not want to override may be omitted.

To make use of postcss-px-to-viewport with gulp-postcss, add a job to your gulpfile.js alongside the strains of the under:

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var pxtoviewport = require('postcss-px-to-viewport');
gulp.job('css', operate () {
    var processors = [
            viewportWidth: 320,
            viewportUnit: 'vmin'
    return gulp.src(['build/css/**/*.css'])

Extensively used throughout Asia

At some point in Summer time 2018, we found an energetic neighborhood had sprung up across the plug-in in China — instantly, the problems, pull requests, and forks got here flooding in.

A lot of Asia, however China, particularly, suffers from an energetic proliferation of units that builders are tasked with supporting, and postcss-px-to-viewport rapidly turned invaluable to many!

We rapidly realised that it will be useful if the plug-in got here with directions in Chinese language, not simply English, and we had been thrilled when a neighborhood volunteer answered our name and contributed a Chinese language-language Readme for the undertaking.


postcss-px-to-viewport is a superb instance of a utility that does one factor and does it properly, and we encourage others to open related instruments they could have written. And, in the event you encounter any downside within the improvement of your undertaking and can’t discover a ready-made resolution, we’re right here that can assist you develop a plugin or perhaps a software program that can clear up your downside and probably assist 1000’s of different builders.

Source link


Please enter your comment!
Please enter your name here