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

The best way to skip a line in html?

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

<p>Hello. <br /><br />This is a test</p> 

你可以用div包围'Hello',然后加上css的maring-bottom,例如:

<p>
    <div style='margin-bottom: 40px;'>Hello.</div>
    This is a test
</p>

您可以简单地使用 2 个单独的段落 (<p>) 标签。例如:

<p>Hello.</p>
<p>This is a test</p> 

这是一个demo

我认为使用 br 标签总是一个坏主意。尝试使用段落,css 填充,css 边距,小时。尝试避免使用 br,因为它不是 semantic 并在您的站点 "helps the search" 引擎中使用正确的标签 "understand your site"

<p>Hello. <br /> &nbsp; <br> This is a test</p>

使用块级元素会有所帮助。例如,p 是一个块级元素,它会给出换行符。

因此您可以将文本分成两段。并将 margin/padding 设置为段落

使用 <br> 是一种糟糕的方法。

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

  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>
    

    以此类推

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

  3. 而如果第一部分是一堆单行,而第二部分是一篇散文,那么只需要第二部分是一个段落即可。也无需将第一部分包含在 <p> 标记中。示例:

    Add the water to the recipe<br>
    Add the salt<br>
    
    <p>Note: stir thoroughly!</p>
    
  4. 如果您的意图实际上是 分隔 两行文本,表示它们不属于一起,您可以使用 <hr> .如果你愿意,可以让它不可见。

    This is some text
    <hr style="opacity:0">
    This is some unrelated text
    
  5. 如果下一行恰好是对后半段文字的介绍,就把它改成<h4>(也就是你在任何层级的header此处需要)。

    The last line of the previous section
    <h4>Introduction</h4>
    The fist line of the next section
    

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

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

尝试在需要空白的地方使用它 space:

    &nbsp;

如果你想要多个空格space,那么就这样依次重复代码:

    &nbsp;
    &nbsp;

等等