简体   繁体   中英

Bootstrap carousel images compressed vertically in IE

My bootstrap carousel has a fixed height of 32rem and width of 100%. When an image is larger, it shows the middle portion of the image. This works well in Chrome/edge/FF. However, in IE, the image is compressed vertically to show the full image height in 32rem of vertical space. Any ideas on how to fix this?

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <style type="text/css">
    .row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}

.carousel-inner .carousel-item {
  max-height: 400px;
}
.carousel-inner .carousel-item img {
  max-width: none;
  object-fit: cover;
  overflow:hidden;

}

.carousel-caption {
  background-color:rgba(0,0,0,0.5);
}

main {min-height:460px;}


/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */

/* Carousel base class */
.carousel {
  margin-bottom: 4rem;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
  bottom: 3rem;
  z-index: 10;
}

/* Declare heights because of positioning of img element */
.carousel-item {
  height: 32rem;
}
.carousel-item > img {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  height: 32rem;
}

  </style>
</head>
<body>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script  src="https://code.jquery.com/jquery-3.4.1.min.js"  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="  crossorigin="anonymous"></script>      
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<main class="container">
<div id="myCarousel" class="carousel slide mb-5" data-ride="carousel">
  <ol class="carousel-indicators"><li data-target="#myCarousel" data-slide-to="0" class="active"></li><li data-target="#myCarousel" data-slide-to="1" ></li><li data-target="#myCarousel" data-slide-to="2" ></li><li data-target="#myCarousel" data-slide-to="3" ></li><li data-target="#myCarousel" data-slide-to="4" ></li>                    
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="images/img_113404.jpg" class="d-block w-100" alt="" />
      <div class="container">
        <div class="carousel-caption">
          <h2>Story heading</h2>
          <div>By: photographer</div>                               
          <a class="btn btn-primary" href="#" role="button" title="Click here to read the full story.">Read Story</a>
        </div>
      </div>
    </div>
  </div>
  <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>      
</main>

</body>
</html>

The picture is compressed because object-fit: cover is not supported by IE . You could use background-size: cover as a workaround.

You could add this part in CSS to deal with the IE 11 situation:

@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
    /* IE10+ CSS */
    .carousel-item.active {
        background-image: url(https://armi.usgs.gov/uploads/20200121_111904.jpg);
        background-size: cover;
        background-position: 50% 45%;
     }
     .carousel-item img {
        display:none !important;

     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM