A plugin for PostCSS that generates viewport units (vw, vh, vmin, vmax) from pixel units

Overview

postcss-px-to-viewport

npm version

English | 中文

Based on the evrone/postcss-px-to-viewport project, refactored using Typescript and PostCss8

Note: Because PostCss 8 is refactored, it is not supported below v8. If you use PostCss versions below v8, please use evrone/postcss-px-to-viewport.

A plugin for PostCSS that generates viewport units (vw, vh, vmin, vmax) from pixel units.

Demo

If your project involves a fixed width, this script will help to convert pixels into viewport units.

Input

.class {
  margin: -10px 0.5vh;
  padding: 5vmin 9.5px 1px;
  border: 3px solid black;
  border-bottom-width: 1px;
  font-size: 14px;
  line-height: 20px;
}

.class2 {
  padding-top: 10px; /* px-to-viewport-ignore */
  /* px-to-viewport-ignore-next */
  padding-bottom: 10px;
  /* Any other comment */
  border: 1px solid black;
  margin-bottom: 1px;
  font-size: 20px;
  line-height: 30px;
}

@media (min-width: 750px) {
  .class3 {
    font-size: 16px;
    line-height: 22px;
  }
}

Output

.class {
  margin: -3.125vw 0.5vh;
  padding: 5vmin 2.96875vw 1px;
  border: 0.9375vw solid black;
  border-bottom-width: 1px;
  font-size: 4.375vw;
  line-height: 6.25vw;
}

.class2 {
  padding-top: 10px;
  padding-bottom: 10px;
  /* Any other comment */
  border: 1px solid black;
  margin-bottom: 1px;
  font-size: 6.25vw;
  line-height: 9.375vw;
}

@media (min-width: 750px) {
  .class3 {
    font-size: 16px;
    line-height: 22px;
  }
}

Getting Started

Installation

Add via npm

$ npm install @jonny1994/postcss-px-to-viewport --save-dev

or yarn

$ yarn add -D @jonny1994/postcss-px-to-viewport

or pnpm

$ pnpm add -D @jonny1994/postcss-px-to-viewport

Usage

Default Options:

