Self-closing Tags

I have been using HTML for a long time, at least since 1997. HTML is just text, but you add tags in <tags> like this to get things like bold text, big text, and to insert pictures. Usually you have an opening tag and a closing tag, so to make something bold you put some text between two tags like this <b>some bold text</b>. The slash at the beginning of the second tag means you are closing the tag. Not all tags need to be closed because they don’t surround text, for instance an image tag is all one big img tag. To start a new line, you use a break tag which is just <br>.

XML came along as a souped-up version of HTML and then spawned XHTML. While HTML was pretty forgiving, the idea was to be more rigorous with XML and XHTML. For a while, it was common to use a paragraph tag to start a new paragraph in HTML, so you could put it at the end of a paragraph like this.<p>

<p>But really you are supposed to put a paragraph between an open p tag and a closing p tag like this.</p>

It didn’t matter how you did the p tags, people’s web browser would still show the paragraphs as intended. But it became an issue with style sheets because you might say that everything identified between p tags should have a certain look. If it wasn’t between the p tags, it might default to unformatted text.

When I started messing around with Movable Type and then WordPress templates, I noticed a lot of their tags had a slash at the end like this <br />. It seemed to work fine that way, but I wasn’t sure what the deal was. Whenever I would write HTML, I wouldn’t do that because it wasn’t part of the HTML I had learned. Again, it didn’t seem to make any difference in the appearance of pages with or without slashes in the HTML.

Today I found out that in order to meet the rigid requirements of XML and XHTML, the slash is needed in order to close the tag. All open tags have to have a closing tag in XML, but if you put a slash at the end of an opening tag, the tag is self-closing. It seems kind of silly. Why can’t XML realize that some tags aren’t supposed to close? I think maybe it is because in XML you can make up your own tags instead of being restricted to using standard ones.

This came up because I was checking the HTML in my revamped movie review website and my old HTML checker caught the slashes at the end of some of the tags that had come from the original template and said they were not correct. But it also said that the header at the top of the file saying the code was all XHTML wasn’t correct either. It seems that for XHMTL, which I was telling web browsers I was using, you need those slashes. The other thing I got caught on was using p tags as paragraph markers instead of enclosing the text between p tags.

So I fixed up the code and then went into my Access database to get all my paragraphs in p tags. In Microsoft Word you can search for the two hard returns that make a new paragraph by searching for ^p^p, but in Access you can’t do that. So I found out I had to do an update query, then go to SQL language and do a specially formatted command to put the tags in. I used an online validator to check the new web pages for compliance and they passed with flying colors. So that’s great, but to me the neat thing was finding out why those other pages were using slashes.

One thought on “Self-closing Tags

  1. I was just telling someone the other day that you could do a br with or without the end tag, but I didn’t know why. Now I know. (I also put them because I first spotted this practice in MovableType.)

Leave a Reply

Your email address will not be published. Required fields are marked *