繁体   English   中英

即使没有必要,滚动条也可见 -

[英]Scrollbar visible even when is not necessary -

我使用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级元素。

使用vertical-align:top 更新了Codepen示例

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

使用display:block 更新了Codepen示例

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

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

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

只需将.image-viewport类更改为:

.image-viewport {
  overflow: hidden;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM