Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use a self closing DIV tag? [duplicate]

Tags:

html

Possible Duplicate:
Are self-closing tags valid in HTML5?

For example:

<div id="myDiv" /> 

Something would then be done to populate this div using Javascript.

Is this valid HTML?

like image 813
Abe Miessler Avatar asked Nov 01 '11 19:11

Abe Miessler


People also ask

Should you use self closing tags?

The fact is there is no need to close self closing tags by a slash /> at the end of the tag. Although many people use it but there is no meaning of slash at the end of the start tag even though you use it is ignored by the browsers.

What would happen if you do not use a closing tag?

If you don't add a closing tag the browser won't know where it ends. It will take the next tag and think it belongs to the previous tag without the closing tag.

Why is it important to use closing tags?

Should Optional HTML Tags be Closed? Code with closing tags is much more readable and easy to follow. It is much easier to visually inspect a page with well laid out markup. Working with this markup is easier for developers.

Do all tags need a closing tag?

No, not all the tags are ending tags. Tags may be either paired or unpaired (or single). Paired tags need to be closed, i.e. they contains both ending and non-ending tags, whereas unpaired tags need not be closed, i.e. they only contain opening tags.


2 Answers

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

It is valid in XHTML.

like image 66
Mark Byers Avatar answered Oct 27 '22 12:10

Mark Byers


Div's are not valid self closing tags. To have an empty div it would be better to do this:

<div id="myDiv"></div> 
like image 20
scrappedcola Avatar answered Oct 27 '22 10:10

scrappedcola