UNPKG

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