-webkit-margin 在文本上添加不需要的边距

发布于 2024-11-02 11:48:23 字数 380 浏览 1 评论 0原文

直到现在我才意识到这一点(而且这不仅出现在 webkit 浏览器中)。在 p 标签、h1 标签等所有文本上...文本上方和下方都有一个额外的空格。

在 Chrome 中我发现了这个:

用户代理样式表

-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;

这会导致某些地方的对齐错误。是的,我正在使用重置样式表,并且没有添加填充或边距。几乎是基本设置。这是为什么?我该如何解决?

This hadn't hit me until now (and this is not only in webkit browsers). On all texts in like p tags, h1 tags etc... there's an extra space over and below the text.

In chrome I found this:

user agent stylesheet

-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;

This makes the alignment wrong in some places. And yes I'm using a reset stylesheet and no padding or margin are added. Pretty much a basic setup. Why is this and how do I solve it?

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

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

发布评论

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

评论(13

拍不死你 2024-11-09 11:48:24

对我来说,情况是:

* {margin:0;padding:0;}

Firefox (FF) 和 Google Chrome 无论如何都设置了 0.67em 的边距。
FF 显示了其默认边距,但被划掉,但还是应用了它。
GC 显示其默认边距 (-webkit-margin-before...) 未交叉。

我申请了,

* {margin:0;padding:0; -webkit-margin-before: 0; -webkit-margin-after: 0;}

但没有成功,尽管 GC 现在显示其默认保证金已经交叉。

我发现,我可以应用

line-height: 0;

font-size: 0;

来达到预期的效果。
如果假设边距是 0.67em 类型,那么这是有道理的。
如果有人能解释一下为什么浏览器通过应用行高相关的、不可移动的边距让我们的生活变得痛苦,我将非常感激。

For me, the picture was:

* {margin:0;padding:0;}

Firefox (FF) and Google Chrome both put 0.67em margins regardless.
FF showed its default margin, but crossed out, but applied it anyway.
GC showed its default margin (-webkit-margin-before...) uncrossed.

I applied

* {margin:0;padding:0; -webkit-margin-before: 0; -webkit-margin-after: 0;}

but to no avail, although GC now showed its default margin crossed.

I found out, that I can apply either

line-height: 0;

or

font-size: 0;

to achieve the desired effect.
This makes sense, if one assumes, that the margin is of the .67em - type.
If anybody could give an explanation, why browsers make our lives miserable by applying a line-height dependent, non-removable margin, i would be really grateful.

活泼老夫 2024-11-09 11:48:24

对我来说,在 Chrome 中,是一些 40px padding-start 导致了这个问题。我做了以下有效的操作:

ul {
    -webkit-padding-start: 0em;
}

For me in Chrome it was some 40px padding-start that was causing this. I did the following that worked:

ul {
    -webkit-padding-start: 0em;
}
微暖i 2024-11-09 11:48:24
    -webkit-margin-before: 0em;
    -webkit-padding-start: 0;
    -webkit-margin-after: 0em;

当我遇到这个问题时,这为我解决了。

    -webkit-margin-before: 0em;
    -webkit-padding-start: 0;
    -webkit-margin-after: 0em;

This solved it for me when I was having this exact problem.

南渊 2024-11-09 11:48:24

在您的 css 文件中添加以下内容。

* {
  -webkit-margin-before: 0em !important;
  -webkit-margin-after: 0em !important;
}

'*' 选择所有 css 元素并覆盖 webkit-margin。

In your css file add the following.

* {
  -webkit-margin-before: 0em !important;
  -webkit-margin-after: 0em !important;
}

'*' selects all css elements and overrides the webkit-margin.

轻许诺言 2024-11-09 11:48:24

现代属性

应改用以下属性。

margin-block-start: 0;
margin-block-end: 0;

很少需要使用这些,但以下内容对于避免系列中最后一段之后的额外空间很有用。

p:last-child
{
   margin-block-end: 0;
}

我还发现,即使在 Chrome 中,在某些情况下,您也可以通过设置继承的边距来触发“幽灵边距”。

https://developer.mozilla.org/en- US/docs/Web/CSS/margin-block-start

Modern properties

The following properties should be used instead.

margin-block-start: 0;
margin-block-end: 0;

It's very rare to need to use these at all, but the following can be useful to avoid extra space after the last paragraph in a series.

p:last-child
{
   margin-block-end: 0;
}

I also found that even in Chrome you can trigger the 'ghost margin' by setting margin to inherit in some cases.

https://developer.mozilla.org/en-US/docs/Web/CSS/margin-block-start

沐歌 2024-11-09 11:48:24

我也有同样的问题。突然,我的三个包含数据的表格单元格中的一个的标题向下移动了一点。我的问题只需添加以下内容即可解决:

table td
{
    vertical-align: top;
}

似乎“更高”样式表中的其他一些元素告诉我的数据将其自身置于单元格中,而不是仅仅停留在顶部。

我想这只是愚蠢的,并不是真正的问题......但下一个阅读此主题的人可能会犯和我一样的愚蠢错误:)

小心!

I had the same problem. Suddenly one out of my three table cells containing data its header was moved down a little bit. My problem was simply solved by adding this:

