24

This is probably a rookie question, but I need to know what I need to know :)

Why do some HTML tags end with a forward slash?

For example this:

 <meta name="keywords" content="bla bla bla" />

What's that last forward slash for? What happens if I remove it?

Also some other tags have this as well... I have removed some without anything happening.

Matthias Braun
  • 24,493
  • 16
  • 114
  • 144

4 Answers4

30

In XHTML <foo /> is shorthand for <foo></foo>. In HTML the syntax is obscure and marked as "don't do this" as browsers don't support it.

If you are writing XHTML but pretending it is HTML (so that browsers (such as Internet Explorer 8) which don't support XHTML can handle it), then elements defined as EMPTY in the specification must be represented that way (and elements which are not must not).

HTML 5 became a recommendation five years after this answer was written and changes the rules.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 1
    I don't think that HTML standard reference says what you are saying it does. It lists four things to avoid, none of which is the `` construct. – Tim Nov 22 '09 at 16:26
  • @Tim NET tags are the **first** thing listed in the section I linked to. – Quentin Nov 22 '09 at 17:26
  • Yes. So `` is short for `>` or `>`. Or to use the example in the original question (and noting that the end tag for head and start tag for body are optional): `>` – Quentin Nov 22 '09 at 21:49
  • 2
    @David Fair enough, but my understanding of the SGML NET conecpt is that `bla` and that the reference you cite essentially says don't do that, rather than don't do ``. See also http://en.wikipedia.org/wiki/Sgml#NET – Tim Nov 22 '09 at 21:50
  • @David OK, I can see that the concepts are directly related, but can't see the exact equivalence. I'll defer to your experience on this one. :-) – Tim Nov 22 '09 at 21:53
8

In the early days of HTML, it wasn't uncommon to find code like the following:

<ul>
  <li>item 1
  <li>Item 2
  <li>Item 3
</ul>

The problem with this approach is that it lead to HTML that was very tedious to parse because it was often difficult to understand the intent. As such, developers that parsed HTML had to rely on some [often] unreliable assumptions.

To alleviate this problem, the standards committee mandated that XHTML be well formed. As such, all tags were required to have both a start tag and an end tag, replacing the above HTML with the following:

<ul>
  <li>item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

This worked well for tags that contained inner text or child elements, but it didn't work well for tags that stood alone (e.g., the <br> tag). To overcome this issue, while complying with the rule stating that all tags must have a corresponding closing tag, the standards committee sided with a trailing forward slash (e.g., <br />). It should be noted, however, in XHTML, the following is also legal: <br></br>.

senfo
  • 26,901
  • 15
  • 70
  • 101
3

that's the xhtml syntax for an element that doesn't have a closing tag

Galen
  • 29,108
  • 8
  • 66
  • 88
1

In XHTML syntax, <tag /> is equivalent to <tag></tag>

CesarGon
  • 14,525
  • 6
  • 53
  • 81
  • 1
    No. In HTML syntax, `` is equivalent to `>`, although support among browsers is very week. (HTML5 is likely to change the meaning to match that of XHTML but that specification is still only a draft). – Quentin Nov 22 '09 at 14:54
  • You are right; I have changed "HTML" in my response to "XHTML". Thanks for the clarification. – CesarGon Nov 22 '09 at 14:59
  • 2
    It is still wrong though :) The syntax is equivalent to an empty element, not one with content. – Quentin Nov 22 '09 at 15:01
  • @David. No HTML5 won't change the meaning to match that of XHTML. It will change the meaning to do what browsers currently do; which is = – Alohci Nov 22 '09 at 19:47
  • @Alohci — So it will change the meaning to "This starts and ends the EMPTY elements"? That is what browsers currently do. That is what XHTML does. – Quentin Nov 22 '09 at 22:29
  • @David. For empty elements, yes. For non empty elements, has a meaning in XHTML. It won't have a *meaning* in the HTML serialization of HTML5, but it will be treated as if it means "This starts the element", which is different to XHTML. – Alohci Nov 23 '09 at 01:10