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