Search code examples
htmlcsslistheight

How to make all divs same height, based on text content?


I'm still having trouble with a long-standing problem. To be concise, my site has 3 list items within an unordered list. In my demo the lists are the green boxes. Each list item contains an image and 3 divs (titlebox,locationbox,pricebox). I'm only concerned with titlebox here. My demo site is here:

You can see how each titlebox has different lengths of text, which pushes the location/price down. I've colored titlebox to be grey so you can see them. I want all titlebox heights to match the height of the biggest titlebox. I've attached a screenshot of what I'd like.

Here's a demo link in CodePen -- can you help tweak it? http://codepen.io/anon/pen/azJKYM

The main unordered list item (contains all 3 greenboxes) has CSS:

                .list
                    {
                        width: 100%;
                        overflow: hidden;
                        display: -webkit-flex;
                        display: -ms-flexbox;
                        display: flex;
                        -webkit-flex-wrap: wrap;
                        -ms-flex-wrap: wrap;
                        flex-wrap: wrap;
                    }

The individual list item (green box) has css:

                .list__item
                    {
                       width: 32%;
                       float: left; /* 10 */
                       display: -webkit-flex;
                       display: -ms-flexbox;
                       display: flex;
                       padding-top: 0.625em;
                       padding-bottom: 0.825em;
                       margin-left:1%;
                       margin-right:0%;
                       position:relative;
                       line-height:40px;
                    }

and the titlebox has CSS:

.titlebox{
    width: 80%;
    height: 10%;
    display: inline-block;
    font-size: 4.2vh;
    font-family: Garamond;
    color: #002000;
    text-align: center;
    line-height: 35px;
    font-weight:bold;
    margin-top: 5%;
    margin-right: 10%;
    margin-bottom: 5%;
    margin-left: 10%;
    background-color:grey;

}

Thanks for any help! The screenshot of desired result is below.

Desired Result


Solution

  • As far as I know this cant be done with CSS because these elements are not siblings. If the structure was different you could make set them to display: table-cell and they would adjust to each other. The only way I beleive this will work in its current markup is using a little javascript.

    You can simply loop through each of these on the page, see if that height is greater than a previous one and at the end set all of them to be the tallest one:

    var largest = 0; //start with 0
    
    $(".titlebox").each(function(){ //loop through each section
       var findHeight = $(this).height(); //find the height
       if(findHeight > largest){ //see if this height is greater than "largest" height
          largest = findHeight; //if it is greater, set largest height to this one 
       }  
    });
    
    $(".titlebox").css({"height":largest+"px"});
    

    CODEPEN

    UPDATE

    To find the tallest of a set of three you can first look through the .titlebox of each parent ul and then run the rest of the script:

    $("ul").each(function(){ //loop through each first
    
      var largest = 0;
    
      $(this).find(".titlebox").each(function(){ //find each .titlebox within its parent (rest is the same)
         var findHeight = $(this).height();
         if(findHeight > largest){
            largest = findHeight;
         }  
      });
    
      $(this).find(".titlebox").css({"height":largest+"px"}); //update all .titlebox inside of this ul
    
    });
    

    CODEPEN 2