英文:
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 -->
<div id="product-info">
<p><b>Test Test</b></p>
<p>Product Information:</p>
<span>A: Test</span>
<span>B: Test</span>
<span>C: Test</span>
</div>
<!-- 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 <p>
tag, and use a line break <br />
The natural margin of <p>
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 -->
<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>
<!-- end snippet -->
As a side note .. <span>
is designed to be inline. So something like
<p> I like the color <span style="color:#FF0000"> red </span> </p>
would be an example of how it's meant to be used. For stacked elements, use the <div>
tag instead, as it's display is naturally block
and will stack naturally without the need for additional CSS rules:
<div>A: Test</div>
<div>B: Test</div>
<div>C: Test</div>
答案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 -->
<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>
<!-- 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 > p:first-of-type {
margin-bottom: 0;
}
#product-info > p:last-of-type {
margin-top: 0;
}
<!-- language: lang-html -->
<div id="product-info">
<p><b>Test Test</b></p>
<p>Product Information:</p>
<span>A: Test</span>
<span>B: Test</span>
<span>C: Test</span>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论