Jagger
Jagger

Reputation: 141

HTML ID attribute in close tag?

Is this code okay, where I ID a close div:

<html>
<body>
<div id="main">

</div id=main">
</body>
</html>

Upvotes: 2

Views: 4281

Answers (5)

Uncle Code Monkey
Uncle Code Monkey

Reputation: 1786

Recently having to perform maintenance on older code, I found that using comments at the end of a div tag really made it difficult to comment out large sections of code thanks to HTML not having nestable comment tags. So, I got into the habit of modifying the comments into hidden spans at the end of large block divs.

<div class="modal fade" id="dialog_edit_group">
    <div class="modal-dialog">
        <div class="modal-content">
            ...HTML content here...
        </div><span title=".modal-content" HIDDEN></span>
    </div><span title=".modal-dialog" HIDDEN></span>
</div><span title=".modal #dialog_edit_group" HIDDEN></span>
<!--
<div class="modal fade" id="dialog_edit_group_OLD">
    <div class="modal-dialog">
        <div class="modal-content">
            ...HTML content here...
        </div><span title=".modal-content" HIDDEN></span>
    </div><span title=".modal-dialog" HIDDEN></span>
</div><span title=".modal #dialog_edit_group_OLD" HIDDEN></span>
-->

I put the "HIDDEN" HTML5 attribute in there so if others modify it and add text for some reason, the contents will usually stay hidden. I made it ALL CAPS so that it would stand out a bit more as if to shout "COMMENT HERE!". Yes, it does create a DOM element that now has to be maintained by the browser, but its a small price to pay during heavy active website development.

Using "end div comments" as such conforms to the HTML standard, gives me greater readability and allows me to use the HTML comment tag to disable large blocks of the page to aid in development. Maybe it will be useful for others as well.

Upvotes: 0

Kris
Kris

Reputation: 41827

Nope, It's invalid. but you could do something like this if it's readability you're after.

<html>
    <head>
        <title></title>
    </head>
    <body>
        <div id="main">

        </div><!-- end of main -->
    </body>
</html>

Upvotes: 1

Pekka
Pekka

Reputation: 449405

It's invalid: The validator will output

Line x, Column y: End tag had attributes.

If you want to do this to avoid confusion from multiple closing tags, just use a comment to clarify which tag belongs to which element:

</div> <!-- #main -->

Upvotes: 4

SLaks
SLaks

Reputation: 887375

This is invalid syntax in all markup languages that I know of.
Closing tags cannot have attributes.

Upvotes: 2

Polynomial
Polynomial

Reputation: 28316

No. The id attribute should only be in the opening tag.

If you're doing this for readability, you might want to use HTML comments and tabified source:

<html>
    <body>
        <div id="main">

        </div>
        <!-- close main div -->
    </body>
</html>

Upvotes: 11

Related Questions