没有在第一个 p 标签后添加新行。

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

No new line added after first p tag

问题

我一直在尝试去除第一个 p 后的默认换行,但没有成功。
第二个 p 应该在第一个 p 下面,但不在同一行。

英文:

I've been trying to remove the default new line after the first p but without any success.
The second p should be underneath the first one, but not on the same line.

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

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

span {
  float: left;
  clear: left;
}

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

&lt;div id=&quot;product-info&quot;&gt;
  &lt;p&gt;&lt;b&gt;Test Test&lt;/b&gt;&lt;/p&gt;
  &lt;p&gt;Product Information:&lt;/p&gt;

  &lt;span&gt;A: Test&lt;/span&gt;
  &lt;span&gt;B: Test&lt;/span&gt;
  &lt;span&gt;C: Test&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

Can someone suggests some hints?

答案1

得分: 1

使用单个<p>标记,并使用换行标签<br />

<p>标记的自然边距是1em,在标记元素下方的外边距(margin-bottom)。如果你想要移除这个外边距,可以添加以下样式:

p {
    margin: 0;
}

但如果你喜欢这个外边距,只想去掉两行之间的空白,只需使用换行标签。

<div id="product-info">
    <p>
        <b>Test Test</b>
        <br />
        Product Information:
    </p>

    <span>A: Test</span>
    <span>B: Test</span>
    <span>C: Test</span>
</div>

另外注意,<span>标签设计为内联元素。因此,像下面这样的用法:

<p>我喜欢颜色 <span style="color:#FF0000">红色</span></p>

是它的预期用法示例。对于堆叠的元素,可以使用<div>标签,因为它的默认显示方式是块级(block),会自然地堆叠,无需额外的CSS规则:

<div>A: Test</div>
<div>B: Test</div>
<div>C: Test</div>
英文:

Use a single &lt;p&gt; tag, and use a line break &lt;br /&gt;

The natural margin of &lt;p&gt; margin-bottom (below the element) of the tag is 1em. You can also remove that margin if you add

p {
    margin:0;
}

But if you like the margin, and just want the space between to two lines gone .. Just use line break.

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

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

span {
  float: left;
  clear: left;
}

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

&lt;div id=&quot;product-info&quot;&gt;
 &lt;p&gt;
    &lt;b&gt;Test Test&lt;/b&gt;
    &lt;br /&gt;
    Product Information:
 &lt;/p&gt;

 &lt;span&gt;A: Test&lt;/span&gt;
 &lt;span&gt;B: Test&lt;/span&gt;
 &lt;span&gt;C: Test&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

As a side note .. &lt;span&gt; is designed to be inline. So something like

&lt;p&gt; I like the color &lt;span style=&quot;color:#FF0000&quot;&gt; red &lt;/span&gt; &lt;/p&gt;

would be an example of how it's meant to be used. For stacked elements, use the &lt;div&gt; tag instead, as it's display is naturally block and will stack naturally without the need for additional CSS rules:

&lt;div&gt;A: Test&lt;/div&gt;
&lt;div&gt;B: Test&lt;/div&gt;
&lt;div&gt;C: Test&lt;/div&gt;

答案2

得分: 1

这不是换行。这是外边距。使用浏览器的文档检查器查看。在Chrome中,它看起来像这样:

p {
    display: block;
    margin-block-start: 1em;
    margin-block-end: 1em;
    ...
}

创建自定义类来移除第一个段落下方和第二个段落上方的外边距。

然后,如果您需要块级元素,请使用块级元素。浮动不是21世纪的良好布局策略。

.no-margin-top {
  margin-top: 0;
}

.no-margin-bottom {
  margin-bottom: 0;
}
<div id="product-info">
  <p class="no-margin-bottom"><b>Test Test</b></p>
  <p class="no-margin-top">Product Information:</p>

  <div>A: Test</div>
  <div>B: Test</div>
  <div>C: Test</div>
</div>
英文:

It's not a newline. It's margin. Use your browser's document inspector to see this. it looks like this in Chrome:

p {
    display: block;
    margin-block-start: 1em;
    margin-block-end: 1em;
    ...
}

Create custom classes to remove the margins below the first and above the second paragraph.

Then, if you want block-level elements, use block-level elements. Floats aren't a good layout strategy in the 21st century.

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

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

.no-margin-top {
  margin-top: 0;
}

.no-margin-bottom {
  margin-bottom: 0;
}

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

&lt;div id=&quot;product-info&quot;&gt;
  &lt;p class=&quot;no-margin-bottom&quot;&gt;&lt;b&gt;Test Test&lt;/b&gt;&lt;/p&gt;
  &lt;p class=&quot;no-margin-top&quot;&gt;Product Information:&lt;/p&gt;

  &lt;div&gt;A: Test&lt;/div&gt;
  &lt;div&gt;B: Test&lt;/div&gt;
  &lt;div&gt;C: Test&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 1

移除margin

span {
  float: left;
  clear: left;
}

#product-info > p:first-of-type {
  margin-bottom: 0;
}
#product-info > p:last-of-type {
  margin-top: 0;
}
<div id="product-info">
  <p><b>测试 测试</b></p>
  <p>产品信息:</p>

  <span>A: 测试</span>
  <span>B: 测试</span>
  <span>C: 测试</span>
</div>
英文:

Remove the margin.

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

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

span {
  float: left;
  clear: left;
}

#product-info &gt; p:first-of-type {
  margin-bottom: 0;
}
#product-info &gt; p:last-of-type {
  margin-top: 0;
}

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

&lt;div id=&quot;product-info&quot;&gt;
  &lt;p&gt;&lt;b&gt;Test Test&lt;/b&gt;&lt;/p&gt;
  &lt;p&gt;Product Information:&lt;/p&gt;

  &lt;span&gt;A: Test&lt;/span&gt;
  &lt;span&gt;B: Test&lt;/span&gt;
  &lt;span&gt;C: Test&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月7日 04:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953702.html
匿名

发表评论

匿名网友

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

确定