【问题标题】:Troubles with margin-bottom and vh unitmargin-bottom 和 vh 单位的问题
【发布时间】:2018-10-10 17:30:04
【问题描述】:

我有一个应该滚动浏览器窗口的 div。我设置了 73vh 的顶部和 100vh 的边距底部。所以效果应该是,将这个 div 滚动到“浏览器窗口之外”。在 Chrome 中,这就像一个魅力,但在 Firefox 中,margin-bottom 不是窗口高度。

HTML:

<div class="container">
  Hi!
</div>

CSS:

.container {
  position: relative;
  top: 73vh;
  margin-bottom: 100vh;
  background: red;
}

我已经创建了一个代码笔来重现:Code Pen example

在 Chrome 中打开 CodePen,红色框移出视图,在 Firefox 中打开 CodePen,红色框移动了一点。我想要Chrome的效果。

解决方案

基于 fcalderan 的回答:将顶部更改为边距顶部,然后它也适用于 FF。 CodePen

.container {
  position: relative;
  margin-top: 73vh;
  margin-bottom: 100vh;
  background: red;
}

【问题讨论】:

  • 如果您使用 margin-top 更改顶部,它会按预期工作。似乎firefox正在从元素的静态位置计算margin-bottom(就好像它在位置:静态)
  • 是的,使用 margin-top 而不是 top 它可以工作!看来我不应该把 top 和 margin 混为一谈。谢谢!
  • @fcalderan 澄清一下,当你说“静态位置”时,你的意思是如果元素没有被偏移,它会被定位在哪里?
  • @Daniel 完全正确。好像该值是在没有顶部偏移量的情况下计算出来的一样。

标签: html css


【解决方案1】:

当您将其设为边框底部而不是边缘底部/耸肩/时,它会起作用:

.container {
 position: relative;
 top: 73vh;
 border-bottom: 100vh solid transparent;
 background: red;
 background-clip:padding-box; // credit to @fcalderan
}

链接https://codepen.io/carolmckayau/pen/WapPvE

不是很好,但可以解决。

【讨论】:

  • 我建议使用透明边框,只是为了不覆盖最终定义的任何背景和background-clip: padding-box;(所以红色背景也不会填充边框区域)
  • @fcalderan 拾取 div 的红色背景,即使使用 box-sizing:content-box;溢出:隐藏在.container上
  • 哦,我忘记了背景剪辑!优秀的@fcalderan
猜你喜欢
  • 2014-05-23
  • 2010-09-22
  • 2013-04-12
  • 1970-01-01
  • 2011-10-31
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多