table td
{
    vertical-align: top;
}

Seems like some other element in a 'higher' style sheet was telling my data to center itself in the cell, instead of just staying on top.

I guess its just stupid, and wasnt really a problem... but the next person to read this topic might have the same stupid error as i did :)

Take care!

极致的悲 2024-11-09 11:48:24

如果用户代理样式表正在启动,那是因为标签属性没有在您的CSS样式表中正确定义。

在到达页面稍后引用或“需要”的标签属性设置之前,有可能是拼写错误、忘记括号或分号破坏了样式表。

通过错误检查运行 CSS,例如 CSS LINT 并修复最终检测到的错误。

If user agent stylesheet is kicking in, it is because the tag property was not properly defined in your css stylesheet.

Chances are that a typo, forgotten bracket or semicolon is breaking up your stylesheet BEFORE reaching the tag property settings your page later refers to or "needs".

Run your CSS thru error checking, like CSS LINT and fix whichever errors are eventually detected.

流云如水 2024-11-09 11:48:23

您还可以直接修改这些属性,如下所示:

-webkit-margin-before:0em;
-webkit-margin-after:0em;

Works for me in Chrome/Safari。希望有帮助!

You can also directly modify those attributes like so:

-webkit-margin-before:0em;
-webkit-margin-after:0em;

Works for me in Chrome/Safari. Hope that helps!

孤君无依 2024-11-09 11:48:23

这些 -webkit-marginmargin: 0; 覆盖填充:0;。不用担心他们。

额外空间?也许您已经设置了line-height:

These -webkit-margin(s) are overwritten by margin: 0; padding: 0;. Do not worry about them.

Extra space? Maybe you've set line-height:?

爱人如己 2024-11-09 11:48:23

我有同样的问题。在 Firefox 中显示正确,但在 Chrome 中显示不正确。

我仔细查看了 http://meyerweb.com/eric/tools/css/reset/ 并发现我没有在样式表中声明正文标记的通用行高。将其设置为 1.2,即可在两个浏览器中重新创建正确的布局。

I had the same issue. Displaying correctly in Firefox but not Chrome.

I had a closer look at http://meyerweb.com/eric/tools/css/reset/ and found that I hadn't declared a general line-height for the body tag in my stylesheet. Set it to 1.2 and that recreated the correct layout in both browsers.

娇纵 2024-11-09 11:48:23

只需删除标签之间的空格即可,例如

<p id="one"></p>
<p id="two"></p>

变为:

<p id="one"></p><p id="two"></p>

Just remove the whitespace between tags e.g.

<p id="one"></p>
<p id="two"></p>

becomes:

<p id="one"></p><p id="two"></p>
趁微风不噪 2024-11-09 11:48:23

我有同样的问题。菜单链接之间有额外的空间。上述解决方案均无效。对我有用的是负利润。
只需执行以下操作:

margin: 0 -2px;

新编辑:

这与 -webkit-margins 无关。您的问题很可能发生在行内元素上。发生这种情况是因为内联元素之间有空格或换行符。要解决此问题,您有很多选择:

  • 删除内联元素之间的所有空格和换行符
  • 跳过元素结束标记 - 例如 (HTML5 不关心)
  • 负边距(如上所述- IE6/7 的问题 - 谁在乎,升级;)
  • font-size: 0; 设置为有问题的内联元素容器(android 有问题,如果使用 ems 调整字体大小)
  • 放弃内联并使用float (这样你就可以松开 text-align:center)

解释更多具体内容和示例由 CHRIS COYIER 提供

I had a same problem. Extra space between menu links. None of the above solutions worked. What worked for me, was negative margin.
Just do something like this:

margin: 0 -2px;

NEW EDIT:

This has nothing to do with -webkit-margins. Most likely your problem occurs with inline elements. This happens because you have spaces or line breaks between your inline elements. To fix this, you have many options:

  • remove all spaces and line-breaks between inline elements
  • skip element closing tag - for example </li> (HTML5 does not care)
  • negative margin (as stated above - problems with IE6/7 - who cares, upgrade ;)
  • set font-size: 0; to problematic inline element container (has issues with android and if font-sizing with ems)
  • give up inline and use float (this way you loose text-align:center)

Explained more specifically and examples by CHRIS COYIER

盗心人 2024-11-09 11:48:23

我的

标记也遇到同样的问题。我尝试设置 margin:0;,但没有成功。

我发现我习惯性地使用 // 注释掉 css 中的行。我从来没有注意到它,因为它以前没有引起任何问题。但是当我在声明

之前的行中使用 // 时,它会导致浏览器完全跳过声明。当我用 // 换取 /**/ 时,我能够调整保证金。

这个故事的寓意:始终使用正确的注释语法!

I was having this same problem with my <h3> tag. I tried setting margin:0;, but it didn't work.

I found that I was habitually commenting out lines in my css by using //. I never noticed it because it hadn't caused any problems before. But when I used // in the line before declaring <h3>, it caused the browser to skip the declaration completely. When I traded out // for /**/ I was able to adjust the margin.

Moral of this story: Always use proper commenting syntax!

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