7

Consider this:

h1 { color: red; color: blue }

Or, a more complex example (taken from a SVG file, stroke is twice):

style="fill:none;stroke:#ffffff;stroke-width:20;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke:#555555"

It seems that the answer is “it’s legal, the last assignment wins”, but I’d really like to know: Is there something written in the CSS specification about this topic?

Martin
  • 1,986
  • 15
  • 32
  • Have you tried validating it? http://jigsaw.w3.org/css-validator/ – Pavel Ronin Sep 06 '13 at 09:00
  • But why would you want to use two values for same property? – Mirage Sep 06 '13 at 09:09
  • @Mirage: I have a program that makes some *small* changes to styles in SVG files, and I’d like to avoid to use a complete CSS parser for this task that seems to be so simple. – Martin Sep 06 '13 at 09:14

2 Answers2

14

It is valid to have multiple declarations that assign a value to a property so that the assignments apply to the same element, e.g.

h1 { color: red; }
h1 { color: blue }

Combining the declarations in the same rule does not change this.

There is no explicit statement about this in CSS specifications, so it is allowed simply because there is no rule that forbids it. Multiple declarations are very common, though mostly so that they are in different rules, often even in distinct style sheets. But they can also be used within a rule. A common technique is

 p { max-width: 25em; max-width: 60ch }

which means that older browsers that do not recognize the ch unit will use the setting max-width: 25em, whereas in newer browsers, the latter declaration takes effect.

A general rule in CSS is that all other things being equal, latter declaration wins; this is part of the cascade rules. In the case of h1 { color: red; color: blue }, all other things are equal. But in h1 { color: red !important; color: blue }, the first declaration would win.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • 4
    I found it: [Cascading Order](http://www.w3.org/TR/CSS2/cascade.html#cascading-order): "4. Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins." – Martin Sep 06 '13 at 10:37
5

It is COMPLETELY VALID, h1 { color: red; color: blue } here color will simply be overridden by the next property value i.e blue

enter image description here


Even I use that while my website is in development mode, I often use border: 1px solid #f00; to create a blueprint of the page.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278