UNPKG

17.9 kBMarkdownView Raw
1# Autoprefixer [![Cult Of Martians][cult-img]][cult]
2
3<img align="right" width="94" height="71"
4 src="http://postcss.github.io/autoprefixer/logo.svg"
5 title="Autoprefixer logo by Anton Lovchikov">
6
7[PostCSS] plugin to parse CSS and add vendor prefixes to CSS rules using values
8from [Can I Use]. It is [recommended] by Google and used in Twitter and Taobao.
9
10Write your CSS rules without vendor prefixes (in fact, forget about them
11entirely):
12
13```css
14::placeholder {
15 color: gray;
16}
17```
18
19Autoprefixer will use the data based on current browser popularity and property
20support to apply prefixes for you. You can try the [interactive demo]
21of Autoprefixer.
22
23```css
24::-webkit-input-placeholder {
25 color: gray;
26}
27:-ms-input-placeholder {
28 color: gray;
29}
30::-ms-input-placeholder {
31 color: gray;
32}
33::placeholder {
34 color: gray;
35}
36```
37
38Twitter account for news and releases: [@autoprefixer].
39
40<a href="https://evilmartians.com/?utm_source=autoprefixer">
41<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
42</a>
43
44[interactive demo]: http://autoprefixer.github.io/
45[@autoprefixer]: https://twitter.com/autoprefixer
46[recommended]: https://developers.google.com/web/tools/setup/setup-buildtools#dont_trip_up_with_vendor_prefixes
47[Can I Use]: http://caniuse.com/
48[cult-img]: http://cultofmartians.com/assets/badges/badge.svg
49[PostCSS]: https://github.com/postcss/postcss
50[cult]: http://cultofmartians.com/tasks/autoprefixer-grid.html
51
52
53## Browsers
54
55Autoprefixer uses [Browserslist], so you can specify the browsers
56you want to target in your project with queries like `> 5%`
57(see [Best Practices]).
58
59The best way to provide browsers is a `.browserslistrc` file in your project
60root, or by adding a `browserslist` key to your `package.json`.
61
62We recommend the use of these options over passing options to Autoprefixer so
63that the config can be shared with other tools such as [babel-preset-env] and
64[Stylelint].
65
66See [Browserslist docs] for queries, browser names, config format, and defaults.
67
68[Browserslist docs]: https://github.com/ai/browserslist#queries
69[babel-preset-env]: https://github.com/babel/babel/tree/master/packages/babel-preset-env
70[Best Practices]: https://github.com/browserslist/browserslist#best-practices
71[Browserslist]: https://github.com/ai/browserslist
72[Stylelint]: http://stylelint.io/
73
74
75## FAQ
76
77#### Does Autoprefixer polyfill Grid Layout for IE?
78
79Autoprefixer can be used to use Grid Layout for IE 10 and IE 11, but this
80polyfill will not work in 100% of cases. This is why it is disabled by default.
81
82First, you need to enable Grid prefixes by `grid: true` option or `/* autoprefixer grid: on */` comment.
83
84Second, you need to test every fix with Grid in IE. It is not an enable and
85forget feature, but it is still very useful.
86Financial Times and Yandex use it in production.
87
88Third, there is no auto placement, so all grid cell positions must be set
89explicitly. Autoprefixer _can_ covert `grid-template` and `grid-gap`, but only
90when both are used together.
91
92```css
93.page {
94 display: grid;
95 grid-gap: 33px;
96 grid-template:
97 "head head head" 1fr
98 "nav main main" minmax(100px, 1fr)
99 "nav foot foot" 2fr /
100 1fr 100px 1fr;
101}
102.page__head {
103 grid-area: head;
104}
105.page__nav {
106 grid-area: nav;
107}
108.page__main {
109 grid-area: main;
110}
111.page__footer {
112 grid-area: foot;
113}
114```
115
116See also:
117
118* [The guide about Grids in IE and Autoprefixer].
119* [`postcss-gap-properties`] to use new `gap` property
120 instead of old `grid-gap`.
121* [`postcss-grid-kiss`] has alternate “everything in one property” syntax,
122 which make using Autoprefixer’s Grid safer.
123
124[The guide about Grids in IE and Autoprefixer]: https://css-tricks.com/css-grid-in-ie-css-grid-and-the-new-autoprefixer/
125[`postcss-gap-properties`]: https://github.com/jonathantneal/postcss-gap-properties
126[`postcss-grid-kiss`]: https://github.com/sylvainpolletvillard/postcss-grid-kiss
127
128#### No prefixes in production
129
130Many other tools contain Autoprefixer. For example, webpack uses Autoprefixer
131to minify CSS by cleaning unnecessary prefixes.
132
133If you pass your browsers to Autoprefixer using its `browsers` option, the other
134tools will use their own config, leading webpack to remove the prefixes that
135the first Autoprefixer added.
136
137To avoid this, ensure you use either the [browserslist config file] or
138`browsers` key in your `package.json`, so that all tools (Autoprefixer,
139cssnano, doiuse, cssnext, etc) use the same browsers list.
140
141[browserslist config file]: https://github.com/ai/browserslist#config-file
142
143
144#### What is the unprefixed version of `-webkit-min-device-pixel-ratio`?
145
146```css
147@media (min-resolution: 2dppx) {
148 .image {
149 background-image: url(image@2x.png);
150 }
151}
152```
153
154Will be compiled to:
155
156```css
157@media (-webkit-min-device-pixel-ratio: 2),
158 (-o-min-device-pixel-ratio: 2/1),
159 (min-resolution: 2dppx) {
160 .image {
161 background-image: url(image@2x.png);
162 }
163}
164```
165
166
167#### Does it add polyfills?
168
169No. Autoprefixer only adds prefixes.
170
171Most new CSS features will require client side JavaScript to handle a new
172behavior correctly.
173
174Depending on what you consider to be a “polyfill”, you can take a look at some
175other tools and libraries. If you are just looking for syntax sugar,
176you might take a look at:
177
178- [postcss-preset-env] is a plugins preset with polyfills and Autoprefixer
179 to write future CSS today.
180- [Oldie], a PostCSS plugin that handles some IE hacks (opacity, rgba, etc).
181- [postcss-flexbugs-fixes], a PostCSS plugin to fix flexbox issues.
182
183[postcss-flexbugs-fixes]: https://github.com/luisrudge/postcss-flexbugs-fixes
184[postcss-preset-env]: https://github.com/jonathantneal/postcss-preset-env
185[Oldie]: https://github.com/jonathantneal/oldie
186
187
188#### Why doesn’t Autoprefixer add prefixes to `border-radius`?
189
190Developers are often surprised by how few prefixes are required today.
191If Autoprefixer doesn’t add prefixes to your CSS, check if they’re still
192required on [Can I Use].
193
194[Can I Use]: http://caniuse.com/
195
196
197#### Why does Autoprefixer use unprefixed properties in `@-webkit-keyframes`?
198
199Browser teams can remove some prefixes before others, so we try to use all
200combinations of prefixed/unprefixed values.
201
202
203#### How to work with legacy `-webkit-` only code?
204
205Autoprefixer needs unprefixed property to add prefixes. So if you only
206wrote `-webkit-gradient` without W3C’s `gradient`,
207Autoprefixer will not add other prefixes.
208
209But [PostCSS] has plugins to convert CSS to unprefixed state.
210Use [postcss-unprefix] before Autoprefixer.
211
212[postcss-unprefix]: https://github.com/gucong3000/postcss-unprefix
213
214
215#### Does Autoprefixer add `-epub-` prefix?
216
217No, Autoprefixer works only with browsers prefixes from Can I Use.
218But you can use [postcss-epub]
219for prefixing ePub3 properties.
220
221[postcss-epub]: https://github.com/Rycochet/postcss-epub
222
223
224#### Why doesn’t Autoprefixer transform generic font-family `system-ui`?
225
226`system-ui` is technically not a prefix and the transformation is not
227future-proof. You can use [postcss-font-family-system-ui] to transform
228`system-ui` to a practical font-family list.
229
230[postcss-font-family-system-ui]: https://github.com/JLHwung/postcss-font-family-system-ui
231
232
233## Usage
234
235### Gulp
236
237In Gulp you can use [gulp-postcss] with `autoprefixer` npm package.
238
239```js
240gulp.task('autoprefixer', function () {
241 var postcss = require('gulp-postcss');
242 var sourcemaps = require('gulp-sourcemaps');
243 var autoprefixer = require('autoprefixer');
244
245 return gulp.src('./src/*.css')
246 .pipe(sourcemaps.init())
247 .pipe(postcss([ autoprefixer() ]))
248 .pipe(sourcemaps.write('.'))
249 .pipe(gulp.dest('./dest'));
250});
251```
252
253With `gulp-postcss` you also can combine Autoprefixer
254with [other PostCSS plugins].
255
256[gulp-postcss]: https://github.com/postcss/gulp-postcss
257[other PostCSS plugins]: https://github.com/postcss/postcss#plugins
258
259
260### Webpack
261
262In [webpack] you can use [postcss-loader] with `autoprefixer`
263and [other PostCSS plugins].
264
265```js
266module.exports = {
267 module: {
268 rules: [
269 {
270 test: /\.css$/,
271 use: ["style-loader", "css-loader", "postcss-loader"]
272 }
273 ]
274 }
275}
276```
277
278And create a `postcss.config.js` with:
279
280```js
281module.exports = {
282 plugins: [
283 require('autoprefixer')
284 ]
285}
286```
287
288[other PostCSS plugins]: https://github.com/postcss/postcss#plugins
289[postcss-loader]: https://github.com/postcss/postcss-loader
290[webpack]: http://webpack.github.io/
291
292
293### Grunt
294
295In Grunt you can use [grunt-postcss] with `autoprefixer` npm package.
296
297```js
298module.exports = function(grunt) {
299 grunt.loadNpmTasks('grunt-postcss');
300
301 grunt.initConfig({
302 postcss: {
303 options: {
304 map: true,
305 processors: [
306 require('autoprefixer')
307 ]
308 },
309 dist: {
310 src: 'css/*.css'
311 }
312 }
313 });
314
315 grunt.registerTask('default', ['postcss:dist']);
316};
317```
318
319With `grunt-postcss` you also can combine Autoprefixer
320with [other PostCSS plugins].
321
322[other PostCSS plugins]: https://github.com/postcss/postcss#plugins
323[grunt-postcss]: https://github.com/nDmitry/grunt-postcss
324
325
326### Other Build Tools:
327
328* **Ruby on Rails**: [autoprefixer-rails]
329* **Neutrino**: [neutrino-middleware-postcss]
330* **Jekyll**: add `autoprefixer-rails` and `jekyll-assets` to `Gemfile`
331* **Brunch**: [postcss-brunch]
332* **Broccoli**: [broccoli-postcss]
333* **Middleman**: [middleman-autoprefixer]
334* **Mincer**: add `autoprefixer` npm package and enable it:
335 `environment.enable('autoprefixer')`
336
337[neutrino-middleware-postcss]: https://www.npmjs.com/package/neutrino-middleware-postcss
338[middleman-autoprefixer]: https://github.com/middleman/middleman-autoprefixer
339[autoprefixer-rails]: https://github.com/ai/autoprefixer-rails
340[broccoli-postcss]: https://github.com/jeffjewiss/broccoli-postcss
341[postcss-brunch]: https://github.com/iamvdo/postcss-brunch
342
343
344### Preprocessors
345
346* **Less**: [less-plugin-autoprefix]
347* **Stylus**: [autoprefixer-stylus]
348* **Compass**: [autoprefixer-rails#compass]
349
350[less-plugin-autoprefix]: https://github.com/less/less-plugin-autoprefix
351[autoprefixer-stylus]: https://github.com/jenius/autoprefixer-stylus
352[autoprefixer-rails#compass]: https://github.com/ai/autoprefixer-rails#compass
353
354
355### CSS-in-JS
356
357There is [postcss-js] to use Autoprefixer in React Inline Styles, [Free Style],
358Radium and other CSS-in-JS solutions.
359
360```js
361let prefixer = postcssJs.sync([ autoprefixer ]);
362let style = prefixer({
363 display: 'flex'
364});
365```
366
367[postcss-js]: https://github.com/postcss/postcss-js
368[Free Style]: https://github.com/blakeembrey/free-style
369
370
371### GUI Tools
372
373* [CodeKit](https://codekitapp.com/help/autoprefixer/)
374* [Prepros](https://prepros.io)
375
376
377### CLI
378
379You can use the [postcss-cli] to run Autoprefixer from CLI:
380
381```sh
382npm install postcss-cli autoprefixer
383npx postcss *.css --use autoprefixer -d build/
384```
385
386See `postcss -h` for help.
387
388[postcss-cli]: https://github.com/postcss/postcss-cli
389
390
391### JavaScript
392
393You can use Autoprefixer with [PostCSS] in your Node.js application
394or if you want to develop an Autoprefixer plugin for a new environment.
395
396```js
397var autoprefixer = require('autoprefixer');
398var postcss = require('postcss');
399
400postcss([ autoprefixer ]).process(css).then(function (result) {
401 result.warnings().forEach(function (warn) {
402 console.warn(warn.toString());
403 });
404 console.log(result.css);
405});
406```
407
408There is also a [standalone build] for the browser or for a non-Node.js runtime.
409
410You can use [html-autoprefixer] to process HTML with inlined CSS.
411
412[html-autoprefixer]: https://github.com/RebelMail/html-autoprefixer
413[standalone build]: https://raw.github.com/ai/autoprefixer-rails/master/vendor/autoprefixer.js
414[PostCSS]: https://github.com/postcss/postcss
415
416
417### Text Editors and IDE
418
419Autoprefixer should be used in assets build tools. Text editor plugins are not
420a good solution, because prefixes decrease code readability and you will need
421to change values in all prefixed properties.
422
423I recommend you to learn how to use build tools like [Gulp].
424They work much better and will open you a whole new world of useful plugins
425and automation.
426
427If you can’t move to a build tool, you can use text editor plugins:
428
429* [Sublime Text](https://github.com/sindresorhus/sublime-autoprefixer)
430* [Brackets](https://github.com/mikaeljorhult/brackets-autoprefixer)
431* [Atom Editor](https://github.com/sindresorhus/atom-autoprefixer)
432* [Visual Studio](https://github.com/madskristensen/WebCompiler)
433
434[Gulp]: http://gulpjs.com/
435
436
437## Warnings
438
439Autoprefixer uses the [PostCSS warning API] to warn about really important
440problems in your CSS:
441
442* Old direction syntax in gradients.
443* Old unprefixed `display: box` instead of `display: flex`
444 by latest specification version.
445
446You can get warnings from `result.warnings()`:
447
448```js
449result.warnings().forEach(function (warn) {
450 console.warn(warn.toString());
451});
452```
453
454Every Autoprefixer runner should display these warnings.
455
456[PostCSS warning API]: https://github.com/postcss/postcss/blob/master/docs/api.md#warning-class
457
458
459## Disabling
460
461### Prefixes
462
463Autoprefixer was designed to have no interface – it just works.
464If you need some browser specific hack just write a prefixed property
465after the unprefixed one.
466
467```css
468a {
469 transform: scale(0.5);
470 -moz-transform: scale(0.6);
471}
472```
473
474If some prefixes were generated incorrectly, please create an [issue on GitHub].
475
476[issue on GitHub]: https://github.com/postcss/autoprefixer/issues
477
478
479### Features
480
481You can use these plugin options to disable some of Autoprefixer’s features.
482
483* `grid: true` will enable `-ms-` prefixes for Grid Layout.
484* `supports: false` will disable `@supports` parameters prefixing.
485* `flexbox: false` will disable flexbox properties prefixing.
486 Or `flexbox: "no-2009"` will add prefixes only for final and IE
487 versions of specification.
488* `remove: false` will disable cleaning outdated prefixes.
489
490You should set them to the plugin:
491
492```js
493autoprefixer({ grid: true });
494```
495
496
497### Control Comments
498
499If you do not need Autoprefixer in some part of your CSS,
500you can use control comments to disable Autoprefixer.
501
502```css
503.a {
504 transition: 1s; /* will be prefixed */
505}
506
507.b {
508 /* autoprefixer: off */
509 transition: 1s; /* will not be prefixed */
510}
511
512.c {
513 /* autoprefixer: ignore next */
514 transition: 1s; /* will not be prefixed */
515 mask: url(image.png); /* will be prefixed */
516}
517```
518
519There are three types of control comments:
520
521* `/* autoprefixer: off */` disable the whole block *before* and after comment.
522* `/* autoprefixer: ignore next */` disable only next property
523 or next rule selector or at-rule parameters (but not rule/at‑rule body).
524* `/* autoprefixer grid: on */` enable grid option. Use `off` to disable this option.
525
526You can also use comments recursively:
527
528```css
529/* autoprefixer: off */
530@supports (transition: all) {
531 /* autoprefixer: on */
532 a {
533 /* autoprefixer: off */
534 }
535}
536```
537
538
539## Options
540
541Function `autoprefixer(options)` returns a new PostCSS plugin.
542See [PostCSS API] for plugin usage documentation.
543
544```js
545autoprefixer({ cascade: false })
546```
547
548Available options are:
549
550* `env` (string): environment for Browserslist.
551* `cascade` (boolean): should Autoprefixer use Visual Cascade,
552 if CSS is uncompressed. Default: `true`
553* `add` (boolean): should Autoprefixer add prefixes. Default is `true`.
554* `remove` (boolean): should Autoprefixer [remove outdated] prefixes.
555 Default is `true`.
556* `supports` (boolean): should Autoprefixer add prefixes for `@supports`
557 parameters. Default is `true`.
558* `flexbox` (boolean|string): should Autoprefixer add prefixes for flexbox
559 properties. With `"no-2009"` value Autoprefixer will add prefixes only
560 for final and IE versions of specification. Default is `true`.
561* `grid` (boolean): should Autoprefixer add IE prefixes for Grid Layout
562 properties. Default is `false`. You can also use `/* autoprefixer grid: on */`
563 comment in CSS.
564* `stats` (object): custom [usage statistics] for `> 10% in my stats`
565 browsers query.
566* `browsers` (array): list of queries for target browsers. Try to not use it.
567  The best practice is to use `.browserslistrc` config
568 or `browserslist` key in `package.json` to share target browsers
569 with Babel, ESLint and Stylelint. See [Browserslist docs]
570 for available queries and default value.
571* `ignoreUnknownVersions` (boolean): do not raise error on unknown browser
572 version in Browserslist config or `browsers` option. Default is `false`.
573
574Plugin object has `info()` method for debugging purpose.
575
576You can use PostCSS processor to process several CSS files
577to increase performance.
578
579[usage statistics]: https://github.com/ai/browserslist#custom-usage-data
580[PostCSS API]: http://api.postcss.org
581
582
583## Debug
584
585Run `npx autoprefixer --info` in your project directory to check
586which browsers are selected and which properties will be prefixed:
587
588```
589$ npx autoprefixer --info
590Browsers:
591 Edge: 16
592
593These browsers account for 0.26% of all users globally
594
595At-Rules:
596 @viewport: ms
597
598Selectors:
599 ::placeholder: ms
600
601Properties:
602 appearance: webkit
603 flow-from: ms
604 flow-into: ms
605 hyphens: ms
606 overscroll-behavior: ms
607 region-fragment: ms
608 scroll-snap-coordinate: ms
609 scroll-snap-destination: ms
610 scroll-snap-points-x: ms
611 scroll-snap-points-y: ms
612 scroll-snap-type: ms
613 text-size-adjust: ms
614 text-spacing: ms
615 user-select: ms
616```
617
618JS API is also available:
619
620```js
621var info = autoprefixer().info();
622console.log(info);
623```