Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Bootstrap's Carousel both center AND responsive?

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 carousel is resized and smaller than the image, the image is cropped instead of scaling as default.

How can I both center my image, but keep it to stretch to the carousel item?

like image 386
Luke Vo Avatar asked Oct 25 '13 06:10

Luke Vo


People also ask

How do I center align a carousel image?

If you want the image to be centred vertically as well as horizontally (i.e. the height of the image is not the height of the carousel), the easiest way is to use flexbox alongside making sure the width and height match the parent.

How do you make a carousel item responsive?

1. Set some height and width as per your wish in your custom css file. 2. And set img-responsive in your code in the mail HTML Page for carousel items.

How can I use two Bootstrap carousel in one page?

To insert multiple jQuery carousels to the same webpage, you need to create each carousel with a unique ID. In Amazing Carousel, Publish dialog, select the option Publish to Folder, then click Browse to select a folder to save the generated files. You need to set up a unique Carousel ID for each carousel.


2 Answers

Now (on Boostrap 3+) it's simply:

.carousel-inner img {   margin: auto; } 
like image 69
OlivierLarue Avatar answered Sep 21 '22 04:09

OlivierLarue


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.

like image 41
Ryan Avatar answered Sep 20 '22 04:09

Ryan