megster.net

Nathan Russell

Nathan Russell

Is <br> the work of the devil?

March 8, 2011, by admin, category html

In a word, yes!

Firstly, too many people mis-use (or even abuse?) this element. How many times have we seen sites with code such as <br><br><br> to give some presentational spacing? Oh dear!, that’s really not good is it? The purpose of the <br> element is to add a single line break in a paragraph. If we want to create a new paragraph, we should start a new <p> element. And if we want more spacing above the new paragraph we should style the <p> element with css and apply either some top padding or top margin.

Secondly – and this one is more subtle – <br> is only valid if using the html4 or html5 doctype. It is not valid when using the xhtml doctype. To create a single line break in an xhtml document, one needs to self close the element like this: <br />
If we have declared our document as using the xhtml doctype, as many page authors do, we must make sure that we are producing 100% valid XML. This means that elements such as <br> and <input> must be self closed as <br /> and <input />

I have no evidence to support this, but I wonder if many of the browser rendering bugs, especially those in IE6, are caused by using the xhtml doctype but not writing valid xhtml in the document. If on one hand we tell the browser it is about to receive valid xhtml but then give it malformed XML, should we really be surprised that it has trouble rendering our page?

My personal preference is that of using the html5 doctype and xhtml. I know the html5 doctype does not require of enforce the document to be xhtml, but I think it’s a good standard to try to adhere to and consider it a ‘best practice’

So, what do you think ?