在段落 (X)HTML 和 CSS 之间留出一个空格

发布于 2024-08-05 17:35:29 字数 450 浏览 5 评论 0原文

我希望我的

content 标签之间有空格。不在

标签之前或之后。例如,我的代码是:

<div>
   <h1>A headline</h1>
   <p>Some text</p>
   <p>Some text</p>
</div>
Something

我不希望 h1 和 p 之间有空格,这是在 h1 上以零边距完成的。但我不希望最后一个

标记后有空格。如果没有 :last-child 或一些 JavaScript/jQuery,这可能吗?

我无法在最后一个标签上设置class="last",因为它是 CMS 系统。

I want space between my <p>content</p> tags. Not before and not after <p> tags. For example, my code is:

<div>
   <h1>A headline</h1>
   <p>Some text</p>
   <p>Some text</p>
</div>
Something

I don't want space between h1 and p which is done with zero margin on h1. But I don't want space after the last <p> tag. Is this possible without :last-child or some JavaScript/jQuery?

I can't set class="last" on the last tag because it is a CMS system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

意中人 2024-08-12 17:35:29
p + p {
  margin-top: 1.5em;
}

(尽管这需要比 IE6 更好地支持 CSS 的浏览器。)

p + p {
  margin-top: 1.5em;
}

(Although this requires a browser with better support for CSS than IE6.)

酒解孤独 2024-08-12 17:35:29

如果您不需要支持 Internet Explorer 6 (IE6),您可以使用:

div, h1, p { margin: 0; }
p + p { margin-top: 12px; }

如果您需要支持 IE6,这是一个肮脏的 hack,但无需 JavaScript 即可工作:

div, h1, p { margin: 0; }
h1 { margin-bottom: -12px; }
p { margin-top: 12px; }

此方法的缺点是您不能简单地使用 1em 等来平衡边距,因为它们具有不同的字体大小。您可以根据需要手动调整或使用像素宽度。

If you're not required to support Internet Explorer 6 (IE6) you can use:

div, h1, p { margin: 0; }
p + p { margin-top: 12px; }

If you need to support IE6, this is a dirty hack but it works without JavaScript:

div, h1, p { margin: 0; }
h1 { margin-bottom: -12px; }
p { margin-top: 12px; }

The disadvantage of this method is that you can't simply use, say, 1em for the balancing margins as they have different font sizes. You can either manually adjust as required or use pixel widths.

温折酒 2024-08-12 17:35:29

将默认下边距设置为 p,然后为最后一个标签指定一个没有下边距的类。

Set a default bottom-margin to p, then give the last tag a class with no bottom-margin.

一个人的夜不怕黑 2024-08-12 17:35:29
<div>
   <h1>A headline</h1>
   <p>Some text</p>
   <div class="paragraph-space"></div>
   <p>Some text</p>
</div>

<div>
   <h1>A headline</h1>
   <p>Some text</p>
   <div class="paragraph-space"></div>
   <p>Some text</p>
</div>

?

孤寂小茶 2024-08-12 17:35:29
<div>
  <h1>A headline</h1>
  <p>Some text</p>
  <p style="margin-bottom: 0;">Some text</p>
</div>
<div>
  <h1>A headline</h1>
  <p>Some text</p>
  <p style="margin-bottom: 0;">Some text</p>
</div>
萌辣 2024-08-12 17:35:29

如果您想让它与浏览器更加兼容,您还可以使用:

p { margin-top: 24px; }
h1 { margin-bottom: -24px; } /* Play with this value as necessary */

负边距会将元素拉向它们。它比我喜欢的更混乱,但是当您受制于 CMS 生成的代码而无法添加类时,您必须发挥创造力。

If you want to make it more browser compatible, you could also use:

p { margin-top: 24px; }
h1 { margin-bottom: -24px; } /* Play with this value as necessary */

Negative margins will pull elements toward them. It's messier than I like, but when you are at the mercy of CMS generated code with no way to add classes, you have to be creative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文