Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some HTML tags end with a forward slash?

Tags:

html

People also ask

What does a forward slash mean in HTML?

Forward Slash We use them in URLs for websites. Behind the scenes, they indicate a folder. We also use forward slashes in HTML. In fact, you've already seen them. Forward slashes are what we use to indicate a closing tag.

Which symbol represents closing tag in HTML?

An opening tag begins a section of page content, and a closing tag ends it. For example, to markup a section of text as a paragraph, you would open the paragraph with an opening paragraph tag <p> and close it with a closing paragraph tag </p> (closing tags always proceed the element with a /).


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.


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>.


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


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