html-css-standards icon indicating copy to clipboard operation
html-css-standards copied to clipboard

Closing Tags vs. Boolean Attributes: Guidance Seems To Contradict Intent

Open RunnerRick opened this issue 6 years ago • 3 comments

The guidance for HTML closing tags and boolean attributes seems to be contradictory.

I'm referring to this text:

From https://github.com/thenerdery/html-css-standards/blob/master/standards/html.md#closing-tags:

Closing Tags

All elements should be closed via a tag pair or self closing declaration.

From https://github.com/thenerdery/html-css-standards/blob/master/standards/html.md#boolean-attributes:

Boolean Attributes

Boolean attributes should not have a value.

I assume the reason you would want empty elements (i.e. <input>, <br>) to be "self closing" (e.g. <input />, <br />) is so that an XML parser would parse the HTML source as a well-formed XML document.

However, if boolean attributes "should not have a value," then an XML parser would not consider the document well-formed. See https://stackoverflow.com/questions/6926442/is-an-xml-attribute-without-a-value-valid.

I can see the reason for dropping the value from boolean attributes, because it saves bytes, but if that is the reason, then you should also drop the self-closing declaration.

In other words it seems consistent to use either boolean attribute values with self-closing declarations or drop them both:

<input type="checkbox" selected="selected" />

OR

<input type="checkbox" selected>

NOT

<input type="checkbox" selected />

With that said, I'm open to learning the reasoning behind the guidance.

RunnerRick avatar Aug 02 '17 11:08 RunnerRick

I don't think there's a technical reason to self-close void tags if the doctype isn't xhtml. This standard is in place not to save bytes, but for readability and consistency amongst developers who are writing html.

ccheney avatar Aug 02 '17 16:08 ccheney

Thanks for the clarification.

RunnerRick avatar Sep 02 '17 15:09 RunnerRick

I agree with Rick. Self closing tags in HTML5 is a strange choice. It is not in line with industry convention and harms readability.

noahblon avatar Feb 06 '18 15:02 noahblon