If you use experimental CSS on your sites, demos, articles or tools, there is a certain amount of maintenance required to keep on top of changes. With a number of recent changes in the browser world, now is the time to revisit and update your code. Read on to find out what may need updating.
Reorder your prefixes
There is no specific order that you should place your prefixed properties. However, a commonly recommended method is to place them in alphabetical order, with the prefixless version last. This would make the order: moz, ms, o, webkit, then prefixless.
Opera recently added support for -webkit- prefixes for a limited number of CSS properties. This is needed as a number of developers choose to only include the webkit prefix, or don’t include the opera prefix. As both the -o- prefix and -webkit- prefixes are counted as the same specificity, order wins out and the webkit prefix will be used if both are specified. You probably want the o prefix to be used for Opera, especially if you have to override something, so a useful suggestion is to order them in oder of the length of the prefix, like so:
This also has the benefit that it looks relatively neat, and its easy to spot if you have missed one once you are used to using this scheme. Each property gets one character shorter, except the initial webkit prefix.
This is one of the least critical updates to make in this post, but can prove to be useful.
Remove redundant prefixes
A number of commonly used CSS3 properties have been available without prefix for quite a while now, such as border-radius. In the recent past quite a few CSS specs have reached the stage where it is now considered ok for browser vendors to remove the prefixes. This includes Transforms, Transitions, Animations, Multi-Column Layout, Flexbox, and Image Values (primarily gradients). Different browsers are at a different state in the march towards removing prefixes, but some are ready to be removed, depending on how far back you have to support older browsers.
Internet Explorer and Opera have supported box-sizing without prefix since they were implemented, so the only prefixes you need to consider are for Gecko and WebKit based browsers. Firefox still needs to use the -moz- prefix to this day, but you can drop the WebKit prefix if you don’t need to support:
Chrome 9 and below (~0.30% marketshare)
Safari 5 and below (~0.74% marketshare)
Android 3 browser and below (> 60% active Android devices)
The situation with box-shadow is almost exactly as for box-sizing in the previous section, except Firefox only needed its prefix in versions 3.5 and 3.6. Together, these current account for 0.77% marketshare.
-webkit-box-shadow: value; /*evaluate removing as Android users upgrade or box-shadow usage is not critical */
I would personally remove -webkit-box-shadow as it is not usually a critical piece of styling, unlike box-sizing, which could break layout.
As with the previous two properties, Opera and Internet Explorer have supported border-radius without prefix since implementation. You can drop the -webkit- and -moz- prefixes if you do not need to support the following:
Firefox 3.6 and below (~0.95% marketshare)
Chrome 4 and below (~0.05% marketshare)
Safari 4 and below (~0.12% marketshare)
Android 2.1 and below (~3% active Android devices)
Safari on iOS 3.2 and below
The newer specs
The bad news is that you still need to use -webkit- in modern versions of WebKit for all of the specs in this section. The good news is that you do not need prefixes for gradients, transitions, transforms and animations starting from:
Internet Explorer 10
In addition, prefixes have never been needed for all the specs above (except 2D transforms) in IE. 3D transforms are not implemented in Opera, but it is safe to assume it will be directly implemented without prefix.
I’d recommend to use the -webkit-, -moz-, -o- and the unprefixed version (taking into account the notes below for gradients), with the exception of keeping the -ms- prefix for 2d transforms and dropping the -o- prefix for 3d transforms. You may be able to consider removing the Firefox and Opera prefixes in the relatively near future, as users are upgrading at a fast clip, and these browsers don’t have the problem of the near glacial speed that Android devices update their OS.
Multi-column layout was implemented without prefixes in IE and Opera. Prefixes are still needed in Firefox and all versions of WebKit.
Flexbox has a storied history (see later in this post) and thus needs multiple different syntaxes and prefixes, but is supported by modern versions of all browsers. It is implemented without prefix in Opera 12.1.
Add Prefixless properties
As the specs mentioned are now ready to be implemented without prefix, it is also time for web developers to add the prefixless version to their code. In theory, these properties should not change now they have reached this strange, and there is general consensus in the CSS working group.
There are two schools of thought between developers. The first is to include the prefixless version when writing the original CSS. If you are in this school, you will just need to remove any redundant prefixes, but make sure you check the next section, as there have been breaking changes. The other school of thought is to not include the prefixless version. If you’re in this school, you’ll have to add all of these now. You’ll also need to check the next section to see how to do this.
Update to the new syntax
This is perhaps the most critical part of your spring clean. Especially if you are a tools developer, or have tutorials with a lot of foot traffic.
It is probably well known by web developers that border-radius changed to an alternative syntax from the original found in Firefox 2. It is probably also known that a similar thing happened with gradients, replacing the original Safari proposal. What is less known, and is certainly less updated in the wild, is that the syntax for gradients has changed again. Unless you changed your gradient code relatively recently, you’ll likely have a bunch of outdated code that needs to be updated.
New gradient syntax
Fortunately the new gradient syntax is easy to update to, and you only really need to make the update to the unprefixed version. You can keep all the prefixed properties with the old syntax. All of the latest browsers support this new syntax, with the exception of WebKit. Its true, WebKit is lagging IE!
The general rule is that if you use a direction keyword it needs to be reversed and be preceded by the to keyword. Thus:
If you use an angle, the starting point of the gradient line has shifted. Previously 0deg started at the East position. Angles increased in an anticlockwise direction, such that 90deg started at the North position. With the unprefixed syntax the gradient line for 0deg starts at the North position and the angle increases clockwise. The formula for switching from the old the new degree value is abs(oldDegValue − 450) mod 360.
For radial gradients the position keyword or length now needs to be preceded by the at keyword:
-radial-gradient(50px 25px at center, white, black);
New flexbox syntax
The Flexbox syntax has changed more times than I can count. Fortunately, there is likely to be less content out there using the old syntax as there is with gradients, due to the fact the site will likely break if flexbox isn’t supported.
Currently, only Opera supports the new syntax without prefix. Chrome supports the new syntax with prefix since Chrome 21, Firefox is due to support the new syntax unprefixed in Firefox 20, but currently only supports the old syntax. IE supports a slightly older syntax, but not the same one as the older WebKit and Firefox implementations. Safari currently only supports the old syntax. To get the best cross platform compatibility you will need to include:
The latest syntax unprefixed: display: flex and friends. (Opera, Firefox 20, and probably Chrome in the near future)
The latest syntax with -webkit- prefix: display: -webkit-flex and friends (Chrome 21). You may want to drop this and rely on old syntax until WebKit goes prefixless
The old new syntax with -ms- prefix: display: -ms-flexbox and friends (IE10)
The old syntax with -moz- and -webkit- prefixes: display: -webkit-box (WebKit), display: -moz-box (Firefox
The spec is too involved to include a comparison tacked onto the end of this post, but you can read about how to spot the difference and see some demos of the old old and new new syntax on CSS Tricks. Chris does not include a demo of the old new (or is it new old) syntax, so you can look at Microsoft’s Flexbox documentation to find out more. I may do a full blog post on this in the near future if I find time.