Can an HTML element have multiple ids?

HtmlXhtmlStandards Compliance

Html Problem Overview


I understand that an id must be unique within an HTML/XHTML page.

For a given element, can I assign multiple ids to it?

<div id="nested_element_123 task_123"></div>

I realize I have an easy solution with simply using a class. I'm just curious about using ids in this manner.

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

Contrary to what everyone else said, the correct answer is YES

The Selectors spec is very clear about this:

> If an element has multiple ID attributes, all of them must be treated as IDs for that element for the purposes of the ID selector.Such a situation could be reached using mixtures of xml:id, DOM3 Core, XML DTDs, and namespace-specific knowledge.


Edit

Just to clarify: Yes, an XHTML element can have multiple ids, e.g.

<p id="foo" xml:id="bar">

but assigning multiple ids to the same id attribute using a space-separated list is not possible.

Solution 3 - Html

My understanding has always been:

  • IDs are single use and are only applied to one element...

    • Each is attributed as a unique identifier to (only) one single element.
  • Classes can be used more than once...

    • They can therefore be applied to more than one element, and similarly yet different, there can be more than one class (i.e., multiple classes) per element.

Solution 4 - Html

No. While the definition from W3C for HTML 4 doesn't seem to explicitly cover your question, the definition of the name and id attribute says no spaces in the identifier:

> ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Solution 5 - Html

No. Every DOM element, if it has an id, has a single, unique id. You could approximate it using something like:

<div id='enclosing_id_123'><span id='enclosed_id_123'></span></div>

and then use navigation to get what you really want.

If you are just looking to apply styles, class names are better.

Solution 6 - Html

You can only have one ID per element, but you can indeed have more than one class. But don't have multiple class attributes; put multiple class values into one attribute.

<div id="foo" class="bar baz bax">

is perfectly legal.

Solution 7 - Html

No, you cannot have multiple ids for a single tag, but I have seen a tag with a name attribute and an id attribute which are treated the same by some applications.

Solution 8 - Html

No, you should use nested DIVs if you want to head down that path. Besides, even if you could, imagine the confusion it would cause when you run document.getElementByID(). What ID is it going to grab if there are multiple ones?

On a slightly related topic, you can add multiple classes to a DIV. See Eric Myers discussion at,

http://meyerweb.com/eric/articles/webrev/199802a.html

Solution 9 - Html

I'd like to say technically yes, since really what gets rendered is technically always browser-dependent. Most browsers try to keep to the specifications as best they can and as far as I know there is nothing in the CSS specifications against it. I'm only going to vouch for the actual HTML, CSS, and JavaScript code that gets sent to the browser before any other interpreter steps in.

However, I also say no since every browser I typically test on doesn't actually let you.

If you need to see for yourself, save the following as a .html file and open it up in the major browsers. In all browsers I tested on, the JavaScript function will not match to an element. However, remove either "hunkojunk" from the id tag and all works fine.

Sample Code
<html>
<head>
</head>
<body>
    <p id="hunkojunk1 hunkojunk2"></p>

    <script type="text/javascript">
        document.getElementById('hunkojunk2').innerHTML = "JUNK JUNK JUNK JUNK JUNK JUNK";
    </script>
</body>
</html>

Solution 10 - Html

Any ID assigned to a div element is unique. However, you can assign multiple IDs "under", and not "to" a div element. In that case, you have to represent those IDs as <span></span> IDs.

Say, you want two links in the same HTML page to point to the same div element in the page.

<p><a href="#exponentialEquationsCalculator">Exponential Equations</a></p>

<p><a href="#logarithmicExpressionsCalculator"><Logarithmic Expressions</a></p>
Point to the same section of the page
<!-- Exponential / Logarithmic Equations Calculator -->
<div class="w3-container w3-card white w3-margin-bottom">
   <span id="exponentialEquationsCalculator"></span>
   <span id="logarithmicEquationsCalculator"></span>
</div>

Solution 11 - Html

