Prefix free: Break free from CSS vendor prefix hell!

# -prefix-free

## Break free from CSS prefix hell!

Only 2KB gzipped

-prefix-free lets you use only unprefixed CSS properties everywhere. It works behind the scenes, adding the current browser’s prefix to any CSS code, only when it’s needed.

> “[-prefix-free is] fantastic, top-notch work! Thank you for creating and sharing it.”

> — Eric Meyer

# Features

- Processes every stylesheet in `<link>` or `<style>` elements and adds a vendor prefix where needed
- Processes elements with a `style` attribute and adds a vendor prefix where needed
- Takes care of new `<link>` or `<style>` elements, `style` attribute changes and CSSOM changes (requires plugin)
- Lets jQuery’s `.css()` method get and set unprefixed properties (requires plugin)

# Limitations

- Prefixing code in `@import`-ed files is not supported
- Prefixing cross-origin linked stylesheets is not supported, unless they are CORS-enabled
- Unprefixed linked stylesheets won’t work locally in Chrome and Opera. You can change that for yourself though.
- Unprefixed values in inline styles (in the `style` attribute) won’t work in IE and Firefox < 3.6. Properties as well in Firefox < 3.6.

# Demo?

Check this page’s stylesheet ;-)

You can also visit the Test Drive page, type in any code you want and check out how it would get prefixed for the current browser.

# How to use

Just include `prefixfree.js` anywhere in your page. It is recommended to put it right after the stylesheets, to minimize FOUC

That’s it, you’re done!

# Browser support

The target browser support is IE9+, Opera 10+, Firefox 3.5+, Safari 4+ and Chrome on desktop and Mobile Safari, Android browser, Chrome and Opera Mobile on mobile.

If it doesn’t work in any of those, it’s a bug so please report it. Just before you do, please make sure that it’s not because the browser doesn’t support a CSS3 feature at all, even with a prefix.

In older browsers like IE8, nothing will break, just properties won’t get prefixed. Which wouldn’t be useful anyway as IE8 doesn’t support much CSS3 ;)

# Test drive

Test the prefixing that -prefix-free would do for this browser, by writing some CSS below:

@keyframes rotate { from { transform: rotate(15deg) } to { transform: rotate(375deg) } } .download { position: absolute; top: 1em; left: -1.5em; width: 6em; height: 6em; padding: 1em 0; background: #80A060; background-image: linear-gradient(90deg, transparent, rgba(10,0,0,.3)),linear-gradient(transparent, transparent); color: white; line-height: 1; font-size: 140%; text-align: center; text-decoration: initial; text-shadow: .08em .08em .2em rgba(0,0,0,.6); border-radius: 50%; box-shadow: .1em .2em .4em -.2em black; box-sizing: border-box; transform: rotate(15deg); animation: rotate; cursor: zoom-in; } :read-write {}

# FAQ

# What if I actually want to target only a specific prefix?

Properties/values etc that already have a prefix won’t be altered. However, if you use the unprefixed property/value etc after the prefixed properties, you might run into issue #16. For those cases, -prefix-free adds a class on the root element (the `<html>` element in HTML) with the same name as the current prefix. So for example, to solve the problem presented in the above issue, you could do:

    .myselector {
        transform: rotate(15deg);
    }
    .-webkit- .myselector {
        transform: rotate(15deg) rotateX(0);
    }

It’s not ideal, but it’s a solution, until a more intuitive way to deal with these cases is added in -prefix-free.

Please note that in unsupported browsers like IE8, no such class will be added.

# How do I exclude specific files from being prefixed?

You can exclude a file from being prefixed by adding the `data-noprefix` attribute to the `<link>` or `<style>` element. For example:

    <link href="//example.com/style.css" rel="stylesheet" type="text/css" data-noprefix>

# How do I enable local testing?

Firefox (and IE?) natively support local XHR, so -prefix-free will work fine locally with them.

To enable local XHR for Chrome, you need to run it with the flag `--allow-file-access-from-files`.

To enable local XHR for Opera, you have to go to opera:config#UserPrefs|AllowFileXMLHttpRequest, check that option and Save.

# Something like this belongs to the server-side

A server side script would need to add all prefixes, making the size of the CSS file considerably larger. Also, it should maintain a list of features that need prefixes, or add them all and unnecessarily bloat the stylesheet. -prefix-free automatically detects what needs a prefix and what doesn’t.

Also, a common argument against doing this on the client side, is that it makes the design rely on JavaScript. However, that is completely false: If JS is disabled, only some of the CSS3 won’t show. But if your design relies on CSS3 to be functional, you have bigger problems bro.

You can read some more about this in my recent CSS Tricks interview

But every solution has its own pros and cons. If you would feel more comfortable with a server-side script, use that and don’t troll me please. Remember: nobody forced you to use -prefix-free. KTHXBAI ☺

# Plugins

Extra code on top of -prefix-free that makes it more flexible, integrates it with different APIs etc

# Dynamic DOM plugin

Originally a part of -prefix-free, it’s now a separate plugin. It makes -prefix-free take care of:

- New `<link>` and `<style>` added to the document afterwards
- New elements with a `style` attribute added to the document afterwards
- `style` attribute changes through `setAttribute()` (except in Webkit)
- Getting and setting unprefixed properties through the CSSOM, like:

      element.style.transform = 'rotate(10deg)';

Things to be aware of:

- Mutation events have a reputation of being slow, and they are the only way to do stuff when an attribute changes or new elements get added
- `style` attribute modifications will not work in Webkit
- Setting unprefixed CSSOM properties, like:

      element.style.transform = 'rotate(5deg)';

  will not work in Chrome (reading will)

Get the Dynamic DOM plugin now:

- prefixfree.dynamic-dom.js
- prefixfree.dynamic-dom.min.js

# jQuery plugin

A tiny plugin (I didn’t even bother minifying it as it’s so small) that lets you set/get unprefixed CSS properties through jQuery's `.css` method.

Get the jQuery plugin now:

- prefixfree.jquery.js

# Viewport-relative units

A static polyfill for the new vw, vh, vmin, vmax units.

- Will do nothing if the units are supported natively.
- It’s dynamic. Pixel values are updated on screen resize.

- prefixfree.viewport-units.js

# CSS Variables

Enables rudimentary CSS variables support.

- If a prefixed implementation is available, it will add the necessary prefixes and use that
- It supports overwriting the variable value

Limitations:

- Doesn’t support scoping. Every variable is in the global scope and every subsequent declaration of the same variable overwrites the previous ones, regardless of scoping
- It’s not dynamic. Variables are replaced once, and then they function as normal CSS values

- prefixfree.vars.js

Tweet