Should I use <br /> or <br>?

I knew that the correct way was to use <br />.
But I see a lot of people still use it as <br>. Is that correct? I mean, I know it works… but is that correct?

Hi there Norman,

unless you are using an “xhtml1-strict.dtd” DOCTYPE,
and I can think of no good reason why you would, use

<br>

coothead

4 Likes

I’m used to writing XHTML (served as XHTML, but that’s another story)
so I feel more comfortable writing <br /> out of habit.

To be sure, it should not be <br></br>

EDIT
as coothead just posted <br> is perfectly fine.

4 Likes

There is no space needed in <br/> when writing XHTML - of course XHTML also accepts <br></br> as it doesn’t care how you close the tag as long as all tags are closed.

When people added the invalid slash in the end of their HTML tags and served them to Netscape 4 that browser would not only ignore the / it would ignore whatever preceded the /as well.

So:

HTML <br>
XHTML <br/> or <br></br>

Remembering of course that IE8 and earlier browsers don’t understand XHTML and so offer the file for download instead of displaying it in the browser.

1 Like

Hmmm. I’m in the process (finally) of going to HTML now but I went by

Must have a start tag, and must not have an end tag. In XHTML documents, write this element as <br />.

and that the w3c validator never complained about it.

For HTML, that’s correct. But for XHTML, <br></br> is perfectly valid.

W3C XHTML Empty Elements

EDIT: And, for clarity, I’d like to add one line to felgall’s summary.

HTML <br>
XHTML <br/> or <br></br>
HTML-compatible XHTML <br />

The reason many people think the space is required or that empty element closing tags are forbidden is because almost everyone this entire time has only ever been writing HTML-compatible XHTML.

Thank you guys for your replies.

Yes, in the last times I only used <br />.

Ah, the good old “IE quirks mode days” - NOT -

It was a bit involved. I used

<?php
/* Content-type header Section */
header("Vary: Accept");
if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml"))
{
	header("Content-Type: application/xhtml+xml; charset=utf-8");
	header("Content-Language: en");
	header("Content-Style-Type: text/css");
	header("Content-Script-Type: text/javascript");
	echo "<?xml version='1.0' encoding='utf-8'?>\r\n";
//   ^^^ tag causes IE to use quirks mode
}
else
{
	header("Content-Type: text/html; charset=utf-8");
	header("Content-Language: en");
	header("Content-Style-Type: text/css");
	header("Content-Script-Type: text/javascript");
}

/* ***** (X)html Doctype Section ***** */
if($doctype == "trans")
{
	echo "<!DOCTYPE html
        	PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
			'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\r\n";
}
else
{
	echo "<!DOCTYPE html 
        	PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
	        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\r\n";
}
echo "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\r\n";
?>

this page has <br /> and the validator says

This document was successfully checked as XHTML 1.0 Strict!

1 Like

The most recent browser for which the space is necessary in HTML-compatible XHTML is Netscape 4. All more recent browsers will accept it as HTML without the space.

So <br/> is acceptable for HTML compatible XHTML unless you still need to support Netscape 4.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.