Why use a /> at the end of a statement?

M

Mike Barnard

Hi.

I have been examining the code of a site and often, not always, there
is a slash before the end of a statement.

<br class="clear" />

<img src="images/icon_burdick.gif" width="81" height="30"
alt="Burdick" />

<br /><br />

<img src="images/title_latestnews.gif" width="253" height="55"
alt="Latest News" />

Why is it needed, if it is needed, and what is it good for? I
understand </p> </h1> etc but why a /> alone?

I'm sure it'll be obvious when its pointed out to me! Thanks.
 
M

mbstevens

Hi.

I have been examining the code of a site and often, not always, there
is a slash before the end of a statement.

<br class="clear" />

<img src="images/icon_burdick.gif" width="81" height="30"
alt="Burdick" />

<br /><br />

<img src="images/title_latestnews.gif" width="253" height="55"
alt="Latest News" />

Why is it needed, if it is needed, and what is it good for? I
understand </p> </h1> etc but why a /> alone?

I'm sure it'll be obvious when its pointed out to me! Thanks.

It is an XHTML thing. This part of this article should get you
started understanding what's going on:
http://www.w3.org/TR/xhtml1/#h-4.3
 
S

Steve Pugh

Mike Barnard said:
I have been examining the code of a site and often, not always, there
is a slash before the end of a statement.

<br class="clear" />

<img src="images/icon_burdick.gif" width="81" height="30"
alt="Burdick" />

<br /><br />

<img src="images/title_latestnews.gif" width="253" height="55"
alt="Latest News" />

Why is it needed, if it is needed, and what is it good for? I
understand </p> </h1> etc but why a /> alone?

I'm sure it'll be obvious when its pointed out to me! Thanks.

It's required in XHTML. Why? Because one of the restrictions of XML
over SGML and hence of XHTML over HTML is that all elements must be
expclitl closed. Which means both that all those optional closing tags
like </p> and </li> become mandatory, and that empty elements must
close themselves and the XML syntax for that is <br />.

In practical terms it just boils downs to:
If you use XHTML use <br />, <img />, etc.
If you use HTML use <br>, <img>, etc.

Steve
 
J

J.O. Aho

Mike said:
I have been examining the code of a site and often, not always, there
is a slash before the end of a statement.
<br class="clear" />
Why is it needed, if it is needed, and what is it good for? I
understand </p> </h1> etc but why a /> alone?

As mbstevens already pointed out, it's xhtml, just keep in mind to not use
that in html, so if you copy something from an xhtml source to use on your
html page, remove those '/' in the end of the tags, or else it can be a bit
"random" what may happen in a browser.
 
J

Jukka K. Korpela

Scripsit Steve Pugh:
It's required in XHTML. Why? Because one of the restrictions of XML
over SGML and hence of XHTML over HTML is that all elements must be
expclitl closed.

It's somewhat debatable whether the "/" is quite explit.
Which means both that all those optional closing tags
like </p> and </li> become mandatory, and that empty elements must
close themselves and the XML syntax for that is <br />.

Actually, empty elements close themselves just as well in SGML. They just
don't use the "/". When an element has EMPTY declared content, the element
cannot have content and cannot have an end tag, so the start tag is the
entire element. It's "self-closing", so to say.

This is no different in XML, except that since XML is to be processed by
simple tools that possibly don't read any DTDs (so that they cannot know
which elements have EMPTY declared content, among other things), XML has
rules that ensure that a document can be parsed as a sequence of tags and
content strings without knowing anything about any higher level syntax.
Hilariously, people often praise XML for being "more structural".

So the point is that when an SGML parser sees <br>, it needs to check the
document type definition to see that the element cannot have any content,
whereas an XML parser needs a low-level indicator, <br />. Actually <br />
is equivalent to <br></br>, though the longer form is frowned upon for
"compatibility" reasons. Web browsers that eat HTML are expected to skip the
"/" in "<br />" (not really because they process SGML but rather because
they don't - instead they have simplistic poor mans tag slurper parsers that
ignore the "/" just because they traditionally skip everything that they
don't understand).

Quite a mess, eh?

In practical terms, XHTML is pointless as a delivery format of WWW documents
for the time being. It's just an exercise in futility, though some day
things might change, in some direction.
 
M

Mike Barnard

It's required in XHTML. Why? Because one of the restrictions of XML
over SGML and hence of XHTML over HTML is that all elements must be
expclitl closed. Which means both that all those optional closing tags
like </p> and </li> become mandatory, and that empty elements must
close themselves and the XML syntax for that is <br />.

In practical terms it just boils downs to:
If you use XHTML use <br />, <img />, etc.
If you use HTML use <br>, <img>, etc.

As said to others, thanks for the help. I'm (slowly) getting there.
 
M

Mike Barnard

As mbstevens already pointed out, it's xhtml, just keep in mind to not use
that in html, so if you copy something from an xhtml source to use on your
html page, remove those '/' in the end of the tags, or else it can be a bit
"random" what may happen in a browser.

Belive it or not, I don't 'copy' source. :) But I'm trying to
understand why people do what they do. Thanks for the input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top