Jamie
Jamie

Reputation: 379

Rule of thumb for when to use /> in html

Does anyone know of a good reference or rule of thumb for when to finish a tag properly <script></script> or when to use the simpler <script />.

Perhaps a couple of examples of which is best for, eg. <input></input> vs <input />, <script /> vs <script></script> etc etc.


Please feel free to edit and rephrase my question if I don't have the terminology quite right

Upvotes: 1

Views: 199

Answers (3)

Quentin
Quentin

Reputation: 943214

Never use <element /> in HTML since, depending on the version of HTML you are using and where you put it it either:

  • doesn't mean what you think
  • is a syntax error
  • is optional and therefore a waste of time

The list of elements in HTML 4.01 has a column which shows you when start and end tags are optional or forbidden.

If you are writing XHTML then use <element /> when, and only when, the end tag is Forbidden in HTML 4. This is part of the HTML compatibility guidelines. (If you are one of the very tiny number of people serving XHTML as application/xhtml+xml (thus excluding users of IE 8 and lower) then you can use the syntax on any element).

If you are writing HTML 5. Then you can use that syntax under the same rules as XHTML - but it is optional and therefore I wouldn't bother.

Upvotes: 3

New Guy
New Guy

Reputation: 9126

It really depends on your document definition type. If you are unsure which one best fits your doc type then you could always just run it through a validator which will complain if it sees anything that doesn't fit your doc type. For example:

http://validator.w3.org/

Upvotes: 0

Andy
Andy

Reputation: 8562

Usually it's your preference, although it also depends on what type of html you're going for. 4 vs. 5, strict, loose or transitional.

Although I would recommend against doing <script />. It seems that if you do the short version, IE doesn't actually download your JS file! <script></script> works though.

Upvotes: 0

Related Questions