css - 如何使图像缩小以适合固定位置的 flexbox 模态

标签 css internet-explorer flexbox microsoft-edge

如何使图像缩小以适合固定位置的 flexbox 模态?

尝试下面的图像时,图像会与页眉/页脚重叠。它需要在 IE 中同时适用于高或宽的图像。

.modal-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.modal-content {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.modal-header {
  background: red;
  margin: 20px;
}

.modal-body {
  background: green;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 0 20px;
}

.modal-footer {
  background: blue;
  margin: 20px;
}
<div class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        header
      </div>
      <div class="modal-body">
        <img src="https://via.placeholder.com/900x1200" />
      </div>
      <div class="modal-footer">
        footer
      </div>
    </div>
  </div>
</div>

请注意,这是一个简单的示例,我实际上使用的是复杂的 SVG,而不是图像标签,因此我不能使用背景图像。

最佳答案

您可以使用带有最大宽度和 最大高度 的旧式 position: absolute:

/* for demonstration I have replaced all irrelavant styles with this */
.modal-body {
  width: 80vw;
  height: 60vh;
  margin: 10% auto 0;
  background: green;
}
/* parent must be positioned */
.modal-body {
  position: relative;
}
/* size and center */
.modal-body img {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  max-width: 100%;
  max-height: 100%;
  margin: auto;
}
<div class="modal-body">
  <img src="https://via.placeholder.com/900x1200" />
</div>

关于css - 如何使图像缩小以适合固定位置的 flexbox 模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55229960/

相关文章:

javascript - InnoSetup - 有没有办法为 Internet Explorer 手动创建 cookie?

html - 如何使用 flexbox 制作灵活的 2x3 网格

css - 为什么这个 flexbox 布局在 Safari 中被破坏了?

html - 水平和垂直居中 bootstrap 连续 4 列

html - 标签在动画的一部分时不会激活相关字段

html - 盒子阴影和IE9兼容性

css - 在 IE 或 Edge 中不显示 css 类中带有图像的 Div

html - 选择CSS中的每三个元素

jquery - 如何在移动设备上处理宽表

asp.net - ajax请求返回json数组,IE6/7正在缓存它并且数据不新鲜