Search code examples
cssbuttonheight

Css match height


I have an image slideshow with next/prev buttons. I'm trying to make the boxes behind the arrows' height match the main image. But right now they're stretching to the bottom of the thumbnails that are below. I tried both height: 100%, and position: absolute with top: 0 and bottom: 0, but neither of them are working. Display:flex and height: inherit didn't work either. Here's the codepen .

$(document).ready(function(){
    $('#imgDetail').animate({
    opacity: '1'
    },300);
});

$(document).ready(function(){
	// get all images loaded
  var images = $(".unidoor-class");
	// thumbnails containers
  var thumbnailContainer = $("#thumbnails");
    // generate thumbnail images
  generateThumbnails(images, thumbnailContainer);
  // listeners for controls arrows
	$(".prev-next-button").on("click touchstart", function() {
  	// get the images
    var currentImageIndex = $(".unidoor-class.active").index();
		var isPrevious = $(this).hasClass("previous");
    var nextIndex;
    if (isPrevious) {
    	if (currentImageIndex === 0) {
      	nextIndex = images.length - 1;
      }
      
      if (currentImageIndex > 0) {
      	nextIndex = currentImageIndex - 1;
      }
    } else {
    	if (currentImageIndex === images.length - 1) {
      	nextIndex = 0;
      }
      
      if (currentImageIndex < images.length - 1) {
      	nextIndex = currentImageIndex + 1;
      }
    }
		
    // remove any active class from images
		images.removeClass("active");
    // get the next active image and add active class to that next current image
    $(images[nextIndex]).addClass("active");
  });
  
  $(".thumb").on("click touchstart", function(event){
	  event.preventDefault();
    var indexSelected = $(this).data("img-index");
    var currentShown = $(".unidoor-class.active").index();
    if (currentShown === indexSelected) return false;
    images.removeClass("active");
    $(images[indexSelected]).addClass('active');
  });
  
  function generateThumbnails(images, container) {
  	var ul = $("<ul>");
  	images.each(function(index, element){
    	var currentThumb = $("<img>");
      var li = $("<li>");
      var src = $(this).attr("src");
      currentThumb.attr("src", src);
      currentThumb.attr("class", "thumb");
      currentThumb.data("img-index", index);
      li.append(currentThumb);
      ul.append(li);
    });
    container.append(ul);
  }
});

window.sr = ScrollReveal({reset: true});
sr.reveal('.active', {mobile:true});
* {
  margin: 0;
  padding: 0;
}

body{
  margin: 0;
  padding:0;
  font-size: 100%;
/*   line-height: 1.6; */
/*   font-family: Arial, Helvetica, sans-serif; */
  height: 100% !important;
}

.header{
  margin: 0 auto;
  width: 100%;
  background-color: #333;
  padding: 30px 0 0 0; 
}

.header h1{
  margin: 0;
  text-align: center;
  color: white;
  font-family: Arial, Helvetica, sans-serif;
}

.header ul {
  list-style-type: none;
  margin: 0;
/*   padding: 0; */
  overflow: hidden;
  padding: 20px 0px 30px 0;
  text-align: center;
}

.header li {
  display: block;
  display: inline-block;
/*   border-right: 1px solid #bbb; */
  border-right: 1px solid #bbb;
  height: 25px;
}

.header li:last-child{
  border-right: none;
}

.header li a {
  display: block;
  color: white;
  text-align: center;
  text-decoration: none;
  padding: 0px 40px;
  font-size: 1em;
}

.header li a:hover{
  color: #7bbe9a;
/*   color: #80b198; */
}

#green-room {
  background: #333 !important;
}

.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

#unidoor, .unidoor-class {
  position: absolute;
  width: 100%;
  margin: 0 auto;
  display: block;
  top: 0;
  left: 0;
  opacity: 0;
  transition: opacity .5s;
  height: auto;
}

.unidoor-class.active {
  position: relative;
  opacity: 1;
}

#prev {
  position: absolute;
  font-weight: bold;
  color: black;
  background-color: #fff;
  opacity: 0.5;
  left: 0;
  top: 0;
  bottom: 0;
}

#next {
  position: absolute;
  font-weight: bold;
  color: black;
  background-color: #fff;
  opacity: 0.5;
  right: 0;
  top: 0;
  bottom: 0;
}

