Autoprefixer

repository·main·Indexed 12 days ago

https://github.com/postcss/autoprefixer

A PostCSS plugin that parses CSS and automatically adds vendor prefixes to CSS rules using values from the Can I Use website. It ensures cross-browser compatibility by applying prefixes based on current browser popularity and property support, and includes optional support for translating modern CSS Grid syntax for IE 10 and 11. Version 10.5.4.

Tokens
4.5K
Snippets
17
Records
21
Agent score
97%

What's inside Autoprefixer

  1. What is Autoprefixer and how does it work?

    main

    Autoprefixer is a PostCSS plugin that parses CSS and adds vendor prefixes to CSS rules using values from [Can I Use]. It allows you to write standard CSS without vendor prefixes, as it automatically applies them based on current browser popularity and property support.

    Example Transformation:

    Input:

    ::placeholder {
      color: gray;
    }
    
    .image {
      width: stretch;
    }

    Output (depending on target browsers):

    ::-moz-placeholder {
      color: gray;
    }
    ::placeholder {
      color: gray;
    }
    
    .image {
      width: -moz-available;
      width: -webkit-fill-available;
      width: stretch;
    }
  2. Does Autoprefixer add polyfills?

    main

    No, Autoprefixer only adds prefixes. It does not add functional polyfills for new CSS features that require JavaScript to work.

    If you need polyfills or syntax sugar, consider these alternatives:

    • [postcss-preset-env]: A plugin preset that includes both polyfills and Autoprefixer.
    • [Oldie]: A PostCSS plugin for handling IE hacks (like opacity or rgba).
    • [postcss-flexbugs-fixes]: A PostCSS plugin to fix common Flexbox issues.
  3. How Grid Autoplacement works in IE

    main

    When grid is set to "autoplace" (via environment variable, the grid: "autoplace" option, or the /* autoprefixer grid: autoplace */ control comment), Autoprefixer adds limited autoplacement support for IE 10-11.

    Requirements for Autoplacement:

    • Both grid-template-rows and grid-template-columns must be explicitly set.
    • If grid-template or grid-template-areas are used, Autoprefixer uses area-based placement instead of autoplacement.
    • Autoplacement is only supported within an explicit grid using nth-child selectors.

    Limitations:

    • repeat(auto-fit, ...) and repeat(auto-fill, ...) are not supported and will not be IE-friendly.
    • Manual cell placement (e.g., grid-column, grid-row) or column/row spans are not allowed inside an autoplacement grid; doing so will break the layout in IE.
    • Avoid using ::before and ::after pseudo-elements inside an autoplacement grid, as IE will place them in the first grid cell, potentially displacing actual content.
    • If you change grid-gap values via media queries or classes, you must redeclare the grid columns and rows to ensure it works in IE.
    /* autoprefixer grid: autoplace */
    
    .autoplacement-example {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: auto auto;
      grid-gap: 20px;
    }
  4. Disable Autoprefixer using Control Comments

    main

    You can selectively disable Autoprefixer in your CSS using special comments:

    • /* autoprefixer: off */: Disables all translations for the entire block (before and after the comment).
    • /* autoprefixer: ignore next */: Disables Autoprefixer only for the next property, rule selector, or at-rule parameter.
    • /* autoprefixer grid: (autoplace|no-autoplace|off) */: Controls Grid translation behavior for the block.

    Example:

    .b {
      /* autoprefixer: off */
      transition: 1s; /* will not be prefixed */
    }
    
    .c {
      /* autoprefixer: ignore next */
      transition: 1s; /* will not be prefixed */
      mask: url(image.png); /* will be prefixed */
    }
    .a {
      transition: 1s; /* will be prefixed */
    }
    
    .b {
      /* autoprefixer: off */
      transition: 1s; /* will not be prefixed */
    }
    
    .c {
      /* autoprefixer: ignore next */
      transition: 1s; /* will not be prefixed */
      mask: url(image.png); /* will be prefixed */
    }
  5. Enable CSS Grid polyfilling for IE

    main

    Autoprefixer can translate modern CSS Grid syntax into IE 10 and IE 11 syntax, but this is disabled by default because it does not work in 100% of cases. If you choose to use it, you must manually enable it and test your layout thoroughly in IE.

    How to enable Grid prefixes

    You can enable this feature using one of three methods:

    1. Option: Set the grid: "autoplace" option in your Autoprefixer configuration.
    2. Control Comment: Add /* autoprefixer grid: autoplace */ to your CSS.
    3. Environment Variable: Use AUTOPREFIXER_GRID=autoplace when running your build command (e.g., AUTOPREFIXER_GRID=autoplace npm build).

    Limitations and Best Practices

    • Autoplacement: Support is limited. For better results, use grid-template or grid-template-areas instead of relying on implicit autoplacement.
    • Avoid: Do not use repeat(auto-fit, ...) or repeat(auto-fill, ...) as they are not supported in the IE polyfill.
    • Manual Placement: Manual cell placement or column/row spans are not allowed inside an autoplacement grid.
    • Pseudo-elements: Do not create ::before and ::after pseudo-elements within an autoplacement grid.
    • Grid Gap: When changing grid-gap, columns and rows must be re-declared.
    .page {
      display: grid;
      grid-gap: 33px;
      grid-template:
        'head head  head' 1fr
        'nav  main  main' minmax(100px, 1fr)
        'nav  foot  foot' 2fr /
        1fr 100px 1fr;
    }
    .page__head {
      grid-area: head;
    }
    .page__nav {
      grid-area: nav;
    }
    .page__main {
      grid-area: main;
    }
    .page__footer {
      grid-area: foot;
    }
  6. Configure target browsers using Browserslist

    main

    Autoprefixer uses [Browserslist] to determine which prefixes to add. You can specify your target browsers using queries like > 5%.

    Best Practice: Instead of passing options directly to Autoprefixer, provide your browser configuration in one of the following ways so it can be shared with other tools like babel-preset-env and Stylelint:

    1. Create a .browserslistrc file in your project root.
    2. Add a browserslist key to your package.json.

    Refer to the Browserslist documentation for specific query syntax and browser names.

  7. Manage Autoplacement in existing projects

    main

    Enabling autoplacement in established projects can break existing layouts. The safest way to integrate it is to keep it disabled by default and use control comments to enable it only for new, compatible code.

    Option 1: Disable for specific rules Use /* autoprefixer grid: no-autoplace */ to prevent Autoprefixer from attempting to autoplace elements in a specific container.

    Option 2: Enable for new code (Recommended) Keep the global setting or project default as no-autoplace, and use /* autoprefixer grid: autoplace */ only on new grid containers that follow the autoplacement requirements.

    /* Disable autoplacement to fix the issue */
    .grid {
      /* autoprefixer grid: no-autoplace */
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, 1fr);
    }
    
    /* Enable autoplacement when you want to use it in new code */
    .new-autoplace-friendly-grid {
      /* autoprefixer grid: autoplace */
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, auto);
    }
  8. Use Autoprefixer with Webpack

    main

    In Webpack, use postcss-loader within your module rules. You should also define a postcss.config.js file to specify autoprefixer as a plugin.

    // webpack.config.js
    module.exports = {
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ['style-loader', 'css-loader', 'postcss-loader']
          }
        ]
      }
    }
    
    // postcss.config.js
    module.exports = {
      plugins: [require('autoprefixer')]
    }
  9. Enable CSS Grid prefixes in Create React App

    main

    To support CSS Grid prefixes in a Create React App project, follow these steps:

    1. Install autoprefixer and cross-env:
    npm install autoprefixer@latest cross-env --save-dev
    1. Update your package.json to include last 1 ie version in the browserslist development settings:
    "browserslist": {
      "production": [
        ">0.2%",
        "not dead",
        "not op_mini all"
      ],
      "development": [
        "last 1 chrome version",
        "last 1 firefox version",
        "last 1 safari version",
        "last 1 ie version"
      ]
    }
    1. Prefix your scripts with cross-env AUTOPREFIXER_GRID=autoplace:
    "scripts": {
      "start": "cross-env AUTOPREFIXER_GRID=autoplace react-scripts start",
      "build": "cross-env AUTOPREFIXER_GRID=autoplace react-scripts build",
      "test": "cross-env AUTOPREFIXER_GRID=autoplace react-scripts test",
      "eject": "react-scripts eject"
    }

    Use no-autoplace instead of autoplace if you wish to disable autoplacement support.

  10. How to work with legacy -webkit- only code

    main

    Autoprefixer requires the unprefixed version of a property to be able to add other prefixes. If your source code only contains -webkit-gradient without the standard gradient property, Autoprefixer will not add other prefixes.

    Solution: Use [postcss-unprefix] before running Autoprefixer in your PostCSS pipeline to convert legacy prefixed code back to a standard, unprefixed state.

  11. Use Autoprefixer with CSS-in-JS (Astroturf)

    main

    To use Autoprefixer with CSS-in-JS via astroturf, add the astroturf/loader to your Webpack rules for .jsx? files and include postcss-loader for .css files. Ensure a postcss.config.js is present.

    // webpack.config.js
    module.exports = {
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ['style-loader', 'postcss-loader']
          },
          {
            test: /\.jsx?$/,
            use: ['babel-loader', 'astroturf/loader']
          }
        ]
      }
    }
    
    // postcss.config.js
    module.exports = {
      plugins: [require('autoprefixer')]
    }
  12. Run Autoprefixer via CLI

    main

    Install postcss, postcss-cli, and autoprefixer to run Autoprefixer directly from the command line using the --use flag.

    npm install postcss postcss-cli autoprefixer
    npx postcss *.css --use autoprefixer -d build/