You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1099 lines
29 KiB

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