All Projects → evrone → Postcss Px To Viewport

evrone / Postcss Px To Viewport

Licence: mit
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.

Programming Languages

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

Projects that are alternatives of or similar to Postcss Px To Viewport

Postcss Pxtorem
Convert pixel units to rem (root em) units using PostCSS
Stars: ✭ 1,592 (-20.28%)
Mutual labels:  postcss, rem-units, pixel-units
postcss-if-media
A PostCSS plugin to inline or nest media queries within CSS rules & properties.
Stars: ✭ 35 (-98.25%)
Mutual labels:  postcss, media-queries
postcss-sort-media-queries
PostCSS plugin for combine and sort CSS media queries with mobile first or desktop first methods.
Stars: ✭ 118 (-94.09%)
Mutual labels:  postcss, media-queries
postcss-inline-media
Media queries shortcut, built on PostCSS, example: font-size: 20px @1200 18px @480 16px;
Stars: ✭ 47 (-97.65%)
Mutual labels:  postcss, media-queries
react-cli
基于 create-react-app 搭建的前端脚手架
Stars: ✭ 18 (-99.1%)
Mutual labels:  postcss, vw
Postcss High Contrast
Create high contrast version of your project with ease.
Stars: ✭ 108 (-94.59%)
Mutual labels:  postcss
React Typescript Webpack2 Cssmodules Postcss
Simple Starter Template for React, TypeScript, postCSS, ITCSS, CSS-Modules, Webpack and Live Reloading (React Hot Loader 3)
Stars: ✭ 117 (-94.14%)
Mutual labels:  postcss
Tailwindcss
A utility-first CSS framework for rapid UI development.
Stars: ✭ 50,879 (+2447.77%)
Mutual labels:  postcss
Postcss Uncss
Use uncss as a postcss plugin
Stars: ✭ 105 (-94.74%)
Mutual labels:  postcss
React Redux Graphql Apollo Bootstrap Webpack Starter
react js + redux + graphQL + Apollo + react router + hot reload + devTools + bootstrap + webpack starter
Stars: ✭ 127 (-93.64%)
Mutual labels:  postcss
Midas
Syntax highlighter based on PostCSS.
Stars: ✭ 123 (-93.84%)
Mutual labels:  postcss
Webpack4 Cli
🌈 Webpack4 + Vue/React + TS (SPA or MPA) 面向实际工作流(开发 & 编译 & 打包 & 部署 & 上线)的脚手架,并提供docker镜像供快速构建。
Stars: ✭ 115 (-94.24%)
Mutual labels:  postcss
Fabium
+100 for productivity
Stars: ✭ 108 (-94.59%)
Mutual labels:  postcss
Static Site Boilerplate
A better workflow for building modern static websites.
Stars: ✭ 1,633 (-18.23%)
Mutual labels:  postcss
Elm Batteries
Learn how Elm, Parcel, Cypress and Netlify work together. Get started with Elm navigation, routes, remote data and decoder.
Stars: ✭ 108 (-94.59%)
Mutual labels:  postcss
Frasco
Quick starter for Jekyll including full setup for Sass, PostCSS, Autoprefixer, stylelint, Webpack, ESLint, imagemin, Browsersync, etc.
Stars: ✭ 123 (-93.84%)
Mutual labels:  postcss
Postcss Plugins
The "officially unofficial" consolidated list of PostCSS plugins in a ready-to-use package
Stars: ✭ 107 (-94.64%)
Mutual labels:  postcss
Fuzzymail
📨 Email template generator. Making emails fun again.
Stars: ✭ 114 (-94.29%)
Mutual labels:  postcss
Postcss Aspect Ratio Mini
A PostCSS plugin to fix an element's dimensions to an aspect ratio
Stars: ✭ 123 (-93.84%)
Mutual labels:  postcss
Postcss Quantity Queries
PostCSS plugin enabling quantity-queries
Stars: ✭ 113 (-94.34%)
Mutual labels:  postcss

postcss-px-to-viewport

NPM version

English | 中文

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 .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 .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 postcss-px-to-viewport --save-dev

or yarn

$ yarn add -D 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
}
  • 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) Allow px to be converted in media queries.
  • 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.

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

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.

Use with PostCss configuration file

add to your postcss.config.js

module.exports = {
  plugins: {
    // ...
    '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('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'));
});

Contributing

Please read Code of Conduct and Contributing Guidelines for submitting pull requests to us.

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.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License.

Sponsors

Visit Evrone website to get more information about the projects build.

Sponsored by Evrone

Acknowledgments

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].