5636c340bad51b673ee5e9af82fd7b0f1254830bef6b7c699235e95eee856b886cc63c85b57d8762ce03d14f07bfbf2511f49b603319333d80b4eb2357ac1b 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. # Autoprefixer [![Build Status][ci-img]][ci]
  2. <img align="right" width="94" height="71"
  3. src="http://postcss.github.io/autoprefixer/logo.svg"
  4. title="Autoprefixer logo by Anton Lovchikov">
  5. [PostCSS] plugin to parse CSS and add vendor prefixes to CSS rules using values
  6. from [Can I Use]. It is [recommended] by Google and used in Twitter and Taobao.
  7. Write your CSS rules without vendor prefixes (in fact, forget about them
  8. entirely):
  9. ```css
  10. :fullscreen a {
  11. display: flex
  12. }
  13. ```
  14. Autoprefixer will use the data based on current browser popularity and property
  15. support to apply prefixes for you. You can try the [interactive demo]
  16. of Autoprefixer.
  17. ```css
  18. :-webkit-full-screen a {
  19. display: -webkit-box;
  20. display: flex
  21. }
  22. :-moz-full-screen a {
  23. display: flex
  24. }
  25. :-ms-fullscreen a {
  26. display: -ms-flexbox;
  27. display: flex
  28. }
  29. :fullscreen a {
  30. display: -webkit-box;
  31. display: -ms-flexbox;
  32. display: flex
  33. }
  34. ```
  35. Twitter account for news and releases: [@autoprefixer].
  36. <a href="https://evilmartians.com/?utm_source=autoprefixer">
  37. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
  38. </a>
  39. [interactive demo]: http://autoprefixer.github.io/
  40. [@autoprefixer]: https://twitter.com/autoprefixer
  41. [recommended]: https://developers.google.com/web/tools/setup/setup-buildtools#dont-trip-up-with-vendor-prefixes
  42. [Can I Use]: http://caniuse.com/
  43. [PostCSS]: https://github.com/postcss/postcss
  44. [ci-img]: https://travis-ci.org/postcss/autoprefixer.svg
  45. [ci]: https://travis-ci.org/postcss/autoprefixer
  46. ## Features
  47. ### Write Pure CSS
  48. Working with Autoprefixer is simple: just forget about vendor prefixes
  49. and write normal CSS according to the latest W3C specs. You don’t need
  50. a special language (like Sass) or remember where you must use mixins.
  51. Autoprefixer supports selectors (like `:fullscreen` and `::selection`),
  52. unit function (`calc()`), at‑rules (`@supports` and `@keyframes`)
  53. and properties.
  54. Because Autoprefixer is a postprocessor for CSS,
  55. you can also use it with preprocessors such as Sass, Stylus or LESS.
  56. ### Flexbox, Filters, etc.
  57. Just write normal CSS according to the latest W3C specs and Autoprefixer
  58. will produce the code for old browsers.
  59. ```css
  60. a {
  61. display: flex;
  62. }
  63. ```
  64. compiles to:
  65. ```css
  66. a {
  67. display: -webkit-box;
  68. display: -webkit-flex;
  69. display: -ms-flexbox;
  70. display: flex
  71. }
  72. ```
  73. Autoprefixer has [27 special hacks] to fix web browser differences.
  74. [27 special hacks]: https://github.com/postcss/autoprefixer/tree/master/lib/hacks
  75. ### Only Actual Prefixes
  76. Autoprefixer utilizes the most recent data from [Can I Use]
  77. to add only necessary vendor prefixes.
  78. It also removes old, unnecessary prefixes from your CSS
  79. (like `border-radius` prefixes, produced by many CSS libraries).
  80. ```css
  81. a {
  82. -webkit-border-radius: 5px;
  83. border-radius: 5px;
  84. }
  85. ```
  86. compiles to:
  87. ```css
  88. a {
  89. border-radius: 5px;
  90. }
  91. ```
  92. [Can I Use]: http://caniuse.com/
  93. ## Browsers
  94. Autoprefixer uses [Browserslist], so you can specify the browsers
  95. you want to target in your project by queries like `last 2 versions`
  96. or `> 5%`.
  97. The best way to provide browsers is `browserslist` config
  98. or `package.json` with `browserslist` key. Put it in your project root.
  99. We recommend to avoid Autoprefixer option and use `browserslist` config
  100. or `package.json`. In this case browsers will be shared with other tools
  101. like [babel-preset-env] or [Stylelint].
  102. See [Browserslist docs] for queries, browser names, config format,
  103. and default value.
  104. [Browserslist docs]: https://github.com/ai/browserslist#queries
  105. [babel-preset-env]: https://github.com/babel/babel-preset-env
  106. [Browserslist]: https://github.com/ai/browserslist
  107. [Stylelint]: http://stylelint.io/
  108. ## Outdated Prefixes
  109. By default, Autoprefixer also removes outdated prefixes.
  110. You can disable this behavior with the `remove: false` option. If you have
  111. no legacy code, this option will make Autoprefixer about 10% faster.
  112. Also, you can set the `add: false` option. Autoprefixer will only clean outdated
  113. prefixes, but will not add any new prefixes.
  114. Autoprefixer adds new prefixes between any unprefixed properties and already
  115. written prefixes in your CSS. If it will break the expected prefixes order,
  116. you can clean all prefixes from your CSS and then
  117. add the necessary prefixes again:
  118. ```js
  119. var cleaner = postcss([ autoprefixer({ add: false, browsers: [] }) ]);
  120. var prefixer = postcss([ autoprefixer ]);
  121. cleaner.process(css).then(function (cleaned) {
  122. return prefixer.process(cleaned.css)
  123. }).then(function (result) {
  124. console.log(result.css);
  125. });
  126. ```
  127. ## FAQ
  128. #### No prefixes in production
  129. Many other tools contain Autoprefixer. For example, webpack uses Autoprefixer
  130. to minify CSS by cleaning unnecessary prefixes.
  131. If you set browsers list to Autoprefixer by `browsers` option, only first
  132. Autoprefixer will know your browsers. Autoprefixer inside webpack will use
  133. default browsers list. As result, webpack will remove prefixes, that first
  134. Autoprefixer added.
  135. You need to put your browsers to [`browserslist` config] in project root —
  136. as result all tools (Autoprefixer, cssnano, doiuse, cssnext) will use same
  137. browsers list.
  138. [`browserslist` config]: https://github.com/ai/browserslist#config-file
  139. #### Does it add polyfills?
  140. No. Autoprefixer only adds prefixes.
  141. Most new CSS features will require client side JavaScript to handle a new
  142. behavior correctly.
  143. Depending on what you consider to be a “polyfill”, you can take a look at some
  144. other tools and libraries. If you are just looking for syntax sugar,
  145. you might take a look at:
  146. - [Oldie], a PostCSS plugin that handles some IE hacks (opacity, rgba, etc).
  147. - [postcss-flexbugs-fixes], a PostCSS plugin to fix flexbox issues.
  148. - [cssnext], a tool that allows you to write standard CSS syntax non-implemented
  149. yet in browsers (custom properties, custom media, color functions, etc).
  150. [postcss-flexbugs-fixes]: https://github.com/luisrudge/postcss-flexbugs-fixes
  151. [cssnext]: https://github.com/MoOx/postcss-cssnext
  152. [Oldie]: https://github.com/jonathantneal/oldie
  153. #### Why doesn’t Autoprefixer add prefixes to `border-radius`?
  154. Developers are often surprised by how few prefixes are required today.
  155. If Autoprefixer doesn’t add prefixes to your CSS, check if they’re still
  156. required on [Can I Use].
  157. There is a [list with all supported] properties, values, and selectors.
  158. [list with all supported]: https://github.com/postcss/autoprefixer/wiki/support-list
  159. [Can I Use]: http://caniuse.com/
  160. #### Why Autoprefixer uses unprefixed properties in `@-webkit-keyframes`?
  161. Browser teams can remove some prefixes before others. So we try to use
  162. all combinations of prefixed/unprefixed values.
  163. #### How to work with legacy `-webkit-` only code?
  164. Autoprefixer needs unprefixed property to add prefixes. So if you only
  165. wrote `-webkit-gradient` without W3C’s `gradient`,
  166. Autoprefixer will not add other prefixes.
  167. But [PostCSS] has a plugins to convert CSS to unprefixed state.
  168. Use them before Autoprefixer:
  169. * [postcss-unprefix]
  170. * [postcss-flexboxfixer]
  171. * [postcss-gradientfixer]
  172. [postcss-gradientfixer]: https://github.com/hallvors/postcss-gradientfixer
  173. [postcss-flexboxfixer]: https://github.com/hallvors/postcss-flexboxfixer
  174. [postcss-unprefix]: https://github.com/yisibl/postcss-unprefix
  175. #### Does Autoprefixer add `-epub-` prefix?
  176. No, Autoprefixer works only with browsers prefixes from Can I Use.
  177. But you can use [postcss-epub](https://github.com/Rycochet/postcss-epub)
  178. for prefixing ePub3 properties.
  179. #### Why doesn’t Autoprefixer transform generic font-family `system-ui`?
  180. `system-ui` is technically not a prefix and the transformation is not future-proof. But you can use [postcss-font-family-system-ui](https://github.com/JLHwung/postcss-font-family-system-ui) to transform `system-ui` to a practical font-family list.
  181. ## Usage
  182. ### Gulp
  183. In Gulp you can use [gulp-postcss] with `autoprefixer` npm package.
  184. ```js
  185. gulp.task('autoprefixer', function () {
  186. var postcss = require('gulp-postcss');
  187. var sourcemaps = require('gulp-sourcemaps');
  188. var autoprefixer = require('autoprefixer');
  189. return gulp.src('./src/*.css')
  190. .pipe(sourcemaps.init())
  191. .pipe(postcss([ autoprefixer() ]))
  192. .pipe(sourcemaps.write('.'))
  193. .pipe(gulp.dest('./dest'));
  194. });
  195. ```
  196. With `gulp-postcss` you also can combine Autoprefixer
  197. with [other PostCSS plugins].
  198. [other PostCSS plugins]: https://github.com/postcss/postcss#plugins
  199. [gulp-postcss]: https://github.com/postcss/gulp-postcss
  200. ### Webpack
  201. In [webpack] you can use [postcss-loader] with `autoprefixer`
  202. and [other PostCSS plugins].
  203. ```js
  204. module.exports = {
  205. module: {
  206. loaders: [
  207. {
  208. test: /\.css$/,
  209. loader: "style-loader!css-loader!postcss-loader"
  210. }
  211. ]
  212. }
  213. }
  214. ```
  215. And create a `postcss.config.js` with:
  216. ```js
  217. module.exports = {
  218. plugins: [
  219. require('autoprefixer')
  220. ]
  221. }
  222. ```
  223. [other PostCSS plugins]: https://github.com/postcss/postcss#plugins
  224. [postcss-loader]: https://github.com/postcss/postcss-loader
  225. [webpack]: http://webpack.github.io/
  226. ### Grunt
  227. In Grunt you can use [grunt-postcss] with `autoprefixer` npm package.
  228. ```js
  229. module.exports = function(grunt) {
  230. grunt.loadNpmTasks('grunt-postcss');
  231. grunt.initConfig({
  232. postcss: {
  233. options: {
  234. map: true,
  235. processors: [
  236. require('autoprefixer')
  237. ]
  238. },
  239. dist: {
  240. src: 'css/*.css'
  241. }
  242. }
  243. });
  244. grunt.registerTask('default', ['postcss:dist']);
  245. };
  246. ```
  247. With `grunt-postcss` you also can combine Autoprefixer
  248. with [other PostCSS plugins].
  249. [other PostCSS plugins]: https://github.com/postcss/postcss#plugins
  250. [grunt-postcss]: https://github.com/nDmitry/grunt-postcss
  251. ### Other Build Tools:
  252. * **Ruby on Rails**: [autoprefixer-rails]
  253. * **Brunch**: [postcss-brunch]
  254. * **Broccoli**: [broccoli-postcss]
  255. * **Middleman**: [middleman-autoprefixer]
  256. * **Mincer**: add `autoprefixer` npm package and enable it:
  257. `environment.enable('autoprefixer')`
  258. * **Jekyll**: add `autoprefixer-rails` and `jekyll-assets` to `Gemfile`
  259. [middleman-autoprefixer]: https://github.com/middleman/middleman-autoprefixer
  260. [autoprefixer-rails]: https://github.com/ai/autoprefixer-rails
  261. [broccoli-postcss]: https://github.com/jeffjewiss/broccoli-postcss
  262. [postcss-brunch]: https://github.com/iamvdo/postcss-brunch
  263. ### Preprocessors
  264. * **Less**: [less-plugin-autoprefix]
  265. * **Stylus**: [autoprefixer-stylus]
  266. * **Compass**: [autoprefixer-rails#compass]
  267. [less-plugin-autoprefix]: https://github.com/less/less-plugin-autoprefix
  268. [autoprefixer-stylus]: https://github.com/jenius/autoprefixer-stylus
  269. [autoprefixer-rails#compass]: https://github.com/ai/autoprefixer-rails#compass
  270. ### CSS-in-JS
  271. There is [postcss-js] to use Autoprefixer in React Inline Styles, [Free Style],
  272. Radium and other CSS-in-JS solutions.
  273. ```js
  274. let prefixer = postcssJs.sync([ autoprefixer ]);
  275. let style = prefixer({
  276. display: 'flex'
  277. });
  278. ```
  279. [postcss-js]: https://github.com/postcss/postcss-js
  280. [Free Style]: https://github.com/blakeembrey/free-style
  281. ### GUI Tools
  282. * [CodeKit](https://incident57.com/codekit/help.html#autoprefixer)
  283. * [Prepros](https://prepros.io)
  284. ### CLI
  285. You can use the [postcss-cli] to run Autoprefixer from CLI:
  286. ```sh
  287. npm install --global postcss-cli autoprefixer
  288. postcss --use autoprefixer *.css -d build/
  289. ```
  290. See `postcss -h` for help.
  291. [postcss-cli]: https://github.com/postcss/postcss-cli
  292. ### JavaScript
  293. You can use Autoprefixer with [PostCSS] in your Node.js application
  294. or if you want to develop an Autoprefixer plugin for new environment.
  295. ```js
  296. var autoprefixer = require('autoprefixer');
  297. var postcss = require('postcss');
  298. postcss([ autoprefixer ]).process(css).then(function (result) {
  299. result.warnings().forEach(function (warn) {
  300. console.warn(warn.toString());
  301. });
  302. console.log(result.css);
  303. });
  304. ```
  305. There is also [standalone build] for the browser or as a non-Node.js runtime.
  306. You can use [html-autoprefixer] to process HTML with inlined CSS.
  307. [html-autoprefixer]: https://github.com/RebelMail/html-autoprefixer
  308. [standalone build]: https://raw.github.com/ai/autoprefixer-rails/master/vendor/autoprefixer.js
  309. [PostCSS]: https://github.com/postcss/postcss
  310. ### Text Editors and IDE
  311. Autoprefixer should be used in assets build tools. Text editor plugins are not
  312. a good solution, because prefixes decrease code readability and you will need
  313. to change value in all prefixed properties.
  314. I recommend you to learn how to use build tools like [Gulp].
  315. They work much better and will open you a whole new world of useful plugins
  316. and automatization.
  317. But, if you can’t move to a build tool, you can use text editor plugins:
  318. * [Sublime Text](https://github.com/sindresorhus/sublime-autoprefixer)
  319. * [Brackets](https://github.com/mikaeljorhult/brackets-autoprefixer)
  320. * [Atom Editor](https://github.com/sindresorhus/atom-autoprefixer)
  321. * [Visual Studio](http://vswebessentials.com/)
  322. [Gulp]: http://gulpjs.com/
  323. ## Warnings
  324. Autoprefixer uses the [PostCSS warning API] to warn about really important problems
  325. in your CSS:
  326. * Old direction syntax in gradients.
  327. * Old unprefixed `display: box` instead of `display: flex`
  328. by latest specification version.
  329. You can get warnings from `result.warnings()`:
  330. ```js
  331. result.warnings().forEach(function (warn) {
  332. console.warn(warn.toString());
  333. });
  334. ```
  335. Every Autoprefixer runner should display this warnings.
  336. [PostCSS warning API]: https://github.com/postcss/postcss/blob/master/docs/api.md#warning-class
  337. ## Disabling
  338. Autoprefixer was designed to have no interface – it just works.
  339. If you need some browser specific hack just write a prefixed property
  340. after the unprefixed one.
  341. ```css
  342. a {
  343. transform: scale(0.5);
  344. -moz-transform: scale(0.6);
  345. }
  346. ```
  347. If some prefixes were generated in a wrong way,
  348. please create an issue on GitHub.
  349. Autoprefixer has 4 features, which can be disabled by options:
  350. * `supports: false` will disable `@supports` parameters prefixing.
  351. * `flexbox: false` will disable flexbox properties prefixing.
  352. Or `flexbox: "no-2009"` will add prefixes only for final and IE
  353. versions of specification.
  354. * `grid: false` will disable Grid Layout prefixes for IE.
  355. * `remove: false` will disable cleaning outdated prefixes.
  356. If you do not need Autoprefixer in some part of your CSS,
  357. you can use control comments to disable Autoprefixer.
  358. ```css
  359. a {
  360. transition: 1s; /* it will be prefixed */
  361. }
  362. b {
  363. /* autoprefixer: off */
  364. transition: 1s; /* it will not be prefixed */
  365. }
  366. ```
  367. Control comments disable Autoprefixer within the whole rule in which
  368. you place it. In the above example, Autoprefixer will be disabled
  369. in the entire `b` rule scope, not only after the comment.
  370. You can also use comments recursively:
  371. ```css
  372. /* autoprefixer: off */
  373. @supports (transition: all) {
  374. /* autoprefixer: on */
  375. a {
  376. /* autoprefixer: off */
  377. }
  378. }
  379. ```
  380. In Sass/SCSS you can use all the disable options above, add an exclamation mark
  381. in the start of comment: `/*! autoprefixer: off */`.
  382. ## Options
  383. Function `autoprefixer(options)` returns new PostCSS plugin.
  384. See [PostCSS API] for plugin usage documentation.
  385. ```js
  386. var plugin = autoprefixer({ cascade: false });
  387. ```
  388. There are 8 options:
  389. * `browsers` (array): list of browsers query (like `last 2 versions`),
  390. which are supported in your project. We recommend to use `browserslist`
  391. config or `browserslist` key in `package.json`, rather than this option
  392. to share browsers with other tools. See [Browserslist docs] for available
  393. queries and default value.
  394. * `env` (string): environment for Browserslist.
  395. * `cascade` (boolean): should Autoprefixer use Visual Cascade,
  396. if CSS is uncompressed. Default: `true`
  397. * `add` (boolean): should Autoprefixer add prefixes. Default is `true`.
  398. * `remove` (boolean): should Autoprefixer [remove outdated] prefixes.
  399. Default is `true`.
  400. * `supports` (boolean): should Autoprefixer add prefixes for `@supports`
  401. parameters. Default is `true`.
  402. * `flexbox` (boolean|string): should Autoprefixer add prefixes for flexbox
  403. properties. With `"no-2009"` value Autoprefixer will add prefixes only
  404. for final and IE versions of specification. Default is `true`.
  405. * `grid` (boolean): should Autoprefixer add IE prefixes for Grid Layout
  406. properties. Default is `true`.
  407. * `stats` (object): custom [usage statistics] for `> 10% in my stats`
  408. browsers query.
  409. Plugin object has `info()` method for debugging purpose.
  410. You can use PostCSS processor to process several CSS files
  411. to increase performance.
  412. [usage statistics]: https://github.com/ai/browserslist#custom-usage-data
  413. [PostCSS API]: https://github.com/postcss/postcss/blob/master/docs/api.md
  414. ## Debug
  415. You can check which browsers are selected and which properties will be prefixed:
  416. ```js
  417. var info = autoprefixer().info();
  418. console.log(info);
  419. ```