英文:
why is there no empty line between <p> and <a> when there is one <br> in between them
问题
The provided code snippet contains HTML and some questions about its behavior. Here's the translated HTML part:
<a href="google.com">google</a>
<br />
<a href="google.com">google</a>
<br />
<p>This is the first paragraph.</p>
<br />
<p>This is another paragraph.</p>
If you have any further questions or need more translations, please feel free to ask.
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<a href="google.com">google</a>
<br />
<a href="google.com">google</a>
<br />
<p>This is first paragraph.</p>
<br />
<p>This is another paragraph.</p>
<!-- end snippet -->
The above code produces the same output even if you remove the <br>
tag between the <p>
and the <a>
tag. <br>
tag breaks the line and <p>
being a block level element should start in a new line, i.e. it should again breaks the line before itself. So shouldn't there be an empty line in between the <p>
and the <a>
tag in the output. But in the output, there's no empty line between them. Also when subsequently adding more <br>
tags between <a>
and <p>
, it does create more empty lines. But just one <br>
tag makes no difference, why ?
I tried adding a <br>
tag in between an <a>
tag and a <p>
tag hoping that this would create an empty line in between the <a>
and the <p>
tag. But it didn't happen so with just one <br>
tag and it produces the same output even if there were no <br>
tag. Why is it so ?
答案1
得分: 1
这是因为<p>
元素是一个块级元素,这意味着它占据其父容器的整个宽度,并在元素之前和之后创建新的行。所以,即使在<p>
元素之前有一个换行符,浏览器仍然会将它视为与之前的文本在同一行上。
你可以使用display: block
将你的<a>
元素显示为块级元素以在它们之间创建空间:
a {
display: block;
}
<a href="google.com">google</a>
<br />
<a href="google.com">google</a>
<br />
<p>This is first paragraph.</p>
<br />
<p>This is another paragraph.</p>
此外,你也可以使用margins
来创建空间,而不必使用<br />
元素:
a {
display: block;
margin-bottom: 16px;
}
<a href="google.com">google</a>
<a href="google.com">google</a>
<p>This is first paragraph.</p>
<p>This is another paragraph.</p>
英文:
This is because a <p>
element is a block-level element, which means it takes up the full width of its parent container and creates a new line before and after the element. So, even though there is a line break before the <p>
element, the browser will still treat it as being on the same line as the text that precedes it.
You can do your <a>
element displayed like block
with display: block
to got space between them:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
a {
display: block;
}
<!-- language: lang-html -->
<a href="google.com">google</a>
<br />
<a href="google.com">google</a>
<br />
<p>This is first paragraph.</p>
<br />
<p>This is another paragraph.</p>
<!-- end snippet -->
Also, you can use margins
, to create space without <br />
elements:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
a {
display: block;
margin-bottom:16px;
}
<!-- language: lang-html -->
<a href="google.com">google</a>
<a href="google.com">google</a>
<p>This is first paragraph.</p>
<p>This is another paragraph.</p>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论