From 7.5.2 Element identifiers: the id and class attributes:

> The id attribute assigns a unique identifier to an element (which may > be verified by an SGML parser).

and

> ID and NAME tokens must begin with a letter ([A-Za-z]) and may be > followed by any number of letters, digits ([0-9]), hyphens ("-"), > underscores ("_"), colons (":"), and periods (".").

So "id" must be unique and can't contain a space.

Solution 12 - Html

The simple answer is no, as others have said before me. An element can't have more than one ID and an ID can't be used more than once in a page. Try it out and you'll see how well it doesn't work.

In response to tvanfosson's answer regarding the use of the same ID in two different elements. As far as I'm aware, an ID can only be used once in a page regardless of whether it's attached to a different tag.

By definition, an element needing an ID should be unique, but if you need two ID's then it's not really unique and needs a class instead.

Solution 13 - Html

No.

Having said that, there's nothing to stop you doing it. But you'll get inconsistent behaviour with the various browsers. Don't do it. One ID per element.

If you want multiple assignations to an element use classes (separated by a space).

Solution 14 - Html

That's interesting, but as far as I know the answer is a firm no. I don't see why you need a nested ID, since you'll usually cross it with another element that has the same nested ID. If you don't there's no point, if you do there's still very little point.

Solution 15 - Html

Classes are specially made for this, and here is the code from which you can understand it:

<html>
<head>
    <style type="text/css">
     .personal{
            height:100px;
            width: 100px;

        }
    .fam{
            border: 2px solid #ccc;
        }
    .x{
            background-color:#ccc;
        }

    </style>
</head>

<body>
    <div class="personal fam x"></div>
</body>
</html>

Solution 16 - Html

Nay.

From 3.2.3.1 The id attribute:

> The value must not contain any space characters.

id="a b" <-- find the space character in that VaLuE.

That said, you can style multiple IDs. But if you're following the specification, the answer is no.

Solution 17 - Html

ID's should be unique, so you should only use a particular ID once on a page. Classes may be used repeatedly.

Check HTML id Attribute (W3Schools) for more details.

Solution 18 - Html

I don´t think you can have two Id´s but it should be possible. Using the same id twice is a different case... like two people using the same passport. However one person could have multiple passports... Came looking for this since I have a situation where a single employee can have several functions. Say "sysadm" and "team coordinator" having the id="sysadm teamcoordinator" would let me reference them from other pages so that employees.html#sysadm and employees.html#teamcoordinator would lead to the same place... One day somebody else might take over the team coordinator function while the sysadm remains the sysadm... then I only have to change the ids on the employees.html page ... but like I said - it doesn´t work :(

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwebmatView Question on Stackoverflow
Solution 1 - HtmltimmowView Answer on Stackoverflow
Solution 2 - Htmluser123444555621View Answer on Stackoverflow
Solution 3 - HtmlRossView Answer on Stackoverflow
Solution 4 - HtmlacrosmanView Answer on Stackoverflow
Solution 5 - HtmltvanfossonView Answer on Stackoverflow
Solution 6 - HtmlAmbroseChapelView Answer on Stackoverflow
Solution 7 - HtmltpowerView Answer on Stackoverflow
Solution 8 - HtmlAnjisanView Answer on Stackoverflow
Solution 9 - HtmlJamesView Answer on Stackoverflow
Solution 10 - HtmlSamdom For PeaceView Answer on Stackoverflow
Solution 11 - HtmlAlexandrView Answer on Stackoverflow
Solution 12 - HtmlTaylorView Answer on Stackoverflow
Solution 13 - HtmlSnowcrashView Answer on Stackoverflow
Solution 14 - HtmlRobert KView Answer on Stackoverflow
Solution 15 - Htmluser3266067View Answer on Stackoverflow
Solution 16 - HtmlcorysimmonsView Answer on Stackoverflow
Solution 17 - HtmlAgboola FeyikemiView Answer on Stackoverflow
Solution 18 - HtmlOle Reidar JohansenView Answer on Stackoverflow