user7311741
user7311741

Reputation:

Slideshow Container Same Height

I have an image slideshow with four images of varying heights. I'm trying to keep them all the same height of 700px, but I can't figure out how. I set the img at max-height: 700px, but that didn't seem to work.

Here's the codepen.

   

 var slideIndex = 1;

  showSlide(slideIndex);


  function plusSlides(n) {
      showSlide(slideIndex += n);
  }

  function currentSlide(n) {
      showSlide(slideIndex = n);
  }


  function showSlide(n) {

      var i;

      var slides = document.getElementsByClassName("myslides");

      var dots = document.getElementsByClassName("dots");

      if (n > slides.length) {
          slideIndex = 1
      };

      if (n < 1) {
          slideIndex = slides.length
      };

      for (i = 0; i < slides.length; i++) {

          slides[i].style.display = "none";

      };

      for (i = 0; i < dots.length; i++) {

          dots[i].className = dots[i].className.replace(" active", "");

      };

      slides[slideIndex - 1].style.display = "block";

      dots[slideIndex - 1].className += " active";

  }
body{
  font-family: verdana,sans-serif;
  margin: 0;
  font-size: 100%;
}

#slideshow-container{
  position: relative;
  margin: auto;
  background-color: black;
  overflow: hidden;
}

#slideshow-container img{
  display: block;
  margin: 0 auto;
  max-width: 100%;
  position: relative;
  max-height: 700px !important;
}

.myslides{
  display: none;
}

.prev , .next{
  position: absolute;
  top: 50%;
  font-size: 30px;
  font-weight: bold;
  padding: 16px;
  margin-top: -22px;
  border-radius: 0 3px 3px 0;
  color: #fff;
  cursor: pointer;
  z-index: 1;
}

.next{
  right: 0;
  border-radius: 3px 0 0 3px;
}

.prev:hover,.next:hover{
  background-color: rgba(0,0,0,0.8);
}

.caption{
  text-align: center;
  position: absolute;
  bottom: 5px;
  width: 100%;
  color: #f2f2f2;
  font-size: 1em;
/*   font-size: 15px; */
/*   padding: 8px 22px; */
}

.fade{
  animation-name:fade;
  animation-duration:1.5s;
}

@keyframes fade{
  from {opacity: 0.4;}
  to {opacity: 1;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---Images not owned by me --->

<div id="slideshow-container">
 <div id="mainImg">

  <div class="myslides fade">
    <div><img src="https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg"></div>
      <div class="caption">Text 1</div>
  </div>

  <div class="myslides fade">
    <div><img src="https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png"></div>
      <div class="caption">Text 2</div>
  </div>

  <div class="myslides fade">
    <div><img src="https://www.shutterbug.com/images/17/promosa111217.png"></div>
      <div class="caption">Text 3</div>
  </div>

  <div class="myslides fade">
    <div><img src="http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg"></div>
      <div class="caption">Text 4</div>
  </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>

    <a class="next" onclick="plusSlides(1)">&#10095;</a>

   </div>
</div>

Upvotes: 0

Views: 1346

Answers (3)

John Baker
John Baker

Reputation: 2398

I'd suggest another approach, add the images as background images since it would make easier to set specific height and keep the width relative to it.

var slideIndex = 1;

showSlide(slideIndex);


function plusSlides(n) {

  showSlide(slideIndex += n);

}


function currentSlide(n) {

  showSlide(slideIndex = n);

}


function showSlide(n) {

  var i;

  var slides = document.getElementsByClassName("myslides");

  var dots = document.getElementsByClassName("dots");

  if (n > slides.length) {
    slideIndex = 1
  };

  if (n < 1) {
    slideIndex = slides.length
  };

  for (i = 0; i < slides.length; i++) {

    slides[i].style.display = "none";

  };

  for (i = 0; i < dots.length; i++) {

    dots[i].className = dots[i].className.replace(" active", "");

  };

  slides[slideIndex - 1].style.display = "block";

  dots[slideIndex - 1].className += " active";

}
body {
  font-family: verdana, sans-serif;
  margin: 0;
  font-size: 100%;
}

#slideshow-container {
  position: relative;
  margin: auto;
  background-color: black;
  overflow: hidden;
}

#slideshow-container>div {
  position: relative;
  height: 700px;
  width: 100%;
}

#slideshow-container .bg-img {
  background-repeat: no-repeat;
  background-size: auto 100%;
  background-position: center;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

#slideshow-container img {
  display: block;
  margin: 0 auto;
  max-width: 100%;
  position: relative;
  max-height: 700px !important;
}

.myslides {
  display: none;
}

.prev,
.next {
  position: absolute;
  top: 50%;
  font-size: 30px;
  font-weight: bold;
  padding: 16px;
  margin-top: -22px;
  border-radius: 0 3px 3px 0;
  color: #fff;
  cursor: pointer;
  z-index: 1;
}

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

.caption {
  text-align: center;
  position: absolute;
  bottom: 5px;
  width: 100%;
  color: #f2f2f2;
  font-size: 1em;
  /*   font-size: 15px; */
  /*   padding: 8px 22px; */
}

