UNPKG

28.7 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 Alibaba.
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]: https://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]: https://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## Contents
54
55- [Browsers](#browsers)
56- [FAQ](#faq)
57 - [Does Autoprefixer polyfill Grid Layout for IE?](#does-autoprefixer-polyfill-grid-layout-for-ie)
58 - [No prefixes in production](#no-prefixes-in-production)
59 - [What is the unprefixed version of `-webkit-min-device-pixel-ratio`?](#what-is-the-unprefixed-version-of--webkit-min-device-pixel-ratio)
60 - [Does it add polyfills?](#does-it-add-polyfills)
61 - [Why doesn’t Autoprefixer add prefixes to `border-radius`?](#why-doesnt-autoprefixer-add-prefixes-to-border-radius)
62 - [Why does Autoprefixer use unprefixed properties in `@-webkit-keyframes`?](#why-does-autoprefixer-use-unprefixed-properties-in--webkit-keyframes)
63 - [How to work with legacy `-webkit-` only code?](#how-to-work-with-legacy--webkit--only-code)
64 - [Does Autoprefixer add `-epub-` prefix?](#does-autoprefixer-add--epub--prefix)
65 - [Why doesn’t Autoprefixer transform generic font-family `system-ui`?](#why-doesnt-autoprefixer-transform-generic-font-family-system-ui)
66- [Usage](#usage)
67 - [Gulp](#gulp)
68 - [Webpack](#webpack)
69 - [CSS-in-JS](#css-in-js)
70 - [CLI](#cli)
71 - [Other Build Tools](#other-build-tools)
72 - [JavaScript](#javascript)
73 - [Text Editors and IDE](#text-editors-and-ide)
74- [Warnings](#warnings)
75- [Disabling](#disabling)
76- [Options](#options)
77- [Grid Autoplacement support in IE](#grid-autoplacement-support-in-ie)
78- [Debug](#debug)
79
80## Browsers
81
82Autoprefixer uses [Browserslist], so you can specify the browsers
83you want to target in your project with queries like `> 5%`
84(see [Best Practices]).
85
86The best way to provide browsers is a `.browserslistrc` file in your project
87root, or by adding a `browserslist` key to your `package.json`.
88
89We recommend the use of these options over passing options to Autoprefixer so
90that the config can be shared with other tools such as [babel-preset-env] and
91[Stylelint].
92
93See [Browserslist docs] for queries, browser names, config format, and defaults.
94
95[Browserslist docs]: https://github.com/ai/browserslist#queries
96[babel-preset-env]: https://github.com/babel/babel/tree/master/packages/babel-preset-env
97[Best Practices]: https://github.com/browserslist/browserslist#best-practices
98[Browserslist]: https://github.com/ai/browserslist
99[Stylelint]: https://stylelint.io/
100
101
102## FAQ
103
104### Does Autoprefixer polyfill Grid Layout for IE?
105
106Autoprefixer can be used to translate modern CSS Grid syntax into IE 10 and IE 11 syntax,
107but this polyfill will not work in 100% of cases. This is why it is disabled by default.
108
109First, you need to enable Grid prefixes by using either the `grid: "autoplace"` option or the `/* autoprefixer grid: autoplace */` control comment.
110
111Second, you need to test every fix with Grid in IE. It is not an enable and
112forget feature, but it is still very useful.
113Financial Times and Yandex use it in production.
114
115Third, there is only very limited auto placement support. Read the [Grid Autoplacement support in IE](#grid-autoplacement-support-in-ie) section for more details.
116
117Fourth, if you are not using the autoplacement feature, the best way to use Autoprefixer is by using `grid-template` or `grid-template-areas`.
118
119```css
120.page {
121 display: grid;
122 grid-gap: 33px;
123 grid-template:
124 "head head head" 1fr
125 "nav main main" minmax(100px, 1fr)
126 "nav foot foot" 2fr /
127 1fr 100px 1fr;
128}
129.page__head {
130 grid-area: head;
131}
132.page__nav {
133 grid-area: nav;
134}
135.page__main {
136 grid-area: main;
137}
138.page__footer {
139 grid-area: foot;
140}
141```
142
143See also:
144
145* [The guide about Grids in IE and Autoprefixer].
146* [`postcss-gap-properties`] to use new `gap` property
147 instead of old `grid-gap`.
148* [`postcss-grid-kiss`] has alternate “everything in one property” syntax,
149 which makes using Autoprefixer’s Grid translations safer.
150
151[The guide about Grids in IE and Autoprefixer]: https://css-tricks.com/css-grid-in-ie-css-grid-and-the-new-autoprefixer/
152[`postcss-gap-properties`]: https://github.com/jonathantneal/postcss-gap-properties
153[`postcss-grid-kiss`]: https://github.com/sylvainpolletvillard/postcss-grid-kiss
154
155### No prefixes in production
156
157Many other tools contain Autoprefixer. For example, webpack uses Autoprefixer
158to minify CSS by cleaning unnecessary prefixes.
159
160If you pass your browsers to Autoprefixer using its `browsers` option, the other
161tools will use their own config, leading webpack to remove the prefixes that
162the first Autoprefixer added.
163
164To avoid this, ensure you use either the [browserslist config file] or
165`browsers` key in your `package.json`, so that all tools (Autoprefixer,
166cssnano, doiuse, cssnext, etc) use the same browsers list.
167
168[browserslist config file]: https://github.com/ai/browserslist#config-file
169
170
171### What is the unprefixed version of `-webkit-min-device-pixel-ratio`?
172
173```css
174@media (min-resolution: 2dppx) {
175 .image {
176 background-image: url(image@2x.png);
177 }
178}
179```
180
181Will be compiled to:
182
183```css
184@media (-webkit-min-device-pixel-ratio: 2),
185 (-o-min-device-pixel-ratio: 2/1),
186 (min-resolution: 2dppx) {
187 .image {
188 background-image: url(image@2x.png);
189 }
190}
191```
192
193
194### Does it add polyfills?
195
196No. Autoprefixer only adds prefixes.
197
198Most new CSS features will require client side JavaScript to handle a new
199behavior correctly.
200
201Depending on what you consider to be a “polyfill”, you can take a look at some
202other tools and libraries. If you are just looking for syntax sugar,
203you might take a look at:
204
205- [postcss-preset-env] is a plugins preset with polyfills and Autoprefixer
206 to write future CSS today.
207- [Oldie], a PostCSS plugin that handles some IE hacks (opacity, rgba, etc).
208- [postcss-flexbugs-fixes], a PostCSS plugin to fix flexbox issues.
209
210[postcss-flexbugs-fixes]: https://github.com/luisrudge/postcss-flexbugs-fixes
211[postcss-preset-env]: https://github.com/jonathantneal/postcss-preset-env
212[Oldie]: https://github.com/jonathantneal/oldie
213
214
215### Why doesn’t Autoprefixer add prefixes to `border-radius`?
216
217Developers are often surprised by how few prefixes are required today.
218If Autoprefixer doesn’t add prefixes to your CSS, check if they’re still
219required on [Can I Use].
220
221[Can I Use]: https://caniuse.com/
222
223
224### Why does Autoprefixer use unprefixed properties in `@-webkit-keyframes`?
225
226Browser teams can remove some prefixes before others, so we try to use all
227combinations of prefixed/unprefixed values.
228
229
230### How to work with legacy `-webkit-` only code?
231
232Autoprefixer needs unprefixed property to add prefixes. So if you only
233wrote `-webkit-gradient` without W3C’s `gradient`,
234Autoprefixer will not add other prefixes.
235
236But [PostCSS] has plugins to convert CSS to unprefixed state.
237Use [postcss-unprefix] before Autoprefixer.
238
239[postcss-unprefix]: https://github.com/gucong3000/postcss-unprefix
240
241
242### Does Autoprefixer add `-epub-` prefix?
243
244No, Autoprefixer works only with browsers prefixes from Can I Use.
245But you can use [postcss-epub]
246for prefixing ePub3 properties.
247
248[postcss-epub]: https://github.com/Rycochet/postcss-epub
249
250
251### Why doesn’t Autoprefixer transform generic font-family `system-ui`?
252
253`system-ui` is technically not a prefix and the transformation is not
254future-proof. You can use [postcss-font-family-system-ui] to transform
255`system-ui` to a practical font-family list.
256
257[postcss-font-family-system-ui]: https://github.com/JLHwung/postcss-font-family-system-ui
258
259
260## Usage
261
262### Gulp
263
264In Gulp you can use [gulp-postcss] with `autoprefixer` npm package.
265
266```js
267gulp.task('autoprefixer', function () {
268 var postcss = require('gulp-postcss');
269 var sourcemaps = require('gulp-sourcemaps');
270 var autoprefixer = require('autoprefixer');
271
272 return gulp.src('./src/*.css')
273 .pipe(sourcemaps.init())
274 .pipe(postcss([ autoprefixer() ]))
275 .pipe(sourcemaps.write('.'))
276 .pipe(gulp.dest('./dest'));
277});
278```
279
280With `gulp-postcss` you also can combine Autoprefixer
281with [other PostCSS plugins].
282
283[gulp-postcss]: https://github.com/postcss/gulp-postcss
284[other PostCSS plugins]: https://github.com/postcss/postcss#plugins
285
286
287### Webpack
288
289In [webpack] you can use [postcss-loader] with `autoprefixer`
290and [other PostCSS plugins].
291
292```js
293module.exports = {
294 module: {
295 rules: [
296 {
297 test: /\.css$/,
298 use: ["style-loader", "css-loader", "postcss-loader"]
299 }
300 ]
301 }
302}
303```
304
305And create a `postcss.config.js` with:
306
307```js
308module.exports = {
309 plugins: [
310 require('autoprefixer')
311 ]
312}
313```
314
315[other PostCSS plugins]: https://github.com/postcss/postcss#plugins
316[postcss-loader]: https://github.com/postcss/postcss-loader
317[webpack]: http://webpack.github.io/
318
319
320### CSS-in-JS
321
322The best way to use PostCSS with CSS-in-JS is [`astroturf`].
323Add it’s loader to your `webpack.config.js`:
324
325```js
326module.exports = {
327 module: {
328 rules: [
329 {
330 test: /\.css$/,
331 use: ['style-loader', 'postcss-loader'],
332 },
333 {
334 test: /\.jsx?$/,
335 use: ['babel-loader', 'astroturf/loader'],
336 }
337 ]
338 }
339}
340```
341
342Then create `postcss.config.js`:
343
344```js
345module.exports = {
346 plugins: [
347 require('autoprefixer')
348 ]
349}
350```
351
352[`astroturf`]: https://github.com/4Catalyzer/astroturf
353
354
355### CLI
356
357You can use the [postcss-cli] to run Autoprefixer from CLI:
358
359```sh
360npm install postcss-cli autoprefixer
361npx postcss *.css --use autoprefixer -d build/
362```
363
364See `postcss -h` for help.
365
366[postcss-cli]: https://github.com/postcss/postcss-cli
367
368
369### Other Build Tools
370
371* **Grunt:** [grunt-postcss]
372* **Ruby on Rails**: [autoprefixer-rails]
373* **Neutrino**: [neutrino-middleware-postcss]
374* **Jekyll**: add `autoprefixer-rails` and `jekyll-assets` to `Gemfile`
375* **Brunch**: [postcss-brunch]
376* **Broccoli**: [broccoli-postcss]
377* **Middleman**: [middleman-autoprefixer]
378* **Mincer**: add `autoprefixer` npm package and enable it:
379 `environment.enable('autoprefixer')`
380
381[neutrino-middleware-postcss]: https://www.npmjs.com/package/neutrino-middleware-postcss
382[middleman-autoprefixer]: https://github.com/middleman/middleman-autoprefixer
383[autoprefixer-rails]: https://github.com/ai/autoprefixer-rails
384[broccoli-postcss]: https://github.com/jeffjewiss/broccoli-postcss
385[postcss-brunch]: https://github.com/iamvdo/postcss-brunch
386[grunt-postcss]: https://github.com/nDmitry/grunt-postcss
387
388
389#### Preprocessors
390
391* **Less**: [less-plugin-autoprefix]
392* **Stylus**: [autoprefixer-stylus]
393* **Compass**: [autoprefixer-rails#compass]
394
395[less-plugin-autoprefix]: https://github.com/less/less-plugin-autoprefix
396[autoprefixer-stylus]: https://github.com/jenius/autoprefixer-stylus
397[autoprefixer-rails#compass]: https://github.com/ai/autoprefixer-rails#compass
398
399
400#### GUI Tools
401
402* [CodeKit](https://codekitapp.com/help/autoprefixer/)
403* [Prepros](https://prepros.io)
404
405
406### JavaScript
407
408You can use Autoprefixer with [PostCSS] in your Node.js application
409or if you want to develop an Autoprefixer plugin for a new environment.
410
411```js
412var autoprefixer = require('autoprefixer');
413var postcss = require('postcss');
414
415postcss([ autoprefixer ]).process(css).then(function (result) {
416 result.warnings().forEach(function (warn) {
417 console.warn(warn.toString());
418 });
419 console.log(result.css);
420});
421```
422
423There is also a [standalone build] for the browser or for a non-Node.js runtime.
424
425You can use [html-autoprefixer] to process HTML with inlined CSS.
426
427[html-autoprefixer]: https://github.com/RebelMail/html-autoprefixer
428[standalone build]: https://raw.github.com/ai/autoprefixer-rails/master/vendor/autoprefixer.js
429[PostCSS]: https://github.com/postcss/postcss
430
431
432### Text Editors and IDE
433
434Autoprefixer should be used in assets build tools. Text editor plugins are not
435a good solution, because prefixes decrease code readability and you will need
436to change values in all prefixed properties.
437
438I recommend you to learn how to use build tools like [Gulp].
439They work much better and will open you a whole new world of useful plugins
440and automation.
441
442If you can’t move to a build tool, you can use text editor plugins:
443
444* [Sublime Text](https://github.com/sindresorhus/sublime-autoprefixer)
445* [Brackets](https://github.com/mikaeljorhult/brackets-autoprefixer)
446* [Atom Editor](https://github.com/sindresorhus/atom-autoprefixer)
447* [Visual Studio](https://github.com/madskristensen/WebCompiler)
448
449[Gulp]: https://gulpjs.com/
450
451
452## Warnings
453
454Autoprefixer uses the [PostCSS warning API] to warn about really important
455problems in your CSS:
456
457* Old direction syntax in gradients.
458* Old unprefixed `display: box` instead of `display: flex`
459 by latest specification version.
460
461You can get warnings from `result.warnings()`:
462
463```js
464result.warnings().forEach(function (warn) {
465 console.warn(warn.toString());
466});
467```
468
469Every Autoprefixer runner should display these warnings.
470
471[PostCSS warning API]: https://github.com/postcss/postcss/blob/master/docs/api.md#warning-class
472
473
474## Disabling
475
476### Prefixes
477
478Autoprefixer was designed to have no interface – it just works.
479If you need some browser specific hack just write a prefixed property
480after the unprefixed one.
481
482```css
483a {
484 transform: scale(0.5);
485 -moz-transform: scale(0.6);
486}
487```
488
489If some prefixes were generated incorrectly, please create an [issue on GitHub].
490
491[issue on GitHub]: https://github.com/postcss/autoprefixer/issues
492
493
494### Features
495
496You can use these plugin options to control some of Autoprefixer’s features.
497
498* `grid: "autoplace"` will enable `-ms-` prefixes for Grid Layout including some
499 [limited autoplacement support](#grid-autoplacement-support-in-ie).
500* `supports: false` will disable `@supports` parameters prefixing.
501* `flexbox: false` will disable flexbox properties prefixing.
502 Or `flexbox: "no-2009"` will add prefixes only for final and IE
503 versions of specification.
504* `remove: false` will disable cleaning outdated prefixes.
505
506You should set them inside the plugin like so:
507
508```js
509autoprefixer({ grid: "autoplace" });
510```
511
512
513### Control Comments
514
515If you do not need Autoprefixer in some part of your CSS,
516you can use control comments to disable Autoprefixer.
517
518```css
519.a {
520 transition: 1s; /* will be prefixed */
521}
522
523.b {
524 /* autoprefixer: off */
525 transition: 1s; /* will not be prefixed */
526}
527
528.c {
529 /* autoprefixer: ignore next */
530 transition: 1s; /* will not be prefixed */
531 mask: url(image.png); /* will be prefixed */
532}
533```
534
535There are three types of control comments:
536
537* `/* autoprefixer: (on|off) */`: enable/disable all Autoprefixer translations for the
538 whole block both *before* and *after* the comment.
539* `/* autoprefixer: ignore next */`: disable Autoprefixer only for the next property
540 or next rule selector or at-rule parameters (but not rule/at‑rule body).
541* `/* autoprefixer grid: (autoplace|no-autoplace|off) */`: control how Autoprefixer handles
542 grid translations for the whole block:
543 * `autoplace`: enable grid translations with autoplacement support.
544 * `no-autoplace`: enable grid translations with autoplacement support *disabled*.
545 (alias for deprecated value `on`)
546 * `off`: disable all grid translations.
547
548You can also use comments recursively:
549
550```css
551/* autoprefixer: off */
552@supports (transition: all) {
553 /* autoprefixer: on */
554 a {
555 /* autoprefixer: off */
556 }
557}
558```
559
560Note that comments that disable the whole block should not be featured in the same
561block twice:
562
563```css
564/* How not to use block level control comments */
565
566.do-not-do-this {
567 /* autoprefixer: off */
568 transition: 1s;
569 /* autoprefixer: on */
570 transform: rotate(20deg);
571}
572```
573
574
575## Options
576
577Function `autoprefixer(options)` returns a new PostCSS plugin.
578See [PostCSS API] for plugin usage documentation.
579
580```js
581autoprefixer({ cascade: false })
582```
583
584Available options are:
585
586* `env` (string): environment for Browserslist.
587* `cascade` (boolean): should Autoprefixer use Visual Cascade,
588 if CSS is uncompressed. Default: `true`
589* `add` (boolean): should Autoprefixer add prefixes. Default is `true`.
590* `remove` (boolean): should Autoprefixer [remove outdated] prefixes.
591 Default is `true`.
592* `supports` (boolean): should Autoprefixer add prefixes for `@supports`
593 parameters. Default is `true`.
594* `flexbox` (boolean|string): should Autoprefixer add prefixes for flexbox
595 properties. With `"no-2009"` value Autoprefixer will add prefixes only
596 for final and IE versions of specification. Default is `true`.
597* `grid` (false|"autoplace"|"no-autoplace"): should Autoprefixer add IE prefixes for Grid Layout
598 properties?
599 * `false` (default): prevent Autoprefixer from outputting CSS Grid translations.
600 * `"autoplace"`: enable Autoprefixer grid translations and *include* autoplacement
601 support. You can also use `/* autoprefixer grid: autoplace */` in your CSS.
602 * `"no-autoplace"`: enable Autoprefixer grid translations but *exclude* autoplacement
603 support. You can also use `/* autoprefixer grid: no-autoplace */` in your CSS.
604 (alias for the deprecated `true` value)
605* `stats` (object): custom [usage statistics] for `> 10% in my stats`
606 browsers query.
607* `browsers` (array): list of queries for target browsers. Try to not use it.
608  The best practice is to use `.browserslistrc` config
609 or `browserslist` key in `package.json` to share target browsers
610 with Babel, ESLint and Stylelint. See [Browserslist docs]
611 for available queries and default value.
612* `ignoreUnknownVersions` (boolean): do not raise error on unknown browser
613 version in Browserslist config or `browsers` option. Default is `false`.
614
615Plugin object has `info()` method for debugging purpose.
616
617You can use PostCSS processor to process several CSS files
618to increase performance.
619
620[usage statistics]: https://github.com/ai/browserslist#custom-usage-data
621[PostCSS API]: http://api.postcss.org
622
623## Grid Autoplacement support in IE
624
625If the `grid` option is set to `"autoplace"`, limited autoplacement support is added to Autoprefixers grid translations. You can also use the `/* autoprefixer grid: autoplace */` control comment to enable autoplacement
626
627Autoprefixer will only autoplace grid cells if both `grid-template-rows` and `grid-template-columns` has been set. If `grid-template` or `grid-template-areas` has been set, Autoprefixer will use area based cell placement instead.
628
629Autoprefixer supports autoplacement by using `nth-child` CSS selectors. It creates [number of columns] x [number of rows] `nth-child` selectors. For this reason Autoplacement is only supported within the explicit grid.
630
631```css
632/* Input CSS */
633
634/* autoprefixer grid: autoplace */
635
636.autoplacement-example {
637 display: grid;
638 grid-template-columns: 1fr 1fr;
639 grid-template-rows: auto auto;
640 grid-gap: 20px;
641}
642```
643
644```css
645/* Output CSS */
646
647/* autoprefixer grid: autoplace */
648
649.autoplacement-example {
650 display: -ms-grid;
651 display: grid;
652 -ms-grid-columns: 1fr 20px 1fr;
653 grid-template-columns: 1fr 1fr;
654 -ms-grid-rows: auto 20px auto;
655 grid-template-rows: auto auto;
656 grid-gap: 20px;
657}
658
659.autoplacement-example > *:nth-child(1) {
660 -ms-grid-row: 1;
661 -ms-grid-column: 1;
662}
663
664.autoplacement-example > *:nth-child(2) {
665 -ms-grid-row: 1;
666 -ms-grid-column: 3;
667}
668
669.autoplacement-example > *:nth-child(3) {
670 -ms-grid-row: 3;
671 -ms-grid-column: 1;
672}
673
674.autoplacement-example > *:nth-child(4) {
675 -ms-grid-row: 3;
676 -ms-grid-column: 3;
677}
678```
679
680### Beware of enabling autoplacement in old projects
681
682Be careful about enabling autoplacement in any already established projects that have
683previously not used Autoprefixer's grid autoplacement feature before.
684
685If this was your html:
686
687```html
688<div class="grid">
689 <div class="grid-cell"></div>
690</div>
691```
692
693The following CSS will not work as expected with the autoplacement feature enabled:
694
695```css
696/* Unsafe CSS when Autoplacement is enabled */
697
698.grid-cell {
699 grid-column: 2;
700 grid-row: 2;
701}
702
703.grid {
704 display: grid;
705 grid-template-columns: repeat(3, 1fr);
706 grid-template-rows: repeat(3, 1fr);
707}
708```
709
710Swapping the rules around will not fix the issue either:
711
712```css
713/* Also unsafe to use this CSS */
714
715.grid {
716 display: grid;
717 grid-template-columns: repeat(3, 1fr);
718 grid-template-rows: repeat(3, 1fr);
719}
720
721.grid-cell {
722 grid-column: 2;
723 grid-row: 2;
724}
725```
726
727One way to deal with this issue is to disable autoplacement in the
728grid-declaration rule:
729
730```css
731/* Disable autoplacement to fix the issue */
732
733.grid {
734 /* autoprefixer grid: no-autoplace */
735 display: grid;
736 grid-template-columns: repeat(3, 1fr);
737 grid-template-rows: repeat(3, 1fr);
738}
739
740.grid-cell {
741 grid-column: 2;
742 grid-row: 2;
743}
744```
745
746The absolute best way to integrate autoplacement into already existing projects though is
747to leave autoplacement turned off by default and then use a control comment to enable it
748when needed. This method is far less likely to cause something on the site to break.
749
750```css
751/* Disable autoplacement by default in old projects */
752/* autoprefixer grid: no-autoplace */
753
754/* Old code will function the same way it always has */
755.old-grid {
756 display: grid;
757 grid-template-columns: repeat(3, 1fr);
758 grid-template-rows: repeat(3, 1fr);
759}
760.old-grid-cell {
761 grid-column: 2;
762 grid-row: 2;
763}
764
765/* Enable autoplacement when you want to use it in new code */
766.new-autoplace-friendly-grid {
767 /* autoprefixer grid: autoplace */
768 display: grid;
769 grid-template-columns: repeat(3, 1fr);
770 grid-template-rows: repeat(3, auto);
771}
772```
773
774Note that the `grid: "no-autoplace"` setting and the
775`/* autoprefixer grid: no-autoplace */` control comment share identical functionality
776to the `grid: true` setting and the `/* autoprefixer grid: on */` control comment.
777There is no need to refactor old code to use `no-autoplace` in place of the old
778`true` and `on` statements.
779
780### Autoplacement limitations
781
782#### Both columns and rows must be defined
783
784Autoplacement only works inside the explicit grid. The columns and rows need to be defined
785so that Autoprefixer knows how many `nth-child` selectors to generate.
786
787```css
788.not-allowed {
789 display: grid;
790 grid-template-columns: repeat(3, 1fr);
791}
792
793.is-allowed {
794 display: grid;
795 grid-template-columns: repeat(3, 1fr);
796 grid-template-rows: repeat(10, auto);
797}
798```
799
800#### Repeat auto-fit and auto-fill are not supported
801
802The `repeat(auto-fit, ...)` and `repeat(auto-fill, ...)` grid functionality relies on
803knowledge from the browser about screen dimensions and the number of available grid
804items for it to work properly. Autoprefixer does not have access to this information
805so unfortunately this little snippet will _never_ be IE friendly.
806
807```css
808.grid {
809 /* This will never be IE friendly */
810 grid-template-columns: repeat(auto-fit, min-max(200px, 1fr))
811}
812```
813
814#### No manual cell placement or column/row spans allowed inside an autoplacement grid
815
816Elements must not be manually placed or given column/row spans inside an autoplacement
817grid. Only the most basic of autoplacement grids are supported. Grid cells can still be
818placed manually outside the the explicit grid though. Support for manually placing
819individual grid cells inside an explicit autoplacement grid is planned for a
820future release.
821
822```css
823.autoplacement-grid {
824 display: grid;
825 grid-template-columns: repeat(3, 1fr);
826 grid-template-rows: repeat(3, auto);
827}
828
829/*
830 grid cells placed inside the explicit grid
831 will break the layout in IE
832*/
833.not-permitted-grid-cell {
834 grid-column: 1;
835 grid-row: 1;
836}
837
838/*
839 grid cells placed outside the
840 explicit grid will work in IE
841*/
842.permitted-grid-cell {
843 grid-column: 1 / span 2;
844 grid-row: 4;
845}
846```
847
848If manual cell placement is required, we recommend using `grid-template` or
849`grid-template-areas` instead:
850
851```css
852.page {
853 display: grid;
854 grid-gap: 30px;
855 grid-template:
856 "head head"
857 "nav main" minmax(100px, 1fr)
858 "foot foot" /
859 200px 1fr;
860}
861.page__head {
862 grid-area: head;
863}
864.page__nav {
865 grid-area: nav;
866}
867.page__main {
868 grid-area: main;
869}
870.page__footer {
871 grid-area: foot;
872}
873```
874
875#### Do not create `::before` and `::after` pseudo elements
876
877Let's say you have this HTML:
878
879```html
880<div class="grid">
881 <div class="grid-cell"></div>
882</div>
883```
884
885And you write this CSS:
886
887```css
888.grid {
889 display: grid;
890 grid-template-columns: 1fr 1fr;
891 grid-template-rows: auto;
892}
893
894.grid::before {
895 content: 'before';
896}
897
898.grid::after {
899 content: 'after';
900}
901```
902
903This will be the output:
904
905```css
906.grid {
907 display: -ms-grid;
908 display: grid;
909 -ms-grid-columns: 1fr 1fr;
910 grid-template-columns: 1fr 1fr;
911 -ms-grid-rows: auto;
912 grid-template-rows: auto;
913}
914
915.grid > *:nth-child(1) {
916 -ms-grid-row: 1;
917 -ms-grid-column: 1;
918}
919
920
921.grid > *:nth-child(2) {
922 -ms-grid-row: 1;
923 -ms-grid-column: 2;
924}
925
926.grid::before {
927 content: 'before';
928}
929
930.grid::after {
931 content: 'after';
932}
933```
934
935IE will place `.grid-cell`, `::before` and `::after` in row 1 column 1.
936Modern browsers on the other hand will place `::before` in row 1 column 1,
937`.grid-cell` in row 1 column 2, and `::after` in row 2 column 1.
938
939See this [Code Pen](https://codepen.io/daniel-tonon/pen/gBymVw) to see a visualization
940of the issue. View the Code Pen in both a modern browser and IE to see the difference.
941
942Note that you can still create `::before` and `::after` elements as long as you manually
943place them outside the explicit grid.
944
945#### When changing the `grid gap` value, columns and rows must be re-declared
946
947If you wish to change the size of a `grid-gap`, you will need to redeclare the grid columns and rows.
948
949```css
950.grid {
951 display: grid;
952 grid-template-columns: 1fr 1fr;
953 grid-template-rows: auto;
954 grid-gap: 50px;
955}
956
957/* This will *NOT* work in IE */
958@media (max-width: 600px) {
959 .grid {
960 grid-gap: 20px;
961 }
962}
963
964/* This will *NOT* work in IE */
965.grid.small-gap {
966 grid-gap: 20px;
967}
968```
969
970```css
971.grid {
972 display: grid;
973 grid-template-columns: 1fr 1fr;
974 grid-template-rows: auto;
975 grid-gap: 50px;
976}
977
978/* This *WILL* work in IE */
979@media (max-width: 600px) {
980 .grid {
981 grid-template-columns: 1fr 1fr;
982 grid-template-rows: auto;
983 grid-gap: 20px;
984 }
985}
986
987/* This *WILL* work in IE */
988.grid.small-gap {
989 grid-template-columns: 1fr 1fr;
990 grid-template-rows: auto;
991 grid-gap: 20px;
992}
993```
994
995## Debug
996
997Run `npx autoprefixer --info` in your project directory to check
998which browsers are selected and which properties will be prefixed:
999
1000```
1001$ npx autoprefixer --info
1002Browsers:
1003 Edge: 16
1004
1005These browsers account for 0.26% of all users globally
1006
1007At-Rules:
1008 @viewport: ms
1009
1010Selectors:
1011 ::placeholder: ms
1012
1013Properties:
1014 appearance: webkit
1015 flow-from: ms
1016 flow-into: ms
1017 hyphens: ms
1018 overscroll-behavior: ms
1019 region-fragment: ms
1020 scroll-snap-coordinate: ms
1021 scroll-snap-destination: ms
1022 scroll-snap-points-x: ms
1023 scroll-snap-points-y: ms
1024 scroll-snap-type: ms
1025 text-size-adjust: ms
1026 text-spacing: ms
1027 user-select: ms
1028```
1029
1030JS API is also available:
1031
1032```js
1033var info = autoprefixer().info();
1034console.log(info);
1035```