web 3d图形渲染器
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.

310 lines
7.6 KiB

  1. # PostCSS Media Minmax
  2. [![CSS Standard Status](https://cssdb.org/badge/media-query-ranges.svg)](https://cssdb.org/#media-query-ranges)
  3. [![Build Status](https://travis-ci.org/postcss/postcss-media-minmax.svg?branch=master)](https://travis-ci.org/postcss/postcss-media-minmax)
  4. [![NPM Downloads](https://img.shields.io/npm/dm/postcss-media-minmax.svg?style=flat)](https://www.npmjs.com/package/postcss-media-minmax)
  5. [![NPM Version](https://img.shields.io/npm/v/postcss-media-minmax.svg?style=flat)](https://www.npmjs.com/package/postcss-media-minmax)
  6. [![License](https://img.shields.io/npm/l/postcss-media-minmax.svg?style=flat)](https://opensource.org/licenses/MIT)
  7. > Writing simple and graceful media queries!
  8. The `min-width`, `max-width` and many other properties of media queries are really confusing. I want to cry every time I see them. But right now according to the new specs, you can use more intuitive `<=` or `>=` to replace the `min-`/`max-` prefixes in media queries.
  9. V2.1.0 began to support `>` or `<` symbol.
  10. This is a polyfill plugin which supports [CSS Media Queries Level 4](https://drafts.csswg.org/mediaqueries/#mq-range-context) and gives you access to the new features right away. Mom will never worry about my study any more. So amazing!
  11. [简体中文](README-zh.md)
  12. -----
  13. ![Gif Demo](https://gtms02.alicdn.com/tps/i2/TB1UIjyGVXXXXcCaXXXx274FpXX-877-339.gif)
  14. ## Installation
  15. $ npm install postcss-media-minmax
  16. ## Quick Start
  17. Example 1:
  18. ```js
  19. var fs = require('fs')
  20. var postcss = require('postcss')
  21. var minmax = require('postcss-media-minmax')
  22. var css = fs.readFileSync('input.css', 'utf8')
  23. var output = postcss()
  24. .use(minmax())
  25. .process(css)
  26. .css
  27. console.log('\n====>Output CSS:\n', output)
  28. ```
  29. Or just:
  30. ```js
  31. var output = postcss(minmax())
  32. .process(css)
  33. .css
  34. ```
  35. input.css:
  36. ```css
  37. @media screen and (width >= 500px) and (width <= 1200px) {
  38. .bar {
  39. display: block;
  40. }
  41. }
  42. ```
  43. You will get:
  44. ```css
  45. @media screen and (min-width: 500px) and (max-width: 1200px) {
  46. .bar {
  47. display: block;
  48. }
  49. }
  50. ```
  51. ## CSS syntax
  52. ### [Syntax](https://drafts.csswg.org/mediaqueries/#mq-range-context)
  53. ```
  54. <mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value>
  55. | <mf-value> [ '<' | '>' ]? '='? <mf-name>
  56. | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value>
  57. | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>
  58. ```
  59. ![syntax](https://gtms03.alicdn.com/tps/i3/TB1Rje0HXXXXXXeXpXXccZJ0FXX-640-290.png)
  60. PostCSS Media Minmax hasn't implemented syntax such as `200px > = width` or `200px < = width` currently because its readability is not good enough yet.
  61. ## [Values](https://drafts.csswg.org/mediaqueries/#values)
  62. **The special values:**
  63. * [<ratio>](https://drafts.csswg.org/mediaqueries/#typedef-ratio)
  64. The <ratio> value type is a positive (not zero or negative) <integer> followed by optional whitespace, followed by a solidus ('/'), followed by optional whitespace, followed by a positive <integer>. <ratio>s can be ordered or compared by transforming them into the number obtained by dividing their first <integer> by their second <integer>.
  65. ```css
  66. @media screen and (device-aspect-ratio: 16 / 9) {
  67. /* rules */
  68. }
  69. /* equivalent to */
  70. @media screen and (device-aspect-ratio: 16/9) {
  71. /* rules */
  72. }
  73. ```
  74. * [<mq-boolean>](https://drafts.csswg.org/mediaqueries/#typedef-mq-boolean)
  75. The <mq-boolean> value type is an <integer> with the value 0 or 1. Any other integer value is invalid. Note that -0 is always equivalent to 0 in CSS, and so is also accepted as a valid <mq-boolean> value.
  76. ```css
  77. @media screen and (grid: -0) {
  78. /* rules */
  79. }
  80. /* equivalent to */
  81. @media screen and (grid: 0) {
  82. /* rules */
  83. }
  84. ```
  85. ## How to use
  86. ### Shorthand
  87. In Example 1, if a feature has both `>=` and `<=` logic, it can be written as follows:
  88. ```css
  89. @media screen and (500px <= width <= 1200px) {
  90. .bar {
  91. display: block;
  92. }
  93. }
  94. /* Or */
  95. @media screen and (1200px >= width >= 500px) {
  96. .bar {
  97. display: block;
  98. }
  99. }
  100. ```
  101. Which will output:
  102. ```css
  103. @media screen and (min-width: 500px) and (max-width: 1200px) {
  104. .bar {
  105. display: block;
  106. }
  107. }
  108. ```
  109. **Note**: When the media feature name is in the middle, we must ensure that two `<=` or `>=` are in the same direction, otherwise which will not be converted.
  110. E.g. in the example below, `width` is greater than or equal to 500px and is greater than or equal to 1200px, which is the wrong in both grammar and logic.
  111. ```css
  112. @media screen and (1200px <= width >= 500px) {
  113. .bar {
  114. display: block;
  115. }
  116. }
  117. ```
  118. ### Media feature names
  119. The following properties support the `min-`/`max-` prefixes in the specifications at present, and will be automatically converted by PostCSS Media Minmax.
  120. * `width`
  121. * `height`
  122. * `device-width`
  123. * `device-height`
  124. * `aspect-ratio`
  125. * `device-aspect-ratio`
  126. * `color`
  127. * `color-index`
  128. * `monochrome`
  129. * `resolution`
  130. ### Using with `@custom-media` & Node Watch
  131. ```js
  132. var fs = require('fs')
  133. var chokidar = require('chokidar')
  134. var postcss = require('postcss')
  135. var minmax = require('postcss-media-minmax')
  136. var customMedia = require('postcss-custom-media')
  137. var src = 'input.css'
  138. console.info('Watching…\nModify the input.css and save.')
  139. chokidar.watch(src, {
  140. ignored: /[\/\\]\./,
  141. persistent: true
  142. }).on('all',
  143. function(event, path, stats) {
  144. var css = fs.readFileSync(src, 'utf8')
  145. var output = postcss()
  146. .use(customMedia())
  147. .use(minmax())
  148. .process(css)
  149. .css;
  150. fs.writeFileSync('output.css', output)
  151. })
  152. ```
  153. input.css:
  154. ```css
  155. @custom-media --foo (width >= 20em) and (width <= 50em);
  156. @custom-media --bar (height >= 300px) and (height <= 600px);
  157. @media (--foo) and (--bar) {
  158. }
  159. ```
  160. output.css:
  161. ```css
  162. @media (min-width: 20em) and (max-width: 50em) and (min-height: 300px) and (max-height: 600px) {
  163. }
  164. ```
  165. ### Grunt
  166. ```js
  167. module.exports = function(grunt) {
  168. grunt.initConfig({
  169. pkg: grunt.file.readJSON('package.json'),
  170. postcss: {
  171. options: {
  172. processors: [
  173. require('autoprefixer-core')({ browsers: ['> 0%'] }).postcss, //Other plugin
  174. require('postcss-media-minmax')(),
  175. ]
  176. },
  177. dist: {
  178. src: ['src/*.css'],
  179. dest: 'build/grunt.css'
  180. }
  181. }
  182. });
  183. grunt.loadNpmTasks('grunt-contrib-uglify');
  184. grunt.loadNpmTasks('grunt-postcss');
  185. grunt.registerTask('default', ['postcss']);
  186. }
  187. ```
  188. ### Gulp
  189. ```js
  190. var gulp = require('gulp');
  191. var rename = require('gulp-rename');
  192. var postcss = require('gulp-postcss');
  193. var selector = require('postcss-media-minmax')
  194. var autoprefixer = require('autoprefixer-core')
  195. gulp.task('default', function () {
  196. var processors = [
  197. autoprefixer({ browsers: ['> 0%'] }), //Other plugin
  198. minmax()
  199. ];
  200. gulp.src('src/*.css')
  201. .pipe(postcss(processors))
  202. .pipe(rename('gulp.css'))
  203. .pipe(gulp.dest('build'))
  204. });
  205. gulp.watch('src/*.css', ['default']);
  206. ```
  207. ## Contributing
  208. * Install all the dependent modules.
  209. * Respect the coding style (Use [EditorConfig](https://editorconfig.org/)).
  210. * Add test cases in the [test](test) directory.
  211. * Run the test cases.
  212. ```
  213. $ git clone https://github.com/postcss/postcss-media-minmaxs.git
  214. $ git checkout -b patch
  215. $ npm install
  216. $ npm test
  217. ```
  218. ## Acknowledgements
  219. * Thank the author of PostCSS [Andrey Sitnik](https://github.com/ai) for giving us such simple and easy CSS syntax analysis tools.
  220. * Thank [Tab Atkins Jr.](https://www.xanthir.com/contact/) for writing the specs of Media Queries Level 4.
  221. * Thank [ziyunfei](https://weibo.com/p/1005051708684567) for suggestions and help of this plugin.
  222. ## [Changelog](CHANGELOG.md)
  223. ## [License](LICENSE)