html - 即使不需要滚动条也可见 -

标签 html css overflow

我在使用 overflow:auto 时出错。

错误:http://cl.ly/image/0K1W3t151T0S

我用的代码http://codepen.io/sebazelonka/pen/pDGin

即使内容的高度与容器的高度相同,滚动条也是可见的。我尝试了不同的选项,但错误仍然存​​在。

我在FF、Chrome、Safari、Opera等不同的浏览器上试过,总是报同样的错误。

HTML

<div class="image-viewport portrait" style="width: 100%; height: 400px;">
      <div class="image-wrapper" style="width: 100%; height: 400px;">
           <img src="http://www.hdwallpapersview.com/wp-content/uploads/2013/05/landscape_7.jpg">
      </div>
</div>

CSS

body {
  background: #999;
}

.image-viewport {
  overflow: auto;
}

.image-wrapper {
  background: #333;
  text-align: center;
}

.image-viewport.portrait img {
  height: 100%;
}

最佳答案

这里有两种不同的解决方案:

  • vertical-align:top 添加到img 元素。 (默认为 vertical-align:baseline)

  • img 更改为 block 级别的元素。

Updated Codepen example使用 vertical-align:top

.image-viewport.portrait img {
    height: 100%;
    vertical-align:top;
}

Updated Codepen example使用 display:block

注意:对于水平居中,使用 margin:0 auto,因为 text-align:center 将不再起作用,因为元素不再是 inline 元素。

.image-viewport.portrait img {
    height: 100%;
    display:block;
    margin:0 auto;
}

此外,如果窗口太小,请不要与添加在主体上的滚动条混淆。 img 包装器上的滚动条已被删除。

关于html - 即使不需要滚动条也可见 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20223977/

相关文章:

html - 可变长度定价表的居中内容

html - 我创建的 Material UI 表有额外的 div 标签,这会破坏我的 CSS

html - LI 在浏览器调整大小时移动不正确

html - 一起使用 "transform"和 "table"

actionscript-3 - 将溢出链接集成到菜单列表末尾的动态下拉菜单中

javascript - 如何在没有任何外部软件的情况下在php中获取html5视频的观看时间?

javascript - 如何设置动画定位固定 :before selector With jQuery

html - Textarea 仅在换行符处自动换行

java - 常见的下溢和上溢异常

javascript - 使用jquery单击按钮上的slidetoggle div容器