Autoprefixer: Stop Memorizing CSS Vendor Prefixes
Autoprefixer lets you write modern CSS and automatically adds vendor prefixes like `-webkit-` during your build. It's a standard part of frontend toolchains, ensuring styles work across browsers.
WHY IT EXISTS: Browsers often ship experimental or new CSS features with a "vendor prefix" (like -webkit- for Chrome/Safari or -moz- for Firefox). Manually tracking which properties need which prefixes for which browser versions is tedious and error-prone. Autoprefixer was created to automate this entire process.
THE MENTAL MODEL: Autoprefixer is like a meticulous proofreader for your CSS. You write your styles using the modern, standard syntax. Before your code ships to users, Autoprefixer reads it, compares every rule against a database of browser support (from Can I Use), and inserts the necessary vendor prefixes so older browsers can understand your code. You forget about prefixes entirely and let the tool handle the busywork.
HOW IT WORKS: Autoprefixer is a plugin for PostCSS, a tool for transforming CSS with JavaScript. During your project's build step, Autoprefixer scans your CSS files. Based on your configuration (often a browserslist telling it which browsers to support), it consults the Can I Use database. If a property like display: flex needs a -webkit- prefix for an older Safari version you support, it adds display: -webkit-flex automatically. It's smart enough to not add prefixes for properties that no longer need them, like border-radius.
WHEN TO USE IT: Use Autoprefixer in virtually any frontend project that requires cross-browser support. It's a standard part of modern toolchains like Create React App, Vite, and build configurations using Webpack or Gulp. It's essential for using modern CSS features like Flexbox, Grid, or animations without worrying about compatibility minutiae.
WHEN NOT TO USE IT: There's rarely a reason to avoid it in a production build process. However, you wouldn't use it if you are writing CSS for a single, known environment (like an Electron app on a fixed Chromium version) where prefixes are unnecessary. Also, remember it's a build tool; you don't run it manually on individual files.
ONE CANONICAL EXAMPLE: You write a simple rule for a placeholder's color. ::placeholder { color: gray; }
If you need to support older Firefox versions, Autoprefixer processes this during your build and outputs the following CSS to ensure compatibility, leaving your source code clean. ::-moz-placeholder { color: gray; } ::placeholder { color: gray; }
Read the original → github.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.