img-fluid没有正确调整图像高度

问题描述 投票:0回答:1

我正在使用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>
html css bootstrap-4 fluid-layout
1个回答
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的大小将影响图像的大小。

© www.soinside.com 2019 - 2024. All rights reserved.