Mark
Mark

Reputation: 2822

Multiple "style" attributes in a "span" tag: what's supposed to happen?

Consider the following HTML fragment with two style attributes:

<span style="color:blue" style="font-style:italic">Test</span>

In Opera 12.16 and Chrome 40, it shows up as blue non-italic text, while Internet Explorer 9 shows blue italic text. What, if anything, does the standard say is supposed to show up?

Upvotes: 25

Views: 132190

Answers (2)

user764357
user764357

Reputation:

In HTML, SGML and XML, (1) attributes cannot be repeated, and should only be defined in an element once.

So your example:

<span style="color:blue" style="font-style:italic">Test</span>

is non-conformant to the HTML standard, and will result in undefined behaviour, which explains why different browsers are rendering it differently.


Since there is no defined way to interpret this, browsers can interpret it however they want and merge them, or ignore them as they wish.

(1): Every article I can find states that attributes are "key/value" pairs or "attribute-value" pairs, heavily implying the keys must be unique. The best source I can find states:

Attribute names (id and status in this example) are subject to the same restrictions as other names in XML; they need not be unique across the whole DTD, however, but only within the list of attributes for a given element. (Emphasis mine.)

Upvotes: 33

Eeji
Eeji

Reputation: 1648

Separate your rules with a semi colon in a single declaration:

<span style="color:blue;font-style:italic">Test</span>

Upvotes: 53

Related Questions