Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put different sizes of images inside carousel in Bootstrap 4?

I'm having trouble putting different size of images inside carrousel in Bootstrap 4. How can I put different sizes of images inside this carousel.

For an example, I want to put different sizes of images as in the below code. In the current situation, when I do that it just takes that images size.

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">


<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous">
</script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous">
</script>




<div class="container">
  <div id="carouselwithIndicators" class="carousel slide w-50" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2s"></li>
    </ol>

    <div class=" carousel-inner">
      <div class="carousel-item active">
        <img class="d-block w-100" src="https://www.tutorialspoint.com/bootstrap/images/slide1.png" alt="First slide">
      </div>

      <div class="carousel-item">
        <img class="d-block w-100" src="https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt="Second slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" src="https://www.tutorialspoint.com/bootstrap/images/slide3.png" alt="Third slide">
      </div>
    </div>

    <a class="carousel-control-prev" href="#carouselwithIndicators" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>

    <a class="carousel-control-next" href="#carouselwithIndicators" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>
like image 714
Gihan Avatar asked May 01 '19 17:05

Gihan


People also ask

How do I fit an image into carousel?

1 method is to use a div with a specified size, use css to set the image as a background image for the div and set background-size to fit, contain or cover. 2 A better approach is to use an image manipulation program like photoshop, i personally use an online application called https://www.photopea.com.

How do I make bootstrap 4 carousel full width?

To make it full-width we don't use any Bootstrap “container” or “row”, just the carousel which I have placed inside a “div” tag (class “top-content”) with a 100% width. The images I've used are 1920 pixels wide so they should cover pretty much every type of device.


1 Answers

Reason behind this

Let's have a look at what you're doing here first:

<div class = "carousel-item">
    <img class = "d-block w-100" src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt = "Second slide">
</div>

Have a close look at the classes you added on the image element, d-block means the image display will be set to block and w-100 means the width will be 100%. Read more here.

Now that your second (dog) image is way too big in height compared to width, and there is no max-height set, it will expand the carousel.

Solution:

Firstly let's remove the w-100 class from the dog image item.

<div class = "carousel-item">
    <img class = "d-block" src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt = "Second slide">
</div>

Now let's add a little css:

.carousel-item img {
  margin: 0 auto; /* this will align the image to center. */
  width: auto; /* for those images which don't have a width specified, but has a max-height will be auto adjusted */
}

As commented above, you already understand which property is going to handle what values.

It's time to check the height of the carousel at the initial time of page load, So, whenever the document is ready, we check the height of the carousel with the #carouselwithIndicators selector. and then we apply this height value to all the images in the carousel.

Here is the JS:

$(document).ready(function(){
  console.log($('#carouselwithIndicators').css('height')); // check the initial height of the carousel;
      
  // now apply this height as a max-height on all the image items; css will handle the rest;
  $('#carouselwithIndicators').find('.carousel-item img').css('max-height', $('#carouselwithIndicators').css('height'))
});

Snippet

Have a look at the snippet now:

$(document).ready(function(){
  console.log($('#carouselwithIndicators').css('height')); // check the initial height of the carousel;
  
  // now apply this height as a max-height on all the image items; css will handle the rest;
  $('#carouselwithIndicators').find('.carousel-item img').css('max-height', $('#carouselwithIndicators').css('height'))
});
.carousel-item img {
  margin: 0 auto;
  width: auto;
}
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous">
</script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous">
</script>





<div class="container">
  <div id="carouselwithIndicators" class="carousel slide w-50" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2s"></li>
    </ol>

    <div class=" carousel-inner">
      <div class="carousel-item active">
        <img class="d-block w-100" src="https://www.tutorialspoint.com/bootstrap/images/slide1.png" alt="First slide">
      </div>

      <div class="carousel-item">
        <img class="d-block" src="https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt="Second slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" src="https://www.tutorialspoint.com/bootstrap/images/slide3.png" alt="Third slide">
      </div>
    </div>

    <a class="carousel-control-prev" href="#carouselwithIndicators" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>

    <a class="carousel-control-next" href="#carouselwithIndicators" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>
like image 84
Towkir Avatar answered Nov 13 '22 07:11

Towkir