如何缩小bootstrap轮播中的图像

时间:2017-02-23 15:50:16

标签: javascript jquery html css twitter-bootstrap

我正在尝试构建一个旋转木马,作为我的网站的背景图像封面,类似于wordpress中的革命性滑块但不是插件。我只为我的静态网站建立它。问题是我的一些图像的高度和宽度不同,所以旋转木马没有正确调整。例如img no。 1是2048 * 3072 px 2nd是3072 * 2048 px等等。我想让我的旋转木马高度和宽度像封面图像一样100%。当我将min-width和height分别设置为100%和100vh时,图像从底部被裁剪。任何帮助?这是我的代码

 <!--carousel-->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
 <!-- Indicators -->
   <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>
    </ol>

    <!-- Wrapper for slides -->
     <div class="carousel-inner" role="listbox">
     <div class="item active">
     <img class ="fill"src="img/msp_0409_3522.jpg" alt="Chania">
    </div>

     <div class="item">
      <img class ="fill" src="img/msp_0406_1786.jpg" alt="Chania">
     </div>

     <div class="item">
      <img class ="fill" src="img/msp_1608_9566.jpg" alt="Flower">
     </div>

    <div class="item">
     <img class ="fill" src="img/msp_1605_6918.jpg" alt="Flower">
    </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-                                 slide="prev">
   <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-    slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true">    </span>
     <span class="sr-only">Next</span>
     </a>
    </div>

CSS

  html,body{
  height:100%;
  }
  .carousel,.item,.active{
 height:100%;
   }
 .carousel-inner{
 height:100%;
 }
 .fill{
  width:100%;
  height:100%;
  background-position:center
  ;background-size:cover;
  }

3 个答案:

答案 0 :(得分:1)

在您的代码中,.carousel-inner>.item>img似乎会覆盖您的填充类。为了覆盖bootstrap属性,在类中添加!important。

.fill {
    width: 100%;
    height: 100% !important;
}

您也不需要background-positionbackground-size,因为它们只会设置背景图片样式

Code Pen link

答案 1 :(得分:0)

你应该使用object-fit属性。 你可以试试这样的东西:

.carousel-inner img {
  object-fit: contain;
}

https://css-tricks.com/almanac/properties/o/object-fit/

答案 2 :(得分:0)

对于背景图片,您可以使用:

stdin