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