html - 图像适合特定尺寸的 flexbox

标签 html css image

所以,我刚才提出了一个类似的问题,关于 img 如何适合 div,答案是设置 height: 100%width: 100%的形象,它运作良好。但是现在我还在那个 div 中使用了一个 flexbox 并且 img 仍然在底部溢出......

.d1 {
  background: green;
  width: 300px;
  height: 100px;
  padding: 5px;
}

.d2 {
  background: red;
  display: flex;
  padding: 5px;
}

.img1 {
  width: 100%;
  height: 100%;
}
<div class="d1">
  <div class="d2">
    <img class="img1" src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  </div>
</div>

最佳答案

height:100% 赋给图像的父 div。同时调整您的填充或使用 calc 设置高度 height:calc(100% - 10px) 见下文。

.d1 {
  background: green;
  width: 300px;
  height: 100px;
  padding: 5px;
}

.d2 {
  background: red;
  display: flex;
  padding: 5px;
  height: calc(100% - 10px);
}

.img1 {
  width: 100%;
  height: 100%;
}
<div class="d1">
  <div class="d2">
    <img class="img1" src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  </div>
</div>

希望这有帮助:)

关于html - 图像适合特定尺寸的 flexbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58386567/

相关文章:

jquery - 滚动时 Wordpress Logo 更改

javascript - 幻灯片顶部的菜单故障

image - 负互相关显示高相似度还是低相似度?

javascript - 如何使用一个大图像并将其分解为网站的小块?

image - Titanium:在不创建 ImageView 的情况下获取 png 高度

html - 如何将表单选择上的下拉按钮移动到左侧?

html - 是否可以防止在 HTML 中包装子元素?

javascript - Spring-jsp页面中未加载css和图像文件

html - Flex 布局中的动态列宽

html - 响应式图标菜单子(monad)菜单