CSS 使 bootstrap carousel 响应高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20849539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:28:10  来源:igfitidea点击:

Make bootstrap carousel responsive on height

csstwitter-bootstrapcarousel

提问by Depete

I need to customize BS carousel, to make images size responsive corectly. I need the carousel to adapt to the height of the active slides images initiel height. When the viewport is wider than the image, the image/slide should scale up, but keep ratio. When viewport is smaller the image should scale down but keep ratio.

我需要自定义 BS 轮播,以使图像大小正确响应。我需要轮播来适应活动幻灯片图像初始高度的高度。当视口比图像宽时,图像/幻灯片应按比例放大,但保持比例。当视口较小时,图像应缩小但保持比例。

I have tried many variations but cannot find the right solution.

我尝试了许多变体,但找不到正确的解决方案。

I hope some css/js expert can help me out here.

我希望一些 css/js 专家可以帮助我。

Here's my current markup:

这是我目前的标记:

<!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-pause="true">
      <!-- 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>
      </ol>
      <div class="carousel-inner">
        <div class="item active"><!--data-src="holder.js/900x500/auto/#777:#7a7a7a/text:First slide"-->
          <img src="MediaContent/Images/Unika%20kobbertryk%201.jpg" alt="First slide">
          <div class="container">
            <div class="carousel-caption">
              <h1>Example headline.</h1>
              <p>Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
              <p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
            </div>
          </div>
        </div>
        <div class="item"> <!--data-src="holder.js/900x500/auto/#666:#6a6a6a/text:Second slide" -->
          <img src="MediaContent/Images/Unika%20kobbertryk%202.jpg" alt="Second slide">
          <div class="container">
            <div class="carousel-caption">
              <h1>Another example headline.</h1>
              <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
              <p><a class="btn btn-lg btn-primary" href="#" role="button">Learn more</a></p>
            </div>
          </div>
        </div>
        <div class="item"><!--data-src="holder.js/900x500/auto/#555:#5a5a5a/text:Third slide"-->
          <img src="MediaContent/Images/Unika%20kobbertryk%203.jpg"  alt="Third slide">
          <div class="container">
            <div class="carousel-caption">
              <h1>One more for good measure.</h1>
              <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
              <p><a class="btn btn-lg btn-primary" href="#" role="button">Browse gallery</a></p>
            </div>
          </div>
        </div>
      </div>
      <a class="left carousel-control" href="#myCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
      <a class="right carousel-control" href="#myCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
    </div><!-- /.carousel -->

And heres my currently css, that does not function like desciped. This version gives me a fixed 500px height, and an image that looses aspect ratio when viewport is smaller than image width.

这是我目前的 css,它的功能不像 desciped。这个版本给了我一个固定的 500px 高度,当视口小于图像宽度时,图像会失去纵横比。

/* GLOBAL STYLES

body {
  padding-bottom: 40px;
  color: #5a5a5a;
}


/* CUSTOMIZE THE NAVBAR
-------------------------------------------------- */

/* Special class on .container surrounding .navbar, used for positioning it into place. */
.navbar-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
}

/* Flip around the padding for proper display in narrow viewports */
.navbar-wrapper .container {
  padding-left: 0;
  padding-right: 0;
}
.navbar-wrapper .navbar {
  padding-left: 15px;
  padding-right: 15px;
}


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

/* Carousel base class */
.carousel {
  min-height: 500px;
  margin-bottom: 60px;
}
/* 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 {
  min-height: 500px;
  background-color: #777;
}
.carousel-inner > .item > img {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 500px;
}



/* MARKETING CONTENT
-------------------------------------------------- */

/* Pad the edges of the mobile views a bit */
.marketing {
  padding-left: 15px;
  padding-right: 15px;
}

/* Center align the text within the three columns below the carousel */
.marketing .col-lg-4 {
  text-align: center;
  margin-bottom: 20px;
}
.marketing h2 {
  font-weight: normal;
}
.marketing .col-lg-4 p {
  margin-left: 10px;
  margin-right: 10px;
}


/* Featurettes
------------------------- */

.featurette-divider {
  margin: 80px 0; /* Space out the Bootstrap <hr> more */
}

/* Thin out the marketing headings */
.featurette-heading {
  font-weight: 300;
  line-height: 1;
  letter-spacing: -1px;
}



/* RESPONSIVE CSS
-------------------------------------------------- */

@media (min-width: 768px) {

  /* Remove the edge padding needed for mobile */
  .marketing {
    padding-left: 0;
    padding-right: 0;
  }

  /* Navbar positioning foo */
  .navbar-wrapper {
    margin-top: 20px;
  }
  .navbar-wrapper .container {
    padding-left:  15px;
    padding-right: 15px;
  }
  .navbar-wrapper .navbar {
    padding-left:  0;
    padding-right: 0;
  }

  /* The navbar becomes detached from the top, so we round the corners */
  .navbar-wrapper .navbar {
    border-radius: 4px;
  }

  /* Bump up size of carousel content */
  .carousel-caption p {
    margin-bottom: 20px;
    font-size: 21px;
    line-height: 1.4;
  }

  .featurette-heading {
    font-size: 50px;
  }

}

@media (min-width: 992px) {
  .featurette-heading {
    margin-top: 120px;
  }
}

Any help appreciated.

