在html中跳过一行的最佳方法是什么?

Leb*_*one 3 html css html5 frontend css3

我已经阅读并访问了很多网站,但没有一个网站为我提供了一个简单的解决方案.我想知道的是在html中添加/跳过一行的最佳方法是什么?我主要使用的是两个<br />标签,但我知道有一个更简单的解决方案.有没有办法使用css跳过一行,而不是这样做:

<p>Hello. <br /><br />This is a test</p> 
Run Code Online (Sandbox Code Playgroud)

nic*_*ost 14

您可以简单地使用2个单独的paragraph(<p>)标记.例如:

<p>Hello.</p>
<p>This is a test</p> 
Run Code Online (Sandbox Code Playgroud)

这是一个演示.


Mr *_*ter 5

从语义上讲,这取决于您的目的。在这种情况下做最好的事情。一些例子。

  1. 如果您确实需要跳过一行,即文本中有一个空行,其中没有任何内容,那么请务必使用两个<br>元素。

    This is one line of text<br>
    This is also one line of text<br>
    The next line happens to be empty<br>
    <br>
    And here is another line, not empty<br>
    
    Run Code Online (Sandbox Code Playgroud)

    等等。

  2. 但是,如果您想在两个散文块之间创建一些空白,那么这两个块应该是段落,如其他答案中所述。

  3. 如果第一部分是一堆单独的行,但第二部分是一篇散文,那么只有第二部分需要是一个段落。<p>也无需将第一部分包含在标签中。例子:

    Add the water to the recipe<br>
    Add the salt<br>
    
    <p>Note: stir thoroughly!</p>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 如果您的目的是实际分隔两行文本,以表示它们不属于在一起,则可以使用<hr>. 如果需要,可以将其设为不可见。

    This is some text
    <hr style="opacity:0">
    This is some unrelated text
    
    Run Code Online (Sandbox Code Playgroud)
  5. 如果下一行恰好是文本后半部分的介绍,请将其更改为 a <h4>(即,此处需要的任何级别的标题)。

    The last line of the previous section
    <h4>Introduction</h4>
    The fist line of the next section
    
    Run Code Online (Sandbox Code Playgroud)

    这是可行的,因为标题也有内置的边距。

  6. 最后,如果您有一个现有的 html 片段,其中两个文本块已经位于两个 HTML 元素中,则不一定需要更改标记;只需更改标记即可。您需要做的就是在 CSS 中设置这些元素的顶部和底部边距。
    假设它们在<section>s 中:

    <style>
      section {margin:1em 0}
    </style>
    ...
    ... The last line of the previous section</section>
    <section>The fist line of the next section ...
    
    Run Code Online (Sandbox Code Playgroud)