简体   繁体   中英

Bootstrap 4 fixed image height

I am using Bootstrap's 4 grid system to display randomly sized image from unsplash.com into a web-page.

The issue I have is that I do not know how can I make a fixed sized thumbnail for all images, regardless of their height but at the same time fit it into it's container.

Here is a sample:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous" /> </head> <body> <div class="container-fluid mt-5 py-3 py-md-5"> <div class="row text-center text-lg-left"> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="" alt="" /> </a> </div> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="" alt="" /> </a> </div> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="" alt="" /> </a> </div> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="" alt="" /> </a> </div> </div> </div> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <script> const imageCards = document.querySelectorAll(".image-card"); const url = "https://source.unsplash.com/collection/308700/300/?sig="; const randomNum = () => { return Math.floor(Math.random() * 100000); }; for (let image of imageCards) { let src = `${url}${randomNum()}`; image.setAttribute("src", src); } </script> </body> </html>

样本

Is there any bootstrap specific class I can apply to have the same image height and also fit it into it's parent div?

I use the padding top trick to give me the aspect ratio of the image and then I can absolutely position the image inside the anchor and use object fit on it (may need a polyfill for ie):

 @import url('https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css'); /* use classes instead of these element selectors */ a { display: block; padding-top: 56.25%; position: relative; } a>img { display: block; width: 100%; height: 100%; max-height: 100%; object-fit: cover; position: absolute; top: 0; left: 0; }
 <div class="container-fluid mt-5 py-3 py-md-5"> <div class="row text-center text-lg-left"> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="https://www.fillmurray.com/200/300" alt="" /> </a> </div> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="https://www.fillmurray.com/300/300" alt="" /> </a> </div> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="https://www.fillmurray.com/300/200" alt="" /> </a> </div> <div class="col-lg-3 col-md-4 col-6"> <a asp-area="" asp-controller="Galerie" asp-action="FotoNunta"> <img class="image-card img-fluid img-thumbnail" src="https://www.fillmurray.com/200/400" alt="" /> </a> </div> </div> </div>

One solution would be setting a height to the img-thumbnail and then giving it a object-fit:

<style>
.img-thumbnail {
  height: 350px;
  object-fit: cover;
}

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