任何帮助表示赞赏。

回答by negative0

In the CSS selector .carousel-inner > .item > img, replace min-widthwith width: 100%;

在 CSS 选择器中.carousel-inner > .item > img,替换min-widthwidth: 100%;

.carousel-inner > .item > img {
  min-width: 100%;
  width: 100%;
}

回答by Lmaquer

I fixed it adding a class to the images in the carousel, for example :

我修复了它为轮播中的图像添加一个类,例如:

<img class="fixed-height">

and in the css:

并在 css 中:

.fixed-height {
    max-height: 400px;
}

回答by user3363454

I was having the same problem as above. Here's my solution. I changed a few lines of CSS:

我遇到了与上述相同的问题。这是我的解决方案。我更改了几行 CSS:

.carousel {
  margin-bottom: 60px;
}

/* 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 {
  min-width: 100%;
  background-color: #fff;
}

.carousel-inner > .item > img {
  min-width: 100%;
  height: 100%;
}

I hope this helps.

我希望这有帮助。

回答by Tapan kumar

I will refer you just to add the class "img-responsive" in the image tags. And Bootstrap will do the rest for you.

我会建议您在图像标签中添加“img-responsive”类。Bootstrap 将为您完成剩下的工作。

This class tells the Bootstrap that you want to scale the images according to the screen size.

此类告诉 Bootstrap 您要根据屏幕尺寸缩放图像。

<div class="carousel slide" id="theCarousel" data-interval="3000">
        <ol class="carousel-indicators">
        <li data-target="#theCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#theCarousel" data-slide-to="1"></li>
        <li data-target="#theCarousel" data-slide-to="2"></li>
        </ol>

            <div class="carousel-inner">
                <div class="item active">
                    <img src="http://www.ansupalake.in/gallery/tapan%20kumar%201.JPG" alt="1" class="img-responsive" />
                    <div class="carousel-caption">
                         <h4 class="text-left">Resting In Style</h4>

                        <p class="text-left">Hi Everybody what's going on.....</p>
                    </div>
                </div>
                <div class="item ">
                    <img src="http://www.ansupalake.in/gallery/tapan%20kumar%202.JPG" alt="2" class="img-responsive" />
                    <div class="carousel-caption">
                         <h4 class="text-left">The Dude Look</h4>

                        <p class="text-left">Hi Everybody what's going on.....</p>
                    </div>
                </div>
                <div class="item ">
                    <img src="http://www.ansupalake.in/gallery/tapan%20kumar.JPG" alt="3" class="img-responsive" />
                    <div class="carousel-caption">
                         <h4 class="text-left">How z It??</h4>

                        <p class="text-left">Hi Everybody what's going on.....</p>
                    </div>
                </div>
            </div> 
        </div>          

For more details Here I have an article regrading this, You can take a look if you want

有关更多详细信息,我有一篇文章对此进行了重新分级,如果您愿意,可以查看

Carousel Image Slider In Bootstrap 3

Bootstrap 3 中的轮播图像滑块

回答by ikbal

define for every devices such as

为每个设备定义,例如

@media (min-width: 360px) {
  .carousel {
    min-height: 140px;
   margin-bottom: 60px;
  }
  /* 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 {
    min-height: 140px;
    background-color: #777;
    }
   .carousel-inner > .item > img {
    position: absolute;
   top: 0;
   left: 0;
   min-width: 100%;
   min-height: 140px;
  }


}

回答by Arif Rahman

To use images on Bootstrap carousel of different sizes and to do the followings, all at the same time,

要在不同大小的 Bootstrap carousel 上使用图像并同时执行以下操作,

  1. Image's width on carousael = 100%
  2. Carousel image slide height = fixed
  3. Maintain the image ratio
  1. carousael 上的图像宽度 = 100%
  2. 轮播图片幻灯片高度 = 固定
  3. 保持图像比例

Remove the imgtag & add a new class instead, say slide1

删除img标签并添加一个新类,比如slide1

<div class="item slide1">
  <div class="container">
    <div class="carousel-caption">
      <h1>Slide1 headline.</h1>
      <p>Slide content.</p>
    </div>
  </div>
</div> 

then add the following CSS code for slide1class,

然后为slide1类添加以下 CSS 代码,

.slide1{
    height: 500px;  /* your desired height for this slide */
    background: url(/img/your-img.jpg) no-repeat center center;
    background-size: cover;
}

You may also configure this heights for different window sizes.

您还可以为不同的窗口大小配置此高度。

回答by Majali

I had the same problem adding the class "img-responsive" in the image tags made images responsive

我在图像标签中添加类“img-responsive”时遇到了同样的问题,使图像具有响应性

            <div class="carousel-inner">
            <div class="item active">
                <img src="/imahes/my-image.jpg" alt="1" class="img-responsive" />
                <div class="carousel-caption">
                     <h4 class="text-left">My caption</h4>

                </div>
            </div>

but the problem was with the slider height so I need to add a media query to resize the control div

但问题出在滑块高度上,所以我需要添加一个媒体查询来调整控件 div 的大小

@media (max-width: 800px) {
.carousel-control{
  height:350px;
}
}

this media query is just an example, customize it to suit your needs, I hope that will help

这个媒体查询只是一个例子,自定义它以满足您的需求,我希望会有所帮助