<br> or </br> Which one should we use for a line break? Furthermore, which is correct <hr /> or <hr>
I tried both tags, and it shows the same results. Nevertheless, I am perplexed as on an online platform they say using </br> will be best compared to <br>. Furthermore, it is necessary to use space in <hr /> or we can simply write <hr/>.
<br> is fine; <br/> works but the / is pointless. </br> is nonsense.
Similarly, <hr> works, <hr/> works but the / is pointless.
Now, some contexts require XML-like tag closing, so sometimes <br/> is what you want, but HTML5 parsers do not care.
TL;DR - use <br> when writing HTML, but use <br/> when writing JSX.
XHTML was once a proposal to essentially make HTML compatible with XML, which is where the practice of using self-closing tag syntax in HTML came from. XHTML never really caught on, and has been superceded by newer versions of the HTML spec for years now.
In HTML, a void element only has an opening tag, and no closing tag. <br> and <hr> are examples of void elements.
You will likely also see a space before the / in some examples, e.g. <br /> instead of <br/>. This comes from a bug in Netscape Navigator 4, which was released 26 years ago and is clearly no longer relevant to the modern web.
However, HTML parsers are robust, even when they try to parse HTML that doesn't adhere precisely to the specification. So if you use <br/> or <br />, browsers will treat it as though you had just used <br>. Some developers (like myself, I have to admit) are simply in the habit of using self-closing tag syntax even though it's no longer necessary not strictly correct.
This robustness also means an HTML parser would likely understand </br> as meaning <br>. But writing a closing tag for a void element isn't meaningful.
One place you may see self-closing tag syntax where it is needed is in JSX, which is essentially an extension to JavaScript first introduced by the popular React framework that allows for HTML-like code to be written alongside JavaScript. Within JSX, void elements are expected to use self-closing syntax like <br/> instead of the HTML style of only having an opening tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With