【问题标题】:Remove gap from bottom of fluid flex containers去除流体柔性容器底部的间隙
【发布时间】:2018-07-01 00:40:12
【问题描述】:

我有一个包含流体宽度/高度容器的 3 列 flex ul 列表。

当我将height 设置为60% 时,它会在底部创建一个间隙。 footer text 应该正对着容器。我需要使用% 而不是px,所以它会随着浏览器的大小而缩放。

在任何元素上设置max-height 似乎并不能消除差距。


https://jsfiddle.net/8ogm45w0/

HTML

<ul class="category">
  <li>
    <a href="#">
      <div class="photo-container">
        <img src="https://i.imgur.com/By5S6Rd.jpg">
      </div>
      <div class="title">Example</div>
    </a>
  </li>
  <li>
    <a href="#">
      <div class="photo-container">
        <img src="https://i.imgur.com/da2NTpb.jpg">
      </div>
      <div class="title">Example</div>
    </a>
  </li>
  <li>
    <a href="#">
      <div class="photo-container">
        <img src="https://i.imgur.com/RGVB4jY.jpg">
      </div>
      <div class="title">Example</div>
    </a>
  </li>
</ul>

<hr /> 
Footer

CSS

ul.category {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  margin: 0;
  padding: 0;
  list-style-type: none;
  max-height: 300px;
}

ul.category li {
  max-width: 32%;
  text-align: center;
  max-height: 300px;
}

.photo-container {
  height: 60%;
  max-height: 300px;
  overflow: hidden;
}

.photo-container img {
  width: 100%;
}

.title {
  margin-bottom: 0.5em;
  padding: 0.4em;
  color: #fff;
  font-weight: 700;
  background: #000;
}

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    更新答案:

    我花了很多时间试图弄清楚这一点,但最后.. :)

    JSFiddle Demo

    body, html {
        height: 100%;
        width: 100%;
        margin: 0;
    }
    
    .category {
        height: 60%;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        margin: 0;
        padding: 0;
        list-style-type: none;
        background: pink;
    }
    
    .category a {
        height: 100%;
        width: 32%;
    }
    
    .photo-container {
        height: 100%;
    }
    
    .photo-container img {
        display: block;
        height: 90%;
        width: 100%;
    }
    
    .title {
        height: 10%;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #fff;
        font-weight: 700;
        background: #000;
    }
    

    【讨论】:

    • 那是个错误,我只放了 300px 以显示它不影响它,并在更新的链接中删除了它们。我必须使用% 而不是px。看看当你缩放你的 jsfiddle 时,标题会从照片中分离出来。
    • @MattMcManis 我刚刚用你想要的结果更新了我的答案,让我知道你的想法。
    • 它更接近了,尽管现在它在缩放时会拉伸图像。它最初保留了外观并用overflow: hidden; 裁剪它。我现在不得不使用几个媒体查询来为每个页面大小调整它,因为我无法弄清楚。
    • 如果您应用 % 高度,图像将会拉伸,这是意料之中的。如果你不想要这个,你总是可以在 px 中应用一个固定的高度。这有什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2014-01-12
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多