7

In order to achieve cross-browser compatibility, we tend to use both vendor specific extensions and standard CSS3 syntax. I know CSS3 is still in draft, but we have already started using it. But the question is, does the order of where they occur matter very much?

For example, lets see here

-moz-border-radius: 10px;
border-radius: 10px;

This applies browser specific border-radius and then falls back to standard method, the later will hopeful be ignored, but still.

Similarly, switching their order

border-radius: 10px;
-moz-border-radius: 10px;

Now, this tries standard syntax first and then falls back to browser based extension.

It there any difference caused by the ordering? May be in terms of performance or else.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Starx
  • 77,474
  • 47
  • 185
  • 261
  • __P.S.__ Is not a duplicate of http://stackoverflow.com/questions/8131846/why-do-browsers-create-vendor-prefixes-for-css-properties :) – Starx Sep 21 '12 at 10:08

3 Answers3

7

Now, this tries standard syntax first and then falls back to browser based extension.

This may be a misleading statement. A compliant browser will try the standard unprefixed property first, but if it also supports the prefixed property in addition to the standard, then it will apply that prefix as well. This usually results in the standard declaration being overridden by the prefixed declaration and a browser's potentially non-standard implementation of that property, defeating the purpose of having the standard property there in the first place.

The reason why you should declare the unprefixed property last is because that's how properties cascade in a rule: a browser will always use the last applicable one. Prefixed and unprefixed versions of a property are treated as the same property with respect to the cascade, so you want a browser to do its best to adhere to the standards when applying that property.1

If a browser implements a prefix but not the standard, that's fine, but if it implements both, you want to ensure it uses the standard instead. You do this by declaring the standard property last.


1 As far as I'm aware this is not dictated by the spec, because as far as the spec is concerned vendor extensions are non-standard and so their implementation cannot be described. Although the syntax of vendor prefixes is described in the spec, implementations are left entirely up to the discretion of vendors.

However it's an agreed-upon convention by most browser developers when implementing prefixed versions of a to-be-standardized property or rule, to always treat both prefixed and unprefixed versions as aliases of each other.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • "Vendor-specific extensions" have been part of the CSS spec for quite a long time: http://www.w3.org/TR/CSS21/syndata.html#vendor-keywords – Rob Sep 21 '12 at 14:18
  • 1) That's the CSS2.1 spec 2) That only describes the syntax, not the implementation. The syntax is defined for implementers to use when creating non-standard properties. These prefixed properties are not part of the standard and are entirely left up to vendors to implement however they like. The spec doesn't say how the prefixed properties themselves should be implemented. – BoltClock Sep 21 '12 at 14:19
  • And where there's syntax there must be an implementation. EDIT: Oh, wait. I get it. Perhaps you are saying the implementation is non-standard and not vendor prefixes. – Rob Sep 21 '12 at 14:19
  • Yes, I'll edit my answer to make this clearer. Sorry for the confusion. – BoltClock Sep 21 '12 at 14:22
  • Are you sure about the prefixed and unprefixed version both working as aliases to each other? I am not entirely sure all browsers would do this. Could you please refer me, your source of research? – Starx Sep 21 '12 at 17:29
  • @Starx: I've had trouble finding a source because the spec doesn't say this at all, but as far as testing goes, major browsers handle prefixed and unprefixed versions of a property the same way. Again, it's implementation-dependent, and from what I've seen merely a convention: for example even though Mozilla used to call it `-moz-border-radius-topleft`, it treats `border-top-left-radius` as the same property (before dropping the prefixed version altogether in recent versions). – BoltClock Sep 21 '12 at 17:40
2

When writing CSS3 properties, the modern wisdom is to list the "real" property last and the vendor prefixes first.

Another thing to think about when you do include the non-prefixed property is to put it after the vendor-prefixed versions. When a browser implements the standard version of a property, as specified in the relevant CSS3 specification, you most likely want it to use that implementation instead of the experimental, browser-specific version (which it will likely still support to be backwards-compatible). Putting it last should ensure that it will override the vendor-prefixed implementation.

See Ordering CSS3 Properties

See also : Remember non-vendor-prefixed CSS 3 properties (and put them last)

Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51
  • Yes, I know that. But the question is, why?? What is the reason behind this modern wisdom? – Starx Sep 21 '12 at 10:19
  • On your quote `override the vendor-prefixed implementation`, that is one of my concerns. Does this raises performance issues? – Starx Sep 21 '12 at 17:33
2

The order of prefixes doesn't matter, as long as you keep the future standard version as last.

If a browser drops support for the prefix, it will simply ignore the rule and execute the standard version.

ps: same as A.K. but simpler, so you don't have to read all pages.

Mark
  • 6,762
  • 1
  • 33
  • 50
  • This question is specifically talking about the order of standard vs vendor properties. So, *why* should the standard version be last? Also, browsers typically support *both* the vendor-specific and standard versions of a property for quite some time. *If* they dropped support, it wouldn't matter which order the properties were in. – 0b10011 Sep 21 '12 at 14:39
  • Because CSS is based on cascading, so the last rule would overrule the previous one? Therefore the standard version should go after the prefixed one. – Mark Sep 21 '12 at 15:14
  • 1
    I wasn't asking for me (I would been the one to write an answer had no other answers been written already). I simply believe this should be clarified in your answer, especially since that's specifically what the OP was asking. Your answer says *what* to do, but not *why* to do it. – 0b10011 Sep 21 '12 at 16:37
  • @bfrohs, I completely agree with you. Mark should try to answer the question as Why first. – Starx Sep 21 '12 at 17:32