Search code examples
cssimagetwitter-bootstrapimage-resizing

bootstrap image slider different image size


I have some experience with bootstrap and I want to create image slider by using:

http://www.w3schools.com/bootstrap/bootstrap_carousel.asp

https://startbootstrap.com/template-overviews/business-casual/

Everything is fine with the examples. The problem arise if I want to use different sized images:

  • width is OK
  • height is changing depending to the image.

I found many similar topics but none of them worked for me.

Is it possible to have fixed ratio - nevertheless of the photo size? Image streching or even "croping" the image only by using css?

Code

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .carousel-inner > .item > img,
  .carousel-inner > .item > a > img {
      width: 70%;
      margin: auto;
  }
  </style>
</head>
<body>

<div class="container">
  <br>
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- 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>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="img_chania.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_chania2.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_flower.jpg" alt="Flower" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_flower2.jpg" alt="Flower" width="460" height="345">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

Solution

  • For some websites I had to make a trick for this kind of issue.

    I just remove the img tags, and set it to the item background.

    Bootply : http://www.bootply.com/AVNXeL4Jtw

    HTML:

     <div class="carousel-inner" role="listbox">
          <div class="item active" style="background-image: url(//placehold.it/1024x700)  ">
          </div>
    
          <div class="item" style="background-image: url(//placehold.it/100x100)  ">
          </div>
    
          <div class="item" style="background-image: url(//placehold.it/1024x700)  ">
          </div>
    
          <div class="item" style="background-image: url(//placehold.it/200x200)  ">
          </div>
        </div>
    

    CSS:

    .carousel-inner .item{ 
       height:500px; 
       background-size:cover;
       background-position: center center;
    }