You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
  1. # postcss-px-to-viewport [![NPM version](https://badge.fury.io/js/postcss-px-to-viewport.svg)](http://badge.fury.io/js/postcss-px-to-viewport)
  2. A plugin for [PostCSS](https://github.com/ai/postcss) that generates viewport units (vw, vh, vmin, vmax) from pixel units.
  3. ## Usage
  4. If your project involves a fixed width, this script will help to convert pixels into viewport units.
  5. ### Input/Output
  6. ```css
  7. // input
  8. .class {
  9. margin: -10px .5vh;
  10. padding: 5vmin 9.5px 1px;
  11. border: 3px solid black;
  12. border-bottom-width: 1px;
  13. font-size: 14px;
  14. line-height: 20px;
  15. }
  16. .class2 {
  17. border: 1px solid black;
  18. margin-bottom: 1px;
  19. font-size: 20px;
  20. line-height: 30px;
  21. }
  22. @media (min-width: 750px) {
  23. .class3 {
  24. font-size: 16px;
  25. line-height: 22px;
  26. }
  27. }
  28. // output
  29. .class {
  30. margin: -3.125vw .5vh;
  31. padding: 5vmin 2.96875vw 1px;
  32. border: 0.9375vw solid black;
  33. border-bottom-width: 1px;
  34. font-size: 4.375vw;
  35. line-height: 6.25vw;
  36. }
  37. .class2 {
  38. border: 1px solid black;
  39. margin-bottom: 1px;
  40. font-size: 6.25vw;
  41. line-height: 9.375vw;
  42. }
  43. @media (min-width: 234.375vw) {
  44. .class3 {
  45. font-size: 5vw;
  46. line-height: 6.875vw;
  47. }
  48. }
  49. ```
  50. ### Example
  51. ```js
  52. 'use strict';
  53. var fs = require('fs');
  54. var postcss = require('postcss');
  55. var pxToViewport = require('..');
  56. var css = fs.readFileSync('main.css', 'utf8');
  57. var options = {
  58. replace: false
  59. };
  60. var processedCss = postcss(pxToViewport(options)).process(css).css;
  61. fs.writeFile('main-viewport.css', processedCss, function (err) {
  62. if (err) {
  63. throw err;
  64. }
  65. console.log('File with viewport units written.');
  66. });
  67. ```
  68. ### Options
  69. Default:
  70. ```js
  71. {
  72. unitToConvert: 'px',
  73. viewportWidth: 320,
  74. viewportHeight: 568, // not now used; TODO: need for different units and math for different properties
  75. unitPrecision: 5,
  76. viewportUnit: 'vw',
  77. fontViewportUnit: 'vw', // vmin is more suitable.
  78. selectorBlackList: [],
  79. minPixelValue: 1,
  80. mediaQuery: false,
  81. exclude: [] // ignore some files
  82. }
  83. ```
  84. - `unitToConvert` (String) unit to convert, by default, it is px.
  85. - `viewportWidth` (Number) The width of the viewport.
  86. - `viewportHeight` (Number) The height of the viewport.
  87. - `unitPrecision` (Number) The decimal numbers to allow the REM units to grow to.
  88. - `viewportUnit` (String) Expected units.
  89. - `fontViewportUnit` (String) Expected units for font.
  90. - `selectorBlackList` (Array) The selectors to ignore and leave as px.
  91. - If value is string, it checks to see if selector contains the string.
  92. - `['body']` will match `.body-class`
  93. - If value is regexp, it checks to see if the selector matches the regexp.
  94. - `[/^body$/]` will match `body` but not `.body`
  95. - `minPixelValue` (Number) Set the minimum pixel value to replace.
  96. - `mediaQuery` (Boolean) Allow px to be converted in media queries.
  97. - `exclude` (Array or Regexp) Ignore some files like 'node_modules'
  98. - If value is regexp, will ignore the matches files.
  99. - If value is array, the elements of the array are regexp.
  100. ### Use with gulp-postcss
  101. ```js
  102. var gulp = require('gulp');
  103. var postcss = require('gulp-postcss');
  104. var pxtoviewport = require('postcss-px-to-viewport');
  105. gulp.task('css', function () {
  106. var processors = [
  107. pxtoviewport({
  108. viewportWidth: 320,
  109. viewportUnit: 'vmin'
  110. })
  111. ];
  112. return gulp.src(['build/css/**/*.css'])
  113. .pipe(postcss(processors))
  114. .pipe(gulp.dest('build/css'));
  115. });
  116. ```