【问题标题】:Margin collapsing in Google Chrome is different than other browsers for a <button> elementGoogle Chrome 中的边距折叠与 <button> 元素的其他浏览器不同
【发布时间】:2012-09-14 03:58:26
【问题描述】:

我终于找到了问题的原因,但我无法确定解决方案,所以需要您的帮助!

我正在尝试设置按钮的样式,它在 FireFox 和 Internet Explorer 中看起来很棒,但在 Chrome 中却不行!我在这里使用负边距,但即使它们是正边距,问题仍然存在。

下面是代码,简化以说明问题:

<div style="display: inline-block;">
    <span style="display: block; margin: -20px; width: 100px; height: 100px;"> div </span>
</div> <!-- DIV works the same in all browsers -->

<button style="display: inline-block;">
    <span style="display: block; margin: -20px; width: 100px; height: 100px;"> button </span>
</button> <!-- BUTTON ignores margins in Chrome only -->

​ 这是 FireFox 中的预期结果:

这是我在 Chrome 中看到的问题:

自己看:http://jsfiddle.net/ScottRippey/SZV45/13/

在我看来,边距被忽略了。但是,我似乎无法禁用按钮的边距折叠!
我试过了:display: inline-block; position: absolute; margin: 1px; padding: 1px; border: 1px solid white;

有什么想法吗?

【问题讨论】:

  • 改天,又一个 WebKit 错误...
  • @BoltClock 这不是我想听到的!来吧,想跳出框框

标签: css html google-chrome margin


【解决方案1】:

丢弃否定的margin,将width/height移动到.button(并调整为margin),并改用positionexample):

<div class="button">
    <span class="inner"> div </span>
</div>

<button class="button">
    <span class="inner"> button </span>
</button>
.button {
    display: inline-block;
    background: rgba(255,0,0,0.5);
    border: none;
    padding: 0;
    margin: 20px;
    position: relative;
    width: 60px;
    height: 60px;
    vertical-align: top;
}
.inner {
    display: block;
    background: rgba(0,0,255,0.5);
    position: absolute;
    top: -20px;
    bottom: -20px;
    left: -20px;
    right: -20px;
}

【讨论】:

  • +1 好答案,谢谢。但是,我不能接受它,因为我忘了提到我实际上是使用负边距来实现我想要的布局。我会相应地更新我的问题,但感谢您的正确答复。
猜你喜欢
  • 2021-12-08
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 2010-10-20
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多