Error Duplicate attribute on the same tag

Why repeating an attribute like class or id on the same tag is invalid, and which value browsers actually use.

The validator flags any attribute name that appears more than once within a single tag.

Example

<!-- Wrong: class repeated -->
<div class="card" class="highlighted">...</div>

<!-- Right: combine into one attribute -->
<div class="card highlighted">...</div>

Why it matters

The HTML parsing spec is explicit here: when a start tag has two attributes with the same name, the parser keeps the first occurrence and discards every later one. This is easy to get backwards — most people assume the last value wins, the way it does with duplicate CSS declarations — and end up debugging why an added class or updated id silently has no effect. It usually happens when a template merges attributes from two sources without checking for an existing value first.

Frequently asked questions

Which duplicate value does the browser use — first or last?

The first one. This is the opposite of how duplicate CSS declarations resolve, which trips people up — in CSS the last declaration wins, but in HTML attribute parsing, the first occurrence of a repeated attribute wins and the rest are discarded.

How does this usually happen by accident?

Most often in templating code that builds a tag's attributes from more than one source — a base class plus a conditional class, for example — without merging them into a single attribute value first.