我有一幅想要响应容器大小的图像。
父容器(封面照片)的高度为180px。
图片的高度为853px。但是,当使用img-fluid时,图像不会保持容器高度为180px,而是保持其自身的高度为853px。



              <!doctype html>
              <html lang="en">
                <head>
                  <title>Hello, world!</title>
                  <!-- Required meta tags -->
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

                  <!-- Bootstrap CSS -->
                  <link rel="stylesheet" href="css/bootstrap.min.css">


                 <style>
                   .cover-photo {
                      height: 180px;
                    }
                   </style>

                </head>
                <body>
                	<div class="container-fluid">

                    <div class="container">
                      <div class="row">
                        <div class="cover-photo">
                          <img class="img-fluid" src="http://newlovewallpapers.in/images/hd-love-images-and-wallpaper/hd-love-images-and-wallpaper-11-1280.jpg" alt="Background profile">
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- Optional JavaScript -->
                  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
                  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
                  <script src="js/bootstrap.min.js"></script>
                  <!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script> -->
                  <script src="js/parallax.js"></script>
                </body>
              </html>





我知道Bootstrap中的img-fluid是最大宽度:100%;和高度:自动;并应用于图像,以便与父元素缩放。但是在这种情况下,它不会缩放到父级高度。
有什么见解吗?谢谢!

最佳答案

早上好

您需要给img-fluid类提供以下CSS

 .img-fluid {
        max-height: 100%;
        max-width: 100%;
        height: auto;
        width: auto;
    }

09-25 18:14
查看更多