调整窗口大小时,Center Bootstrap Carousel图像

时间:2016-03-29 19:44:58

标签: html css twitter-bootstrap css3 carousel

我看过很多类似的问题,但解决方案并不适用。

我正在尝试制作一个Bootstrap轮播,以便在调整窗口大小时,图像保持居中,同时保持所需的图像高度和最小100%宽度属性。

这有点难以解释,但您可以在http://www.lyft.com

上看到类似的概念

我在codepen上复制了一个类似的问题,你可以看到,如果你将窗口缩小到移动设备的大小,图像仍然是固定的,最终你会看到左上角图像的一角(通常是云或一些无法识别的模糊)。

我试图让这个图像向左移动并保持居中,因为水平视图会缩小,以便图像保持相关。

我的其他旋转木马风格:

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

/* Carousel base class */
.carousel {
  height: 500px;
  margin-bottom: 60px;
  overflow: hidden;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
  z-index: 10;
}

/* Declare heights because of positioning of img element */
.carousel .item {
  height: 500px;
  background-color: #777;
}
.carousel-inner > .item > img {
  position: relative;
  top: 0;
  left: 0;
  min-width: 100%;  
  max-width: none;
  height: auto;  
}
.carousel-inner img{
    margin: auto;
}

#mycarousel{
    position: relative;
    top: 0;
}

简单的旋转木马:

<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>
    <li data-target="#mycarousel" data-slide-to="4"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="https://unsplash.it/2000/1250?image=397" data-color="lightblue" alt="First Image">
    </div>
    <div class="item">
      <img src="https://unsplash.it/2000/1250?image=689" data-color="firebrick" alt="Second Image">
    </div>
    <div class="item">
      <img src="https://unsplash.it/2000/1250?image=675" data-color="violet" alt="Third Image">
    </div>
    <div class="item">
      <img src="https://unsplash.it/2000/1250?image=658" data-color="lightgreen" alt="Fourth Image">
    </div>
    <div class="item">
      <img src="https://unsplash.it/2000/1250?image=638" data-color="tomato" alt="Fifth Image">
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

我根据您在帖子中链接的lyft网站提出了解决方案。我将您的img转换为div并使用了背景图片,然后进行了相应的样式设计。

 <div class="carousel-inner" role="listbox">
    <div class="item active">
      <div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=397');" data-color="lightblue" alt="First Image"></div>
    </div>
    <div class="item">
      <div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=689');" data-color="firebrick" alt="Second Image"></div>
    </div>
    <div class="item">
      <div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=675');" data-color="violet" alt="Third Image"></div>
    </div>
    <div class="item">
      <div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=658');" data-color="lightgreen" alt="Fourth Image"></div>
    </div>
    <div class="item">
      <div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=638');" data-color="tomato" alt="Fifth Image"></div>
    </div>

然后删除了这个.carousel-inner > .item > img并添加了这个CSS:

.carousel-inner .item .item-child {
  background-position: top center;
  background-size: cover;
  position: absolute;
  bottom:0;
  top: 0;
  left: 0;
  right:0;
  width: 100%;
  height: auto;  
}

can see the js.fiddle here如果你想玩它。希望有所帮助。

答案 1 :(得分:0)

你去,最容易修复

而不是图像上的最小宽度使用宽度

.carousel-inner > .item > img {
  position: relative;
  top: 0;
  left: 0;
  width: 100%;  
  max-width: none;
  height: auto;  
}

也像这样投入旋转木马的高度调整

.carousel .item {
  height: auto;
  background-color: #777;
}

http://codepen.io/Rohithzr/pen/WwEpgp?editors=1100