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

时间:2019-04-11 18:10:49

标签: 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>

1 个答案:

答案 0 :(得分:1)

不能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的大小会影响图像的大小。