The trailing slash
January 15, 2009 8:29 AM   Subscribe

What is the significance of the trailing slash in <foo/> as used for minor web programming? I'm guessing it's some sort of escape sequence, but what confounds me is that in the code where I often see it, <foo> often seems to work equally well. So what's the point? I see this dilemma frequently all kinds of Web stuff: PHP, Wikipedia templates, embedded code in VBulletin, etc, etc.
posted by crapmatic to Computers & Internet (10 answers total) 1 user marked this as a favorite
 
the trailing slash makes it XHTML valid.
posted by nitsuj at 8:33 AM on January 15, 2009


In XHTML (as opposed to HTML), certain elements (most notably <img> and <br>) are "empty" but must be closed anyway, since XHTML uses the syntax rules of XML. The slash right before the > character closes the element the way a closing tag like </p> would.
posted by letourneau at 8:35 AM on January 15, 2009


...meaning, instead of writing <br></br>, you are supposed to write <br/> to properly close that tag.
posted by arco at 8:37 AM on January 15, 2009


Exactly, it's because XHTML is valid XML, while HTML 4.0 is not. Similarly, in XHTML all the element names are lower case, because XML is case sensitive. In the older HTML specs, it didn't matter. There are all sorts of little rules XHTML is supposed to follow, that older versions of the HTML spec do not.
posted by chunking express at 8:43 AM on January 15, 2009


Oh, and as to "in the code where I often see it, <foo> often seems to work equally well," that's because browsers are built to handle invalid markup gracefully. Although an unclosed <br> is incorrect syntax on an XHTML page, every mainstream browser treats it as though it were written <br/>. But it's nice to be correct about your markup if you can.
posted by letourneau at 8:44 AM on January 15, 2009


Aside from conscientious coders, XSLT parsers can often be grouchy about requiring every tag to be closed- it's their job, after all, to enforce XML compliance.

The upshot is that HTML that's "generated" via XML+XSLT tends to be more compliant than browsers require. As this technique becomes increasingly popular for publishing HTML, you'll see more and more code written properly like this.
posted by mkultra at 8:52 AM on January 15, 2009


There are a couple of HTML tags that, traditionally, never had "closing" tags. So while you would use <b> and </b> to indicate where boldface would start and end, you would only need to use a single <img> tag (to place an image) or a <br> tag (for a line break). These days, as others have said here, in order to implement XHTML correctly, these tags now contain their own trailing slash within the tag, as in <img src="whatever.jpg" /> and <br />.
posted by lgandme0717 at 8:56 AM on January 15, 2009 [2 favorites]


Just a side bit of history: <br> is one of the "self-closing tags." The original specs called for <br/> to be valid, but all of the browsers in existence at the time could not deal with it. However, <br /> did work in existing browsers, and they amended recommendations to be that, instead.

Do write valid XHTML wherever possible, as occasionally lesser browsers or other middleware will simply puke on poorly-formed XHTML, sometimes in subtle ways.
posted by adipocere at 9:09 AM on January 15, 2009


Another reason to write valid XHTML is for the sake of other programmers. If they can expect consistency and the following of standards, the easier it is for them, or you, to debug any potential problems.
posted by Echidna882003 at 10:10 AM on January 15, 2009


Response by poster: Aha, thanks much, <br /> was one of the biggest examples I've seen of this issue. I remember a couple of years ago when I first saw it I was thinking "what the hell?"
posted by crapmatic at 11:13 AM on January 15, 2009


« Older Brunch in Old Town, Alexandria   |   Legit landlord or identity thief? Newer »
This thread is closed to new comments.