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.

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