html - img-fluid 无法正确调整图像高度

标签 html css bootstrap-4 fluid-layout

我正在使用 Bootstrap 4 并尝试创建一个包含 4 张图像的网格,其中 2 张在顶部,2 张在底部。我正在使用 img-fluid 类,但图像会根据宽度调整大小,这只会使图像高度过大并且被截断。我尝试设置 max-height: 100% 但没有成功。

出了什么问题?

.images-container {
  height: 95vh;
}

.nav-button {
  width: 100%;
  height: 50%;
  margin: auto;
  font-size: 5em;
  color: black;
}

.footer-container {
  text-align: center;
}

.bottom-nav-box {
  font-size: 1.5em;
  margin: 0;
}

.sim-image {
  max-height: 100%;
}

i {
  margin: auto;
}

body {
  height: 100%;
}

html {
  height: 100%;
}

footer {
  height: 5vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="col-sm-10 h-100 center-container">
  <div class="row  images-container">
    <div class="row top-images-row h-50">
      <div class="w-50 d-inline-block image-div">
        <img src="https://lorempixel.com/875/656/" class="sim-image" id="simulatorImageTopLeft">
      </div>
      <div class="w-50 d-inline-block image-div" id="simulatorImageTopRight">
        <img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageTopRight">
      </div>
    </div>

    <div class="row bottom-images-row h-50">
      <div class="col image-div center-block text-center">
        <img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomLeft">
      </div>
      <div class="col image-div center-block text-center">
        <div class="row">
          <div class="col center-block text-center">
            <img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomRight">
          </div>
          <div class="col center-block text-center">
            <img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomRight">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

最佳答案

不能 100% 确定我是否正确解释了问题,但如果您想要 2×2 的图像网格,那么您应该能够像这样相对快速地完成;

html:

<div class="panel">
     <img src="blah" />
     <img src="blah" />
     <img src="blah" />
     <img src="blah" />
</div>

CSS:

.panel {
     width = whateversizeyouwant;
     height = whateversizeyouwant;
     display: flex; 
     flex-wrap: wrap;    //the flex stuff should make it display 2×2
}

.panel > img {
     width: 50%;
     height: 50%;
}

这应该适用于任何一组 4 张图像。

如果您希望它们保持相同的宽高比,请在 .panel > img 下设置 height: auto。请记住,这不会给您一个完美的正方形,panel div 的大小会影响图像的大小。

关于html - img-fluid 无法正确调整图像高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55638791/

相关文章:

html - float Div 在 Zoom 上突然出现

css - Shiny 的模态 : Having width adjust automatically to content inside

css - ml-auto 没有将导航栏链接推向右侧

html - 单击鼠标从输入中移除焦点

css - 强制现有网站打印移动版本

html - 如果文档的字体大小以 em 指定,那么 "em"是什么?

html - CSS 叠加在图像顶部具有多种颜色

javascript - 是否可以在一页 HTML 内容上限制用户?

jQuery - 悬停不工作

html - 如何让<div>占满<td>的高度