If both <br> and <br /> are valid syntaxes, which one should I use?

Just so that it’s 100% clear for everyone, the W3C HTML5 spec explicitly states that either syntax is fine. (At least, as of January 2019, anyway)

From the spec:

In HTML, these elements have a start tag only. The self-closing tag syntax may be used.

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content.

In both HTML and XHTML, within each tag, whitespace is permitted after the tag name…Some authors choose to include a space before the slash in the self-closing tag.

So according to the W3C, <br>, <br/>, and <br /> are all valid HTML. You’re free to use whichever you prefer.

Personally, I prefer all of my tags to close. It feels more consistent, to me. It bugs me to see sporadic unclosed tags throughout a document full of otherwise closed tags. I understand that not everyone shares my phobia, though. :wink:

So, by all means, omit the slash if that’s your preference. But it’s perfectly valid to include it if you want to.

7 Likes

Subjective reasoning has no place in the specifications. Style guides are just that, guides.

Bottom line, if the page is being served as text/html then we do not need XML compliant markup.

There is one case where we must use XML compliant tags, JSX. What that suggests is that JSX has XML behind the scenes.

3 Likes

:man_shrugging: Dunno what to say. Take it up with the W3C?

Because, as of now, their specification clearly states that either syntax is valid, without giving an opinion as to which should be used. The spec leaves that decision entirely up to the author. Whether or not that should be the case is up to the W3C.

And to clarify, in my original post, I wasn’t so much responding to your earlier comment about best practices, but rather to the later poster who stated that “void elements should not contain a space and/or a forward slash at the end, as per the W3C .” Because the W3C says nothing of the sort. Per the W3C spec, using self-closing tags for void elements is valid. I just wanted to make that clear to everyone.

2 Likes

The specification does not speak in subjective terms. What we feel has no bearing. Nothing to take up with W3C, they state clearly that we do not need to close void elements. That only applies when the document has a MIME type of "text/html". If it is served with "application/xml" then it MUST conform to XML requirements.

2 Likes

Correct. The spec clearly states that we do not need to close void elements. But it also explicitly states that we may close them. That’s all I was saying. And that it’s my preference to close them, which is still valid HTML. And if anyone else’s preference is to close them, they may also do so whilst still writing valid HTML.

Which is moot. We may choose to do as we wish, but the choice itself should be non-subjective. If we use a style guide that suggests we always close them, then stick to the guide without letting personal feelings or preferences come into it. I agree that we should strive toward consistent usage. Once we enter an organization, it will be their style guide that applies, not our personal choice.

1 Like

I don’t think it is moot. The spec treats both options as equally valid. That is an objective fact. And if both options are equally valid, then the choice as to which one we use is inherently subjective.

You say that self-closing tags are unnecessary for text/html docs and are not part of the HTML 4.1 spec from which the HTML5 spec is mostly derived. If I’m reading you correctly, you think that those are the most important facts to be considered, therefore the best practice is to omit self-closing tags unless otherwise required. That is a perfectly valid opinion. But it is a subjective opinion. You’ve made a value judgement about what you think is most important to consider in this decision.

I say that self-closing tags are valid HTML, were folded into the HTML5 spec from the XHTML spec, and that using them maintains consistency with every other tag in an HTML document, and maintains consistency with other markup languages web devs are likely to encounter, namely XML and JSX, which require self-closing tags. To me, those are the most important facts to consider, therefore the best practice is to use self-closing tags. Using them is one less thing I have to think about when I’m bouncing between documents and languages in a project, which allows me to better focus on larger problems. That is my value judgement and it is no more, nor no less, subjective than yours.

5 Likes

After reading my original comment from last year, it occurred to me I had (in haste) made an error in punctuation (not in wording) that resulted in the error you pointed out as seen below:

Original: "I agree that in HTML5 , void elements should not contain a space and/or a forward slash at the end, as per the W3C. It’s only required in XHTML and XML."

Corrected: I agree that in HTML5 , void elements should not contain a space and/or a forward slash at the end. As per the W3C, it’s only required in XHTML and XML.

1 Like

A mistake?? On the internet?? Noooooooo! :scream:

It’s all good. Happens to all of us.

I’m not an expert, whatsoever. I’m actually studying HTML as a total beginner. However, I would like to comment on this debate. Both of you have arguments. Allow me to express just an observation.

Though HTML allows one to use or not use closing tags for empty elements, the idea is not about what you are allowed to do but what is the minimum requirement in order for one to do what he wants.

Like mrf explained, in a style guide there shouldn’t be any subjective choice in order to create. The fact that you can be subjective it’s another story. A guide should be objective, and you can be objective when you follow the minimum requirement. In this case there is no need for a closing tag when you have an empty element. One cannot be subjective when he follows the minimum requirements because he wouldn’t be able to do what he wants otherwise. So, it’s an objective aspect.

The fact that you want to have closing tags everywhere because it helps you is a subjective choice as you are not asked to do that in the first place as a minimum requirement. I think it’s useful to have consistency too but that’s another story.

I think this problem is not a HTML one but a logical one. This is why I dared to write. I’m a total beginner otherwise.

The way is see it, to meet the required minimum in both XML and HTML is to close tags. You´re meeting both minimums and dont have to remember whether you´re working with HTML or XML.
Is it more work - barely,
does it bring any errors - no,
its it against rules - no,
works it as failsafe - yes.
Win-Win for all.

Minimum argument is bit problematic for me, as minimum is subjective and different file by file, system by system. What I want is go above and beyond and make users happy :slight_smile:

2 Likes

I think there are different style guides for each. I don’t understand why you put them together. We were talking about a HTML guide style, not about having one for both of the coding languages. Though I can understand you and agree with you on the practical aspect, my observation wasn’t on what’s the best way to code, but regarding how a style guide should be. As mrf said, we should look for objective directions. You cannot be subjective with the minimum requirement. The fact that different files types (HTML and XML) have different minimum requirements is not subjective. The point wasn’t to create a universal style guide for all the coding languages but a style guide for each one of them separately. Having this in mind you cannot be subjective by following the minimum requirement for each coding language. The fact that you want to have a common coding style for both coding languages is subjective.

I understand that you want to make users happy but that’s exactly what subjectivity is. A style guide purpose is not to make users happy but to guide them. What you consider to make the users happy may be good, indeed, and I don’t disagree with what you consider to be good. My objection was on the subjective-objective matter, not regarding which is the best way to code.

2 Likes

i think br is a self closing tag witch close itself, so we dont need to colse it with /br right?

thank you, this is helpful

Enjoyed this thread and think I learned a bit about both HTML/XML and logical reasoning :slight_smile: thank you

2 Likes

if you use validator to validate ( https://validator.w3.org/) use

Thanks for the suggestion :heart_eyes: :heart_eyes:

Do you have a problem or something? They have been discussing this stupid topic about the br or br/ and now you ask the question? Read the thread.
Best practice, not use it. Easy is better than Difficult
Difficult is better than Complex

You should know this words as a programmer. Zen.

bit43…, I agree with you. Even though mtf and nemo242 may have annoyed each other (I don’t know if that was the case), their debate really made clear some aspects of HTML that would have been hard to clarify well in a simple statement.

mtf and nemo242, my thanks for your debate!

Let’s say I have a picture of myself. What would be the appropirate alt text for that? Would I simply use my name?(I assume no because that doesn’t describe me)