.fade {
  animation-name: fade;
  animation-duration: 1.5s;
}

@keyframes fade {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 1;
  }
}
<!---Images not owned by me --->

<div id="slideshow-container">
  <div id="mainImg">

    <div class="myslides fade">
      <div>
        <span class="bg-img" style="background-image: url('https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg')"></span>
      </div>
      <div class="caption">Text 1</div>
    </div>

    <div class="myslides fade">
      <div>
        <span class="bg-img" style="background-image: url('https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png')"></span>
      </div>
      <div class="caption">Text 2</div>
    </div>

    <div class="myslides fade">
      <div>
        <span class="bg-img" style="background-image: url('https://www.shutterbug.com/images/17/promosa111217.png')"></span>
      </div>
      <div class="caption">Text 3</div>
    </div>

    <div class="myslides fade">
      <div>
        <span class="bg-img" style="background-image: url('http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg')"></span>
      </div>
      <div class="caption">Text 4</div>
    </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>

    <a class="next" onclick="plusSlides(1)">&#10095;</a>

  </div>
</div>

<br>

 <div style="text-align:center">

<span class="dots" onclick="currentSlide(1)"></span> 

<span class="dots" onclick="currentSlide(2)"></span> 

<span class="dots" onclick="currentSlide(3)"></span> 

<span class="dots" onclick="currentSlide(4)"></span> 

</div>

Upvotes: 0

Alan Omar
Alan Omar

Reputation: 4217

Add the property height="700px" to the outer container and the same property height="700px" to each img element that is:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---Images not owned by me --->

<div id="slideshow-container" height="700px">
 <div id="mainImg">

  <div class="myslides fade">
    <div><img height="700px" src="https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg"></div>
      <div class="caption">Text 1</div>
  </div>

  <div class="myslides fade">
    <div><img height="700px" src="https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png"></div>
      <div  class="caption">Text 2</div>
  </div>

  <div class="myslides fade">
    <div><img height="700px" src="https://www.shutterbug.com/images/17/promosa111217.png"></div>
      <div class="caption">Text 3</div>
  </div>

  <div class="myslides fade">
    <div><img height="700px" src="http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg"></div>
      <div class="caption">Text 4</div>
  </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>

    <a class="next" onclick="plusSlides(1)">&#10095;</a>

   </div>
</div>

Upvotes: 0

Nandita Sharma
Nandita Sharma

Reputation: 13407

Change max-height: 700px style to height: 700px and width: 100%; to width: auto;

var slideIndex = 1;

showSlide(slideIndex);


function plusSlides(n){

showSlide(slideIndex += n);

}


function currentSlide(n) {

showSlide(slideIndex = n);

}


function showSlide(n){

var i;

var slides = document.getElementsByClassName("myslides");

var dots = document.getElementsByClassName("dots");

if (n > slides.length) { slideIndex = 1};

if (n < 1) { slideIndex = slides.length};

for (i=0;i<slides.length;i++) {

slides[i].style.display = "none";

};

for (i=0;i<dots.length;i++) {

dots[i].className = dots[i].className.replace(" active","");

};

slides[slideIndex-1].style.display = "block";

dots[slideIndex-1].className += " active";

}
body{
  font-family: verdana,sans-serif;
  margin: 0;
  font-size: 100%;
}

#slideshow-container{
  position: relative;
  margin: auto;
  background-color: black;
  overflow: hidden;
}

#slideshow-container img{
  display: block;
  margin: 0 auto;
  width: auto;
  position: relative;
  height: 700px !important;
}

.myslides{
  display: none;
}

.prev , .next{
  position: absolute;
  top: 50%;
  font-size: 30px;
  font-weight: bold;
  padding: 16px;
  margin-top: -22px;
  border-radius: 0 3px 3px 0;
  color: #fff;
  cursor: pointer;
  z-index: 1;
}

.next{
  right: 0;
  border-radius: 3px 0 0 3px;
}

.prev:hover,.next:hover{
  background-color: rgba(0,0,0,0.8);
}

.caption{
  text-align: center;
  position: absolute;
  bottom: 5px;
  width: 100%;
  color: #f2f2f2;
  font-size: 1em;
/*   font-size: 15px; */
/*   padding: 8px 22px; */
}

.fade{
  animation-name:fade;
  animation-duration:1.5s;
}

@keyframes fade{
  from {opacity: 0.4;}
  to {opacity: 1;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---Images not owned by me --->

<div id="slideshow-container">
 <div id="mainImg">

  <div class="myslides fade">
    <div><img src="https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg"></div>
      <div class="caption">Text 1</div>
  </div>

  <div class="myslides fade">
    <div><img src="https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png"></div>
      <div class="caption">Text 2</div>
  </div>

  <div class="myslides fade">
    <div><img src="https://www.shutterbug.com/images/17/promosa111217.png"></div>
      <div class="caption">Text 3</div>
  </div>

  <div class="myslides fade">
    <div><img src="http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg"></div>
      <div class="caption">Text 4</div>
  </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>

    <a class="next" onclick="plusSlides(1)">&#10095;</a>

   </div>
</div>

Upvotes: 1

Related Questions