38

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?

Community
  • 1
  • 1
Luke Vo
  • 17,859
  • 21
  • 105
  • 181

13 Answers13

75

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

.carousel-inner img {
  margin: auto;
}
OlivierLarue
  • 2,234
  • 27
  • 28
34

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.

Community
  • 1
  • 1
Ryan
  • 5,959
  • 2
  • 25
  • 24
19

On Boostrap 4 simply add mx-auto to your carousel image class.

<div class="carousel-item">
  <img class="d-block mx-auto" src="http://placehold.it/600x400" />
</div> 

Combine with the samples from the bootstrap carousel documentation as desired.

rox
  • 201
  • 2
  • 5
  • 3
    the most elegant solution without adding any more custom class..... @rox can you explain what the d-block does – Ananda Feb 24 '20 at 11:18
  • d-block sets the css property { display: block }. Basically, this elements stacks vertically. – julianm Jul 16 '21 at 00:15
3

add this

.carousel .item img {
   left: -9999px;  /*important */
   right: -9999px;  /*important */
   margin: 0 auto;  /*important */
   max-width: none;  /*important */
   min-width: 100%;
   position: absolute;
}

And of course one of the parent divs needs to have a position:relative, but I believe it does by default.

See it here: http://paginacrestinului.ro/

Adrian Cumpanasu
  • 1,028
  • 1
  • 10
  • 24
3

Add a class to each image in the carousel html code, then with css apply margin: 0 auto.

Mike
  • 1,158
  • 5
  • 22
  • 32
Escendrix
  • 33
  • 4
3
.carousel-inner > .item > img {
    margin: 0 auto;
}

This simple solution worked for me

FrickeFresh
  • 1,688
  • 1
  • 19
  • 31
2

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>

Edit:-

align attribute is deprecated, use CSS property.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/align

khadim husen
  • 136
  • 1
  • 10
1

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 */
Joe Connor
  • 11
  • 1
1

By using this on bootstrap 3:

#carousel-example-generic{
    margin:auto;
}
abpatil
  • 916
  • 16
  • 20
Vince2017
  • 11
  • 3
1

None of the above solutions worked for me. It's possible that there were some other styles conflicting.

For myself, the following worked, hopefully it may help someone else. I'm using bootstrap 4.

.carousel-inner img {       
   display:block;
   height: auto; 
   max-width: 100%;
}
Gabe Gates
  • 902
  • 1
  • 14
  • 19
0

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

.item img {
    width:100%;
}
Kevinvhengst
  • 1,672
  • 4
  • 27
  • 40
0

I've placed the <div class="carousel" id...> inside another div with the class container (<div class="container"><div class="carousel" id="my-carousel">...</div></div>). That solved it too.

ArtFranco
  • 300
  • 3
  • 10
0

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%;}
austin
  • 998
  • 2
  • 12
  • 22