<p>和<a>之间没有空行是因为它们之间有一个<br>。

huangapple go评论50阅读模式
英文:

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 -->

&lt;a href=&quot;google.com&quot;&gt;google&lt;/a&gt;
&lt;br /&gt;
&lt;a href=&quot;google.com&quot;&gt;google&lt;/a&gt;
&lt;br /&gt;
&lt;p&gt;This is first paragraph.&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;This is another paragraph.&lt;/p&gt;

<!-- end snippet -->

The above code produces the same output even if you remove the &lt;br&gt; tag between the &lt;p&gt; and the &lt;a&gt; tag. &lt;br&gt; tag breaks the line and &lt;p&gt; 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 &lt;p&gt; and the &lt;a&gt; tag in the output. But in the output, there's no empty line between them. Also when subsequently adding more &lt;br&gt; tags between &lt;a&gt; and &lt;p&gt;, it does create more empty lines. But just one &lt;br&gt; tag makes no difference, why ?

I tried adding a &lt;br&gt; tag in between an &lt;a&gt; tag and a &lt;p&gt; tag hoping that this would create an empty line in between the &lt;a&gt; and the &lt;p&gt; tag. But it didn't happen so with just one &lt;br&gt; tag and it produces the same output even if there were no &lt;br&gt; tag. Why is it so ?

答案1

得分: 1

这是因为&lt;p&gt;元素是一个块级元素,这意味着它占据其父容器的整个宽度,并在元素之前和之后创建新的行。所以,即使在&lt;p&gt;元素之前有一个换行符,浏览器仍然会将它视为与之前的文本在同一行上。

你可以使用display: block将你的&lt;a&gt;元素显示为块级元素以在它们之间创建空间:

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来创建空间,而不必使用&lt;br /&gt;元素:

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 &lt;p&gt; 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 &lt;p&gt; element, the browser will still treat it as being on the same line as the text that precedes it.

You can do your &lt;a&gt; 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 -->

&lt;a href=&quot;google.com&quot;&gt;google&lt;/a&gt;
&lt;br /&gt;
&lt;a href=&quot;google.com&quot;&gt;google&lt;/a&gt;
&lt;br /&gt;
&lt;p&gt;This is first paragraph.&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;This is another paragraph.&lt;/p&gt;

<!-- end snippet -->

Also, you can use margins, to create space without &lt;br /&gt; elements:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

a {
  display: block;
  margin-bottom:16px;
}

<!-- language: lang-html -->

&lt;a href=&quot;google.com&quot;&gt;google&lt;/a&gt;
&lt;a href=&quot;google.com&quot;&gt;google&lt;/a&gt;
&lt;p&gt;This is first paragraph.&lt;/p&gt;
&lt;p&gt;This is another paragraph.&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 22:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定