I often write comments like this

<div id="wrapper">
...
</div> <!-- id="wrapper" -->

But what if I wrote them like this?

<div id="wrapper">
...
</div id="wrapper">

At first it seems dangerous to put an id attribute that has the same value on another tag, but then, doesn't the browser throw out that information? As far as I can see Chrome and Firefox both disregard any "attributes" in the closing tag.

Also seems unfortunate that it's not a real comment. It wont be parsed semantically as a comment. But I don't think anyone is using that space for anything else, right? Closing tags only close, right?

Is there any reason not to do this other than it's not a standard?

  • Does it serve any useful purpose? – Robert Harvey Sep 21 '15 at 5:08
  • If you really want the association, grab an editor that will highlight same level closing tags for you. – Mario Sep 21 '15 at 5:33
  • This is like writing in C++: if (A) oneLiner(); else if (B) oneLiner(); else oneLiner(); Is that a if else with another if else in else clause or an if/else if/else? It will probably be interpretted correctly, but you don't leave these things to chance. – Neil Sep 21 '15 at 7:16
  • Why do you want to do it? It sounds like a pointless question - but it's important. If it's to aid others at development-time (i.e. keep track of which opening tag each closing tag belongs to) - there may be IDE plugins that can offer info on closing tags. – JᴀʏMᴇᴇ Sep 21 '15 at 8:09
  • 1
    I sometimes use a comment such as <!-- /wrapper --> to mark the end of the "wrapper" section, for example. Obviously / has no special significance in the comment, but it is short and easy to understand. – Brandin Sep 22 '15 at 11:15
up vote 10 down vote accepted

This scenario is not covered in the spec: http://www.w3.org/html/wg/drafts/html/master/syntax.html#end-tags

Moreover, the next section in the spec (Attributes) claims that attributes must be in start tags.

Since your proposal doesn't respect the spec, I strongly recommend against it.

  • 2
    It just takes a browser to scan the document for id attributes outside of a comment in order to create a potential problem. Don't assume that the parser that the browser uses will discard superfluous syntax. It may simply be a series of sophisticated regular expressions. – Neil Sep 21 '15 at 7:08

Along with the section of the HTML Standard that defines the syntax for a valid end tag, as cited in another answer to this question, the HTML Standard also defines the presence of attributes in end tags as a parse error that error-reporting parsers should report.

See the beginning of the Tokenization section of the HTML parsing algorithm, which says:

When an end tag token is emitted with attributes, that is a parse error

(Note that the word emitted here means in the product of the tokenization phase, which emits tokens that are then consumed by the Tree Construction phase.)

Also in the spirit of having a record of information available at point of use here at StackOverflow when possible—rather than requiring users to follow links to the get the information—here are the details from the section of the HTML Standard that defines the syntax for valid end tag:

  1. The first character of an end tag must be a U+003C LESS-THAN SIGN character (<).
  2. The second character of an end tag must be a U+002F SOLIDUS character (/).
  3. The next few characters of an end tag must be the element's tag name.
  4. After the tag name, there may be one or more space characters.
  5. Finally, end tags must be closed by a U+003E GREATER-THAN SIGN character (>).

Somewhat interestingly, that allows end tags like </body > that contain whitespace, and also things like this:

  <a href=foo>prefix-</a
  ><i>something following w/o space between, on a separate line for readability</i>
  • 1
    Thanks to your answer I'm now (ab)using spaces in end tags. It's making my life easy – user2528 Sep 29 '16 at 7:46

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.