Can an HTML Element Have the Same Attribute Twice

Can an HTML element have the same attribute twice?

It is not valid to have the same attribute name twice in an element. The authoritative references for this are somewhat complicated, as old HTML versions were nominally based on SGML and the restriction is implied by a normative reference to the SGML standard. In HTML5 PR, section 8.1.2.3 Attributes explicitly says: “There must never be two or more attributes on the same start tag whose names are an ASCII case-insensitive match for each other.”

What happens in practice is that the latter attribute is ignored. Well, future browsers might do otherwise. In the DOM, attributes appear as properties of the element node as well as in the attributes object, so there would be no natural way to store two values.

Can multiple different HTML elements have the same ID if they're different elements?

No.

Element IDs should be unique within the entire document.

Adding more than one class html

Yes, it is possible, but you can only declare the class attribute once per HTML element. Just separate the classes you want to apply by a space.

<a href="#" class="class1 class2">My Text</a>

If you declare the class attribute more than once, all definitions beyond the first will be ignored, so in your code .class2 will not be applied to your link.

HTML element with double attribute

If this is processed as XHTML it will be not-well-formed and throw an error and I would expect any conformant HTML parser to do this.

Can an HTML element have multiple ids?

No. From the XHTML 1.0 Spec

In XML, fragment identifiers are of
type ID, and there can only be a
single attribute of type ID per
element. Therefore, in XHTML 1.0 the
id attribute is defined to be of type
ID. In order to ensure that XHTML 1.0
documents are well-structured XML
documents, XHTML 1.0 documents MUST
use the id attribute when defining
fragment identifiers on the elements
listed above. See the HTML
Compatibility Guidelines for
information on ensuring such anchors
are backward compatible when serving
XHTML documents as media type
text/html.

How to add the same attribute multiple times to an Element Tag in XML

You can't. Attribute names are unique per element.

If you need to have multiple bits of data under the same name, then the usual solutions are either a space separated list or child elements.

<event department="foo bar baz" />

or

<event>
<department>foo</department>
<department>bar</department>
<department>baz</department>
</event>


Related Topics



Leave a reply



Submit