#imgDetail {
  position: relative;
  width: 90%;
  margin: 0 auto;
  opacity: 0;
}

#imgDetail a {
    text-decoration: none;
    display: inline-block;
    padding: 8px 16px;
}

#imgDetail a:hover {
  background-color: #7bbe9a;
  color: white;
  opacity: 1;
}

#imgDetail ul {
  margin: 0 auto;
  display: block;
/*   width: 50%; */
}

.thumb { 
  width: 30%; 
  height: auto; 
  margin: 15px 1% 0px 2%;
  cursor: pointer;
}

#imgDetail li { 
  display: inline; 
/*   margin-right: 10px;  */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Images not Owned by Me -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Daniel Pollack</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css"/>
  </head>

  <body id="green-room">
  	<div class="header">
      <div id="title"><h1>Lorem Ipsum 3D Online Portfolio</h1></div>
      <nav id="menu">
        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="index.html#portfolio">Portfolio</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav>
    </div>
    
<div class="slideshow-container">
  <div id="imgDetail">
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" class="unidoor-class active" />
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_2.jpg" class="unidoor-class" />
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="unidoor-class" />
    <!--CONTROLS-->
    <a href="#" id="prev" class="prev-next-button previous">&#8249;</a>
    <a href="#" id="next" class="prev-next-button next">&#8250;</a>
    <!--Thumbnails-->
    <div id="thumbnails">
    </div>
  </div>
</div>
    
   <br><br/>
    
    <script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script>
    <script>
      window.sr = ScrollReveal({reset: true});
      sr.reveal('#unidoor');
    </script>
    
    </body>

</html>


