Make Bootstrap's Carousel both center AND responsive?

后端 未结 13 2192
北海茫月
北海茫月 2020-12-05 02:45

I want my carousel images to be at the center (horizontally), which is not by default. I follow the solution at this topic.

However, using this solution, when the ca

相关标签:
13条回答
  • 2020-12-05 03:02

    Was having a very similar issue to this while using a CMS but it was not resolving with the above solution. What I did to solve it is put the following in the applicable css:

    background-size: cover
    

    and for placement purposes in case you are using bootstrap, I used:

    background-position: center center /* or whatever position you wanted */
    
    0 讨论(0)
  • 2020-12-05 03:04

    I assume you have different sized images. I tested this myself, and it works as you describe (always centered, images widths appropriately)

    /*CSS*/
    div.c-wrapper{
        width: 80%; /* for example */
        margin: auto;
    }
    
    .carousel-inner > .item > img, 
    .carousel-inner > .item > a > img{
    width: 100%; /* use this, or not */
    margin: auto;
    }
    
    <!--html-->
    <div class="c-wrapper">
    <div id="carousel-example-generic" class="carousel slide">
      <!-- Indicators -->
      <ol class="carousel-indicators">
        <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
        <li data-target="#carousel-example-generic" data-slide-to="1"></li>
        <li data-target="#carousel-example-generic" data-slide-to="2"></li>
      </ol>
    
      <!-- Wrapper for slides -->
      <div class="carousel-inner">
        <div class="item active">
          <img src="http://placehold.it/600x400">
          <div class="carousel-caption">
            hello
          </div>
        </div>
            <div class="item">
          <img src="http://placehold.it/500x400">
          <div class="carousel-caption">
            hello
          </div>
        </div>
        <div class="item">
          <img src="http://placehold.it/700x400">
          <div class="carousel-caption">
            hello
          </div>
        </div>
      </div>
    
      <!-- Controls -->
      <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
        <span class="icon-prev"></span>
      </a>
      <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
        <span class="icon-next"></span>
      </a>
    </div>
    </div>
    

    This creates a "jump" due to variable heights... to solve that, try something like this: Select the tallest image of a list

    Or use media-query to set your own fixed height.

    0 讨论(0)
  • 2020-12-05 03:04

    in bootstrap v4, i center and fill the carousel img to the screen using

    <img class="d-block mx-auto" max-width="100%" max-height="100%">
    

    pretty sure this requires parent elements' height or width to be set

    html,body{height:100%;}
    .carousel,.carousel-item,.active{height:100%;}
    .carousel-inner{height:100%;}
    
    0 讨论(0)
  • 2020-12-05 03:08
    .carousel-inner > .item > img {
        margin: 0 auto;
    }
    

    This simple solution worked for me

    0 讨论(0)
  • 2020-12-05 03:11

    Try giving a Width in % to the image in the carousel?

    .item img {
        width:100%;
    }
    
    0 讨论(0)
  • 2020-12-05 03:14

    This work for me very simple just add attribute align="center". Tested on Bootstrap 4.

    <!-- The slideshow -->
                <div class="carousel-inner" align="center">             
                    <div class="carousel-item active">
                        <img src="image1.jpg" alt="no image" height="550">
                    </div>
                    <div class="carousel-item">
                        <img src="image2.jpg" alt="no image" height="550">
                    </div>                
                    <div class="carousel-item">
                        <img src="image3.png" alt="no image" height="550">
                    </div>
                 </div>
    
    0 讨论(0)
提交回复
热议问题