{
  unitToConvert: 'px',
  viewportWidth: 320,
  unitPrecision: 5,
  propList: ['*'],
  viewportUnit: 'vw',
  fontViewportUnit: 'vw',
  selectorBlackList: [],
  minPixelValue: 1,
  mediaQuery: false,
  replace: true,
  exclude: undefined,
  include: undefined,
  landscape: false,
  landscapeUnit: 'vw',
  landscapeWidth: 568,
  rules: []
}
  • unitToConvert (String) unit to convert, by default, it is px.
  • viewportWidth (Number) The width of the viewport.
  • unitPrecision (Number) The decimal numbers to allow the vw units to grow to.
  • propList (Array) The properties that can change from px to vw.
    • 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']
  • viewportUnit (String) Expected units.
  • fontViewportUnit (String) Expected units for 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
  • minPixelValue (Number) Set the minimum pixel value to replace.
  • mediaQuery (Boolean or Regexp or Array of Regexp) Allow px to be converted in media queries.
    • If value is regexp, the matches media queries params will be converted.
    • If value is array, the elements of the array are regexp.
  • replace (Boolean) replaces rules containing vw instead of adding fallbacks.
  • exclude (Regexp or Array of Regexp) Ignore some files like 'node_modules'
    • If value is regexp, will ignore the matches files.
    • If value is array, the elements of the array are regexp.
  • include (Regexp or Array of Regexp) If include is set, only matching files will be converted, for example, only files under src/mobile/ (include: /\/src\/mobile\//)
    • If the value is regexp, the matching file will be included, otherwise it will be excluded.
    • If value is array, the elements of the array are regexp.
  • landscape (Boolean) Adds @media (orientation: landscape) with values converted via landscapeWidth.
  • landscapeUnit (String) Expected unit for landscape option
  • landscapeWidth (Number) Viewport width for landscape orientation.
  • rules (Array) Custom path rules.

exclude and include can be set together, and the intersection of the two rules will be taken.

rules option

Customize rule overrides based on paths Example:

module.exports = {
  plugins: {
    // ...
    '@jonny1994/postcss-px-to-viewport': {
      // ...otherOptions
      rules: [
        [
          /\/node_modules\/vant\//, // The regex or string of the path
          (pixels, parsedVal, prop) => {
            if (prop.includes('font')) {
              return parsedval * 2 + 'vmin'
            }
            return parsedval * 2 + 'vw'
          }
        ]
      ]
    }
  }
}

mediaQuery option

Translate only rules filtered by the mediaQuery option. Example:

module.exports = {
  plugins: {
    // ...
    '@jonny1994/postcss-px-to-viewport': {
      // ...otherOptions
      mediaQuery: /min\-width/, // or
      mediaQuery: [/min\-width/, /max\-width/]
    }
  }
}

Ignoring

You can use special comments for ignore conversion of single lines:

  • /* px-to-viewport-ignore-next */ — on a separate line, prevents conversion on the next line.
  • /* px-to-viewport-ignore */ — after the property on the right, prevents conversion on the same line.

Example:

/* example input: */
.class {
  /* px-to-viewport-ignore-next */
  width: 10px;
  padding: 10px;
  height: 10px; /* px-to-viewport-ignore */
  border: solid 2px #000; /* px-to-viewport-ignore */
}

/* example output: */
.class {
  width: 10px;
  padding: 3.125vw;
  height: 10px;
  border: solid 2px #000;
}

There are several more reasons why your pixels may not convert, the following options may affect this: propList, selectorBlackList, minPixelValue, mediaQuery, exclude, include, rules.

Use with PostCss configuration file

add to your postcss.config.js

module.exports = {
  plugins: {
    // ...
    '@jonny1994/postcss-px-to-viewport': {
      // options
    }
  }
}

Use with gulp-postcss

add to your gulpfile.js:

var gulp = require('gulp')
var postcss = require('gulp-postcss')
var pxtoviewport = require('@jonny1994/postcss-px-to-viewport')

gulp.task('css', function () {
  var processors = [
    pxtoviewport({
      viewportWidth: 320,
      viewportUnit: 'vmin'
    })
  ]

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

Running the tests

In order to run tests, you need to install dev-packages:

$ npm install

Then run the tests via npm script:

$ npm run test

Changelog

The changelog is here.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License.

Acknowledgments

You might also like...

Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

Jan 3, 2023

PostCSS plugin to render WordPress global styles from a theme.json file

postcss-wp-global-styles PostCSS plugin to render WordPress global styles from a theme.json file. As of now it only supports preset styles. Usage @wp-

Aug 5, 2022

A PostCSS plugin that transforms `css var` to `less var`.

PostCSS CSS var to Less var A PostCSS plugin to convert CSS variables to Less variables Installation npm install postcss-transform-css-var Examples /

Nov 28, 2022

React Starter Kit — isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

React Starter Kit — isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

React Starter Kit — "isomorphic" web app boilerplate React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Expr

Dec 30, 2022

React Starter Kit — isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

React Starter Kit — isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

React Starter Kit — "isomorphic" web app boilerplate React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Expr

Jan 1, 2023

Converts geojson to svg string given svg viewport size and maps extent.

geojson2svg Converts geojson to svg string given svg viewport size and maps extent. Check world map, SVG scaled map and color coded map examples to de

Dec 17, 2022

A single tab web browser built with puppeteer. Also, no client-side JS. Viewport is streamed with MJPEG. For realz.

A single tab web browser built with puppeteer. Also, no client-side JS. Viewport is streamed with MJPEG. For realz.

:tophat: A single tab web browser built with puppeteer. Also, no client-side JS. Viewport is streamed with MJPEG. For realz.

Dec 23, 2022

This is a starter file with base SvelteKit skeleton configurations with Open Props, Jit Props, and PostCSS configured.

create-svelte Everything you need to build a Svelte project, powered by create-svelte; Creating a project If you're seeing this, you've probably alrea

Jul 22, 2022

🛤 Detection of elements in viewport & smooth scrolling with parallax.

Locomotive Scroll Detection of elements in viewport & smooth scrolling with parallax effects. Installation ⚠️ Scroll-hijacking is a controversial prac

Dec 31, 2022

A starter to show you how to use PostCSS with Remix.

remix-postcss This project demonstrates how to use PostCSS (TailwindCSS) in a Remix project, you can find a tutorial video on Youtube. This project is

Mar 18, 2022

Single Page Application with React, React Router, PostCSS, Webpack, Docker and Docker Compose.

spa-store Single Page Application with React, React Router, PostCSS, Webpack, Docker and Docker Compose. Contributing Feedback Roadmap Releases Check

Jul 4, 2022

A ScrollSpy library for detecting enter/exit of elements in the viewport when the user scrolls

jquery-scrollspy A jQuery plugin for detecting enter/exit of elements in the viewport when the user scrolls. New Features Added a couple new features:

Jul 1, 2022

Lazyload images, iframes or any src* element until they are visible in the viewport.

Lazyload images, iframes or any src* element until they are visible in the viewport.

Nov 15, 2022

Device.js is a JavaScript library to detect device, viewport, and browser information using plain JavaScript.

Device.js Device.js is a JavaScript library to detect device, viewport, and browser information using plain JavaScript. Compatibility Works with all m

Dec 16, 2022

Pixel based heatmap with html5 canvas.

heatcanvas Note that this project is no longer active maintained. Please let me know(file an issue or send me email) if you are interested in taking o

Dec 12, 2022

The smallest, simplest and fastest JavaScript pixel-level image comparison library

The smallest, simplest and fastest JavaScript pixel-level image comparison library

pixelmatch The smallest, simplest and fastest JavaScript pixel-level image comparison library, originally created to compare screenshots in tests. Fea

Jan 8, 2023

A very barebones editor that supports clicking on any pixel in a JPEG image to see the 64 DCT coefficients that make up that 8x8 block

A very barebones editor that supports clicking on any pixel in a JPEG image to see the 64 DCT coefficients that make up that 8x8 block

JPEG Sandbox This is a very barebones editor that supports clicking on any pixel in a JPEG image to see the 64 DCT coefficients that make up that 8x8

Jan 2, 2023

Create amazing pixel art murals. 🐸

Create amazing pixel art murals. 🐸

Pixelate Create amazing pixel art murals with sticky notes. Pixelate is a image editor that shows assembly guides to put art with sticky notes on your

Dec 11, 2022
Comments
  • rules似乎无法使用

    rules似乎无法使用

    我在vite中使用了此插件 并给予rules

     px2viewport({
          //   unitToConvert: 'px',
          viewportWidth: 1920,
          //   unitPrecision: 5,
          //   propList: ['*'],
          //   viewportUnit: 'vw',
          //   fontViewportUnit: 'vw',
          //   selectorBlackList: [],
          //   minPixelValue: 1,
          //   mediaQuery: false,
          //   replace: true,
          //   exclude: undefined,
          //   include: undefined,
          //   landscape: false,
          //   landscapeUnit: 'vw',
          //   landscapeWidth: 568,
          rules: [
            /\/node_modules\/vant\//, // 路径的正则或者字符串
            (pixels, parsedVal, prop) => {
              if (prop.includes('font')) {
                return parsedVal * 2 + 'vmin';
              }
              return parsedVal * 2 + 'vw';
            },
          ],
        }),
    

    但编译报错

    17:45:06 [vite] Internal server error: object is not iterable (cannot read property Symbol(Symbol.iterator))
      Plugin: vite:css
      File: C:/Users/Administrator/Desktop/git/hnyj-vis-vue3/src/assets/iconfont.css
          at C:/Users/Administrator/Desktop/git/hnyj-vis-vue3/src/assets/iconfont.css:10:3
          at C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\@jonny1994\postcss-px-to-viewport\dist\utils.js:13:90
          at Array.find (<anonymous>)
          at createPxReplace (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\@jonny1994\postcss-px-to-viewport\dist\utils.js:13:85)      
          at Declaration (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\@jonny1994\postcss-px-to-viewport\dist\index.js:94:91)
          at LazyResult.visitTick (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\postcss\lib\lazy-result.js:502:16)
          at LazyResult.runAsync (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\postcss\lib\lazy-result.js:410:30)
          at async compileCSS (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\vite\dist\node\chunks\dep-9c153816.js:18917:27)
          at async TransformContext.transform (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\vite\dist\node\chunks\dep-9c153816.js:18488:50)
          at async Object.transform (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\vite\dist\node\chunks\dep-9c153816.js:38334:30)      
          at async doTransform (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\vite\dist\node\chunks\dep-9c153816.js:53030:29)
    17:45:06 [vite] Internal server error: object is not iterable (cannot read property Symbol(Symbol.iterator))
      Plugin: vite:css
      File: C:/Users/Administrator/Desktop/git/hnyj-vis-vue3/src/assets/reset.css
          at C:/Users/Administrator/Desktop/git/hnyj-vis-vue3/src/assets/reset.css:136:3
          at C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\@jonny1994\postcss-px-to-viewport\dist\utils.js:13:90
          at Array.find (<anonymous>)
          at createPxReplace (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\@jonny1994\postcss-px-to-viewport\dist\utils.js:13:85)      
          at Declaration (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\@jonny1994\postcss-px-to-viewport\dist\index.js:94:91)
          at LazyResult.visitTick (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\postcss\lib\lazy-result.js:502:16)
          at LazyResult.runAsync (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\postcss\lib\lazy-result.js:410:30)
          at LazyResult.async (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\postcss\lib\lazy-result.js:221:30)
          at LazyResult.then (C:\Users\Administrator\Desktop\git\hnyj-vis-vue3\node_modules\postcss\lib\lazy-result.js:206:17)
    
    
    
    opened by xiankaiqun 2
  • 插件在gulp-less中某些参数失效

    插件在gulp-less中某些参数失效

    你好,在搭配gulp-less使用时 发现include, exclude参数失效 用法:

    var ToViewport = require('@jonny1994/postcss-px-to-viewport');
    var Viewport = new ToViewport({ 
        include: [/\/src\/Pay\/\/h5\//],
        exclude: [/src[/|\\]Privilege/, /src[/|\\]Pay\/pc/, /\/node_modules\//], 
    });
    
    return gulp.src('./less/**/*.less')
      .pipe(less({
        plugins: [Viewport]
      }))
      .pipe(gulp.dest('./public/css'));
    

    执行build命令后所有的css全部被转换成了vw,exclude规则里的文件没有被忽略 烦请指教

    opened by shen774411223d 2
Releases(v1.1.0)
Owner
Jonny Chen
Jonny Chen
A simple game where you can draw art similar to pixel art, or practice Kufic Calligraphy.

Etch-A-Sketch Where to try it? https://jee-el.github.io/etch-a-sketch/ What's the game about? It's a sketching website or game where you can : Draw wh

Lhoussaine Ghallou 6 Oct 27, 2022
Math magicians is a single page app for all mathematics lovers. Math Magicians allow users to perform simple calculations and It generates mathematics quotes.

Math Magicians Math Magician is a single Page App that have three interfaces. It allows users to perform simple math calculations, user can read the m

levy_ukwishaka 10 Aug 21, 2022
This plugin was created to calibrate 3D printer settings easily.

Calibration Companion This plugin was created to calibrate your 3D printer settings easily. It comes really handy when you want to try a new filament

Guyot François 29 Jun 22, 2022
A live2d docusaurus plugin

docusaurus-plugin-2dlive A live2d docusaurus plugin Installation npm i docusaurus-plugin-2dlive or... yarn add docusaurus-plugin-2dlive Usage In file

Kim Minh Thắng 3 Dec 14, 2022
Octoprint-Detector2 is a detection plugin that runs in locally your browser and emails you if it detects some spaghetti, stringing or blobs on your print

Octoprint-Detector2 is a detection plugin that runs in locally your browser and emails you if it detects some spaghetti, stringing or blobs on your print. All you need is an email account and a PC.

Mikulash 24 Jan 2, 2023
A browserify plugin to load CSS Modules

css-modulesify A browserify plugin to load CSS Modules. Please note that this is still highly experimental. Why CSS Modules? Normally you need to use

null 407 Aug 15, 2022
A Stylis 4.x plugin that translates pixel units to rem units

stylis-px2rem-plugin A Stylis 4.x plugin that translates pixel units to rem units. Installation With npm $ npm install stylis-px2rem-plugin With yarn

oncar 13 Sep 15, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022