Solution

  • What I did is that I remove the #thumbnails inside the container of .imgDetail so that the next and prev button will follow the height of the .imgDetail.

    To be able to center the next and prev button i use display:flex,

    #imgDetail a {
      text-decoration: none;
      display: flex;
      padding: 8px 16px;
      justify-content: center;
      align-items: center;
    }
    

    and then enclose the text of the next and prev <p></p> tag

    <a href="#" id="prev" class="prev-next-button previous">
      <p>&#8249;</p>
    </a>
    <a href="#" id="next" class="prev-next-button next">
      <p>&#8250;</p>
    </a>
    




    Sample code below

    $(document).ready(function(){
        $('#imgDetail').animate({
        opacity: '1'
        },300);
    });
    
    $(document).ready(function(){
    	// get all images loaded
      var images = $(".unidoor-class");
    	// thumbnails containers
      var thumbnailContainer = $("#thumbnails");
        // generate thumbnail images
      generateThumbnails(images, thumbnailContainer);
      // listeners for controls arrows
    	$(".prev-next-button").on("click touchstart", function() {
      	// get the images
        var currentImageIndex = $(".unidoor-class.active").index();
    		var isPrevious = $(this).hasClass("previous");
        var nextIndex;
        if (isPrevious) {
        	if (currentImageIndex === 0) {
          	nextIndex = images.length - 1;
          }
          
          if (currentImageIndex > 0) {
          	nextIndex = currentImageIndex - 1;
          }
        } else {
        	if (currentImageIndex === images.length - 1) {
          	nextIndex = 0;
          }
          
          if (currentImageIndex < images.length - 1) {
          	nextIndex = currentImageIndex + 1;
          }
        }
    		
        // remove any active class from images
    		images.removeClass("active");
        // get the next active image and add active class to that next current image
        $(images[nextIndex]).addClass("active");
      });
      
      $(".thumb").on("click touchstart", function(event){
    	  event.preventDefault();
        var indexSelected = $(this).data("img-index");
        var currentShown = $(".unidoor-class.active").index();
        if (currentShown === indexSelected) return false;
        images.removeClass("active");
        $(images[indexSelected]).addClass('active');
      });
      
      function generateThumbnails(images, container) {
      	var ul = $("<ul>");
      	images.each(function(index, element){
        	var currentThumb = $("<img>");
          var li = $("<li>");
          var src = $(this).attr("src");
          currentThumb.attr("src", src);
          currentThumb.attr("class", "thumb");
          currentThumb.data("img-index", index);
          li.append(currentThumb);
          ul.append(li);
        });
        container.append(ul);
      }
    });
    
    window.sr = ScrollReveal({reset: true});
    sr.reveal('.active', {mobile:true});
    * {
      margin: 0;
      padding: 0;
    }
    
    body{
      margin: 0;
      padding:0;
      font-size: 100%;
    /*   line-height: 1.6; */
    /*   font-family: Arial, Helvetica, sans-serif; */
      height: 100% !important;
    }
    
    .header{
      margin: 0 auto;
      width: 100%;
      background-color: #333;
      padding: 30px 0 0 0; 
    }
    
    .header h1{
      margin: 0;
      text-align: center;
      color: white;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    .header ul {
      list-style-type: none;
      margin: 0;
    /*   padding: 0; */
      overflow: hidden;
      padding: 20px 0px 30px 0;
      text-align: center;
    }
    
    .header li {
      display: block;
      display: inline-block;
    /*   border-right: 1px solid #bbb; */
      border-right: 1px solid #bbb;
      height: 25px;
    }
    
    .header li:last-child{
      border-right: none;
    }
    
    .header li a {
      display: block;
      color: white;
      text-align: center;
      text-decoration: none;
      padding: 0px 40px;
      font-size: 1em;
    }
    
    .header li a:hover{
      color: #7bbe9a;
    /*   color: #80b198; */
    }
    
    #green-room {
      background: #333 !important;
    }
    
    .slideshow-container {
      max-width: 1000px;
      position: relative;
      margin: auto;
    }
    
    #unidoor, .unidoor-class {
      position: absolute;
      width: 100%;
      margin: 0 auto;
      display: block;
      top: 0;
      left: 0;
      opacity: 0;
      transition: opacity .5s;
      height: auto;
    }
    
    .unidoor-class.active {
      position: relative;
      opacity: 1;
    }
    
    #prev {
      position: absolute;
      font-weight: bold;
      color: black;
      background-color: #fff;
      opacity: 0.5;
      left: 0;
      top: 0;
      bottom: 0;
    }
    
    #next {
      position: absolute;
      font-weight: bold;
      color: black;
      background-color: #fff;
      opacity: 0.5;
      right: 0;
      top: 0;
      bottom: 0;
    }
    
    #imgDetail {
      position: relative;
      width: 90%;
      margin: 0 auto;
      opacity: 0;
    }
    
    #imgDetail a {
        text-decoration: none;
        display: flex;
        padding: 8px 16px;
        justify-content: center;
        align-items: center;
    }
    
    #imgDetail a:hover {
      background-color: #7bbe9a;
      color: white;
      opacity: 1;
    }
    
    #imgDetail ul {
      margin: 0 auto;
      display: block;
    /*   width: 50%; */
    }
    
    .thumb { 
      width: 30%; 
      height: auto; 
      margin: 15px 1% 0px 2%;
      cursor: pointer;
    }
    
    #imgDetail li { 
      display: inline; 
    /*   margin-right: 10px;  */
    }
    
    #thumbnails ul{
      margin: 0 auto;
        display: block;
    }
    
    #thumbnails li{
      display: inline;
    }
    <script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Images not Owned by Me -->
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Daniel Pollack</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css"/>
      </head>
    
      <body id="green-room">
      	<div class="header">
          <div id="title"><h1>Lorem Ipsum 3D Online Portfolio</h1></div>
          <nav id="menu">
            <ul>
              <li><a href="index.html">Home</a></li>
              <li><a href="index.html#portfolio">Portfolio</a></li>
              <li><a href="#about">About</a></li>
              <li><a href="#contact">Contact</a></li>
            </ul>
          </nav>
        </div>
        
    <div class="slideshow-container">
      <div id="imgDetail">
        <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" class="unidoor-class active" />
        <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_2.jpg" class="unidoor-class" />
        <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="unidoor-class" />
        <!--CONTROLS-->
        <a href="#" id="prev" class="prev-next-button previous"><p>&#8249;</p></a>
        <a href="#" id="next" class="prev-next-button next"><p>&#8250;</p></a>
        <!--Thumbnails-->
        
      </div>
        <div id="thumbnails">
          
        </div>
    </div>
        
       <br><br/>
        
        <script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script>
        <script>
          window.sr = ScrollReveal({reset: true});
          sr.reveal('#unidoor');
        </script>
        
        </body>
    
    </html>

    Hope this helps