html - 当容器具有最小和最大高度时,CSS overflow auto 不起作用

标签 html css

我有一个具有最小和最大高度的容器 div。

里面有 2 个列样式的 div,一个宽度为 80%,另一个宽度为 20%

80% 的人将有一个 <img />占据其容器的全高和宽度。

20% 的将有缩略图。我在此设置了自动溢出,以便用户可以滚动浏览缩略图并选择一个,但自动溢出没有任何效果。我无法设置容器的确切高度,因为它必须是可变的。

如何在带有缩略图的 div 中使用滚动条来实现溢出?

请引用这个插件:https://plnkr.co/edit/el2KNvEJdpUfA6v24uTV?p=preview

代码:

<div class="container">
    <div class="gallery-main">
      <div class="img-container">
        <img src="http://lorempixel.com/800/600" alt="">
      </div>
    </div>
    <div class="gallery-list">
        <img src="http://lorempixel.com/800/600?test=1" alt="">
        <img src="http://lorempixel.com/800/600?test=2" alt="">
        <img src="http://lorempixel.com/800/600?test=3" alt="">
    </div>
</div>
<style>
 .container{
    min-width: 420px;
    min-height: 316px;
    max-height: 337px;
    max-width: 540px;
}
.gallery-main{
    width: 79%;
    display: inline-block;
    vertical-align: top;
    max-height: inherit;
    height: 100%;
    text-align: center;
}
.gallery-list {
    width: 20%;
    overflow: auto;
    display: inline-block;
    vertical-align: top;
    height: inherit;
    max-height:100%;
}

.gallery-list img {
    width: 100%;
    height: auto;
    margin: 5px 0;
    border-radius: 5px;
}
.img-container {
    height: inherit;
    width: 100%;
    display: inline-block;
    max-height: inherit;
}
.img-container img {
    border-radius: 5px;
    max-height: 100%;
    max-width:100%;
}
</style>

解决方案是在容器上设置相对位置,在行为不当的 div 上设置绝对位置。 (看来,CSS 并不是精确的科学)。使用解决方案更新了 Plunker,以便其他人可以找到它:https://plnkr.co/edit/el2KNvEJdpUfA6v24uTV?p=preview

最佳答案

编辑:这是我的一个片段。我认为这可以满足您的需求。

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <style>
.container{
      min-width: 420px;
    min-height: 316px;
    max-height: 337px;
    max-width: 540px;
    position: relative;
}
.gallery-main{
    width: 79%;
    display: inline-block;
    vertical-align: top;
    max-height: inherit;
    height: 100%;
    text-align: center;
}
.gallery-list {
    width: 20%;
    overflow: auto;
    display: inline-block;
    vertical-align: top;
    height: inherit;
  max-height:100%;
  position: absolute;
}

.gallery-list img {
    width: 100%;
    height: auto;
    margin: 5px 0;
    border-radius: 5px;
}
.img-container {
    height: inherit;
    width: 100%;
    height: 100%;
    display: inline-block;
}
.img-container img {
    border-radius: 5px;
    height: 100%;
    width:100%;
}
    </style>
  </head>

  <body>
    <div class="container">
    <div class="gallery-main">
      <div class="img-container">
        <img src="http://lorempixel.com/800/600" alt="">
      </div>
    </div>
    <div class="gallery-list">
        <img src="http://lorempixel.com/800/600?test=1" alt="">
        <img src="http://lorempixel.com/800/600?test=2" alt="">
        <img src="http://lorempixel.com/800/600?test=3" alt="">
        <img src="http://lorempixel.com/800/600?test=4" alt="">
        <img src="http://lorempixel.com/800/600?test=5" alt="">
        <img src="http://lorempixel.com/800/600?test=6" alt="">
        <img src="http://lorempixel.com/800/600?test=7" alt="">
        <img src="http://lorempixel.com/800/600?test=8" alt="">
        <img src="http://lorempixel.com/800/600?test=9" alt="">
        <img src="http://lorempixel.com/800/600?test=10" alt="">
        <img src="http://lorempixel.com/800/600?test=11" alt="">
    </div>
  </div>
  </body>

</html>

关于html - 当容器具有最小和最大高度时,CSS overflow auto 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277799/

相关文章:

html - z-index 不起作用

javascript - 如何用图像替换表单按钮,并在悬停时更改图像?还有 Sprite 与单独的图像?

jquery - 当通过单击按钮隐藏右侧/左侧网格并返回原始形式时,如何以全宽显示网格?

javascript - 刷新页面时阻止 safari 显示未保存的表单提示

html - nth-of-type(x)(带有媒体查询)在 Safari 9.1.1 中无法正常工作

java - Vaadin 背景图像设置大小?

javascript - jQuery如何获取父元素的兄弟元素

javascript - 多个下拉菜单和单选按钮

javascript - Bootstrap 中的水平和垂直可滚动表

javascript - 如何将 php 中接收到的数据分类为 html 中的 3 个不同的 div 标签?