Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid HTML5 to use a single tag for a div?

Tags:

For example: <div/> instead of <div></div>. I did this and apparently the HTML5 validator passed this as valid. I was wondering it this is actually true?

PS: I'm serving page as application/xhtml+xml

like image 463
user434366 Avatar asked Oct 04 '10 02:10

user434366


People also ask

Is DIV tag valid in HTML5?

No, technically using a self-closing <div/> tag is invalid HTML5.

Can I use a tag as a div?

The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!

Can you have multiple div tags in HTML?

Creating Web Layout using Div Tag The div tag is a container tag inside div tag we can put more than one HTML element and can group together and can apply CSS for them.

Can a div be a self closing tag?

No. HTML 4. x doesn't have any concept of self closing tags.


2 Answers

This is not valid HTML 5 (HTML does not allow shorttags, the equivalent HTML construct is a single opening div tag). It is valid XHTML 5, as it is valid XML.

The reason why you might see this pass through a validator just fine is because of what you stated:

PS: I'm serving page as application/xhtml+xml

Which means that you tell the validator that it must treat your markup as XML. In other words your page is not HTML 5 at all.

like image 171
user268396 Avatar answered Oct 02 '22 13:10

user268396


That syntax is allowed for a specific subset of HTML5 elements, known as void elements, and a few other cases:

Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

Void elements:

area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

They're not allowed for any others, including <div>.

(I'd originally answered that yes, this is valid HTML5, since it's such a common construct in XML. Rex M, and a close reading of the spec, tells me that I'm wrong)

like image 41
Michael Petrotta Avatar answered Oct 02 '22 14:10

Michael Petrotta