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.

208 lines
5.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
  1. # postcss-px-to-viewport
  2. [![NPM version](https://badge.fury.io/js/postcss-px-to-viewport.svg)](http://badge.fury.io/js/postcss-px-to-viewport)
  3. English | [中文](README_CN.md)
  4. A plugin for [PostCSS](https://github.com/postcss/postcss) that generates viewport units (vw, vh, vmin, vmax) from pixel units.
  5. <a href="https://evrone.com/?utm_source=postcss-px-to-viewport">
  6. <img src="https://user-images.githubusercontent.com/417688/34437029-dbfe4ee6-ecab-11e7-9d80-2b274b4149b3.png"
  7. alt="Sponsored by Evrone" width="231">
  8. </a>
  9. ## Demo
  10. If your project involves a fixed width, this script will help to convert pixels into viewport units.
  11. ### Input
  12. ```css
  13. .class {
  14. margin: -10px .5vh;
  15. padding: 5vmin 9.5px 1px;
  16. border: 3px solid black;
  17. border-bottom-width: 1px;
  18. font-size: 14px;
  19. line-height: 20px;
  20. }
  21. .class2 {
  22. border: 1px solid black;
  23. margin-bottom: 1px;
  24. font-size: 20px;
  25. line-height: 30px;
  26. }
  27. @media (min-width: 750px) {
  28. .class3 {
  29. font-size: 16px;
  30. line-height: 22px;
  31. }
  32. }
  33. ```
  34. ### Output
  35. ```css
  36. .class {
  37. margin: -3.125vw .5vh;
  38. padding: 5vmin 2.96875vw 1px;
  39. border: 0.9375vw solid black;
  40. border-bottom-width: 1px;
  41. font-size: 4.375vw;
  42. line-height: 6.25vw;
  43. }
  44. .class2 {
  45. border: 1px solid black;
  46. margin-bottom: 1px;
  47. font-size: 6.25vw;
  48. line-height: 9.375vw;
  49. }
  50. @media (min-width: 750px) {
  51. .class3 {
  52. font-size: 16px;
  53. line-height: 22px;
  54. }
  55. }
  56. ```
  57. ## Getting Started
  58. ### Installation
  59. Add via npm
  60. ```
  61. $ npm install postcss-px-to-viewport --save-dev
  62. ```
  63. or yarn
  64. ```
  65. $ yarn add -D postcss-px-to-viewport
  66. ```
  67. ### Usage
  68. Default Options:
  69. ```js
  70. {
  71. unitToConvert: 'px',
  72. viewportWidth: 320,
  73. unitPrecision: 5,
  74. propList: ['*'],
  75. viewportUnit: 'vw',
  76. fontViewportUnit: 'vw',
  77. selectorBlackList: [],
  78. minPixelValue: 1,
  79. mediaQuery: false,
  80. replace: true,
  81. exclude: undefined,
  82. include: undefined,
  83. landscape: false,
  84. landscapeUnit: 'vw',
  85. landscapeWidth: 568
  86. }
  87. ```
  88. - `unitToConvert` (String) unit to convert, by default, it is px.
  89. - `viewportWidth` (Number) The width of the viewport.
  90. - `unitPrecision` (Number) The decimal numbers to allow the vw units to grow to.
  91. - `propList` (Array) The properties that can change from px to vw.
  92. - Values need to be exact matches.
  93. - Use wildcard * to enable all properties. Example: ['*']
  94. - Use * at the start or end of a word. (['*position*'] will match background-position-y)
  95. - Use ! to not match a property. Example: ['*', '!letter-spacing']
  96. - Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']
  97. - `viewportUnit` (String) Expected units.
  98. - `fontViewportUnit` (String) Expected units for font.
  99. - `selectorBlackList` (Array) The selectors to ignore and leave as px.
  100. - If value is string, it checks to see if selector contains the string.
  101. - `['body']` will match `.body-class`
  102. - If value is regexp, it checks to see if the selector matches the regexp.
  103. - `[/^body$/]` will match `body` but not `.body`
  104. - `minPixelValue` (Number) Set the minimum pixel value to replace.
  105. - `mediaQuery` (Boolean) Allow px to be converted in media queries.
  106. - `replace` (Boolean) replaces rules containing vw instead of adding fallbacks.
  107. - `exclude` (Array or Regexp) Ignore some files like 'node_modules'
  108. - If value is regexp, will ignore the matches files.
  109. - If value is array, the elements of the array are regexp.
  110. - `include` (Array or Regexp) If `include` is set, only matching files will be converted, for example, only files under 'src/mobile'
  111. - If the value is regexp, the matching file will be included, otherwise it will be excluded.
  112. - If value is array, the elements of the array are regexp.
  113. - `landscape` (Boolean) Adds `@media (orientation: landscape)` with values converted via `landscapeWidth`.
  114. - `landscapeUnit` (String) Expected unit for `landscape` option
  115. - `landscapeWidth` (Number) Viewport width for landscape orientation.
  116. > `exclude` and` include` can be set together, and the intersection of the two rules will be taken.
  117. #### Use with gulp-postcss
  118. add to your `gulpfile.js`:
  119. ```js
  120. var gulp = require('gulp');
  121. var postcss = require('gulp-postcss');
  122. var pxtoviewport = require('postcss-px-to-viewport');
  123. gulp.task('css', function () {
  124. var processors = [
  125. pxtoviewport({
  126. viewportWidth: 320,
  127. viewportUnit: 'vmin'
  128. })
  129. ];
  130. return gulp.src(['build/css/**/*.css'])
  131. .pipe(postcss(processors))
  132. .pipe(gulp.dest('build/css'));
  133. });
  134. ```
  135. #### Use with PostCss configuration file
  136. add to your `postcss.config.js`
  137. ```js
  138. module.exports = {
  139. plugins: {
  140. // ...
  141. 'postcss-px-to-viewport': {
  142. // options
  143. }
  144. }
  145. }
  146. ```
  147. ## Running the tests
  148. In order to run tests, you need to install dev-packages:
  149. ```
  150. $ npm install
  151. ```
  152. Then run the tests via npm script:
  153. ```
  154. $ npm run test
  155. ```
  156. ## Contributing
  157. Please read [Code of Conduct](CODE-OF-CONDUCT.md) and [Contributing Guidelines](CONTRIBUTING.md) for submitting pull requests to us.
  158. ## Versioning
  159. We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/evrone/postcss-px-to-viewport/tags).
  160. ## Changelog
  161. The changelog is [here](CHANGELOG.md).
  162. ## Authors
  163. * [Dmitry Karpunin](https://github.com/KODerFunk) - *Initial work*
  164. * [Ivan Bunin](https://github.com/chernobelenkiy)
  165. See also the list of [contributors](https://github.com/evrone/postcss-px-to-viewport/contributors) who participated in this project.
  166. ## License
  167. This project is licensed under the [MIT License](LICENSE).
  168. ## Acknowledgments
  169. * Hat tip to https://github.com/cuth/postcss-pxtorem/ for inspiring us for this project.