删除<p>标记HTML上方和下方的空格

OVE*_*ONE 28 html css

太简单了.

以此ul为例.

<ul>
        <li>HI THERE</li>
        <br>
        <li>
           <p >ME </p>
        </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

当Li标签的innerHtml为空时,Li将自己包裹到文本中.

P标签不会发生这种情况.我假设这是因为p是段落之前和之后通常有空格.

我的问题:

有什么办法可以删除吗?

Que*_*tin 53

<p>元素通常具有边距和/或填充.您可以在样式表中将它们设置为零.

li p {
    margin: 0;
    padding: 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,从语义上讲,拥有一个段落列表是相当不寻常的.

  • 或者您可以使用 `&lt;span&gt;` 而不是 `&lt;p&gt;` 来添加该 ID :) (2认同)

Sar*_*est 7

看这里:http://www.w3schools.com/tags/tag_p.asp

p元素自动在其自身之前和之后创建一些空间.该空间由浏览器自动应用,或者您可以在样式表中指定它.

您可以使用css删除额外的空间

p {
   margin: 0px;
   padding: 0px;
}
Run Code Online (Sandbox Code Playgroud)

或者使用<span>没有默认边距的元素,它是一个内联元素.

  • 请不要将[dreadful](http://w3fools.com/)W3Schools的工作错误归因于W3C. (3认同)

use*_*388 5

如果有人希望使用 bootstrap 来做到这一点,版本 4 提供了以下功能:

这些类使用 xs 的格式 {property}{sides}-{size} 和 sm、md、lg 和 xl 的 {property}{sides}-{breakpoint}-{size} 格式命名。

其中财产是以下之一:

m - for classes that set margin
p - for classes that set padding
Run Code Online (Sandbox Code Playgroud)

其中 side 是以下之一:

t - for classes that set margin-top or padding-top
b - for classes that set margin-bottom or padding-bottom
l - for classes that set margin-left or padding-left
r - for classes that set margin-right or padding-right
x - for classes that set both *-left and *-right
y - for classes that set both *-top and *-bottom
blank - for classes that set a margin or padding on all 4 sides of the element
Run Code Online (Sandbox Code Playgroud)

其中大小是以下之一:

0 - for classes that eliminate the margin or padding by setting it to 0
1 - (by default) for classes that set the margin or padding to $spacer * .25
2 - (by default) for classes that set the margin or padding to $spacer * .5
3 - (by default) for classes that set the margin or padding to $spacer
4 - (by default) for classes that set the margin or padding to $spacer * 1.5
5 - (by default) for classes that set the margin or padding to $spacer * 3
auto - for classes that set the margin to auto
Run Code Online (Sandbox Code Playgroud)

例如:

.mt-0 {
  margin-top: 0 !important;
}

.ml-1 {
  margin-left: ($spacer * .25) !important;
}

.px-2 {
  padding-left: ($spacer * .5) !important;
  padding-right: ($spacer * .5) !important;
}

.p-3 {
  padding: $spacer !important;
}
Run Code Online (Sandbox Code Playgroud)

参考: https: //getbootstrap.com/docs/4.0/utilities/spacing/