The <br>
elements are always rendered in all browsers, since all browsers are aligned to follow a particular specification amended by the W3C Consortium, but in some instances their effects gets hidden.
You will understand, why it is so, when you get a deeper perspective on how the <br>
and <p>
tags work in this particular context of your question. So let us take a deeper look.
The <br>
tag - When applied after a content, it leaves a line break, two <br>
tags simultaneously can leave a single line of space between visible texts as seen between paragraphs. Note: By virtue of design of <br>
tags, every subsequent <br>
tag after two consecutive <br>
tags will insert a single line of empty space(Note: A single line of empty space is conventionally known as a paragraph break) instead of a line break.
The <p>
tag - It does ensure that, there is an empty line of space on either side of the embedded content(Note: The empty lines of space within the content that precedes or succeeds it, are treated as part of the content itself). It does not take into account of any line breaks ( Note: It takes two line breaks to leave an empty line of space(Paragraph Break) ie; 2 <br>
tags & every subsequent <br>
tags after 2 consequent <br>
tags insert a line of empty space instead of a line break.).
Take a look at the following code snippets.
1.
<p>This is a paragraph.</p>
This is a paragraph.
In the above code snippet, see that the first line of text is within the <p>
tag while the second line is not, and it functions as i described above.
2.
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
In the above code snippet, both the texts are embedded within 2 separate <p>
tags, still there is only a single line of empty space between them. This is because the <p>
tag will only consider the empty lines of space that are part of the text/content while leaving its own empty line of space.
You'll get more clarity as you examine the code snippets below.
3.
<p>This is a paragraph.<br></p>
<p>This is a paragraph.</p>
In the above code snippet, a single <br>
tag is embedded within the first <p>
tag, but see that a single <br>
is not equivalent to an empty line of space( it takes 2 <br>
tags to be equivalent to an empty line of space). So an empty line of space is not part of our first text content so you will not see the effect of <br>
tag, as a single line break is not equivalent of a line of space & <p
> tag only takes into account of preceding lines of space before inserting its own single line of space.
By virtue of design, the <p>
tag will consider the empty lines of space within an element as part of that element, so in case an element has empty lines of space towards its end then the <p>
tag still leaves its own single line of empty space thus adding an extra blank line to the already existing lines of spaces of the content, Look at the example that follows.
4.
<p>This is a paragraph.<br><br></p>
<p>This is a paragraph.</p>
In the above code snippet, 2 <br>
tags have been inserted inside the first <p>
tag so it leaves an empty line of space as part of the first <p>
tag. The second <p>
tag will consider this empty line of space as a part of the content within the first <p>
tag and when it insert its own single line of space after the previous content, it seems as if an extra blank line of space have been added to it.
5.
<p>This is a paragraph.<br><br><br></p>
<p>This is a paragraph.</p>
In the above code snippet, you can see 3 <br>
tags are part of the first <p>
tag, now scroll up and look at the functioning of <br
> tags as i described in the begining, you can see that now 2 lines of empty space are part of the content inside first <p>
tag, hence the spacing between the either <p>
tags are of 3 lines of empty space(2 empty lines by virtue of 3 <br>
tags and 1 empty line by virtue of the succeeding <p>
tag).
Hope this address your doubt, please also do refer this BR tag has no effect when parent has height, to get a different perspective.
<br>
is not supposed to be used like this. I'm not the person creating this HTML, someone else has, and I want to still properly handle it. I want to replicate the browser's behavior in this case, and even if it is wrongly used, knowing a bit more about how this will be rendered is important to me. – Poverty