Nova
Nova

Reputation: 423

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

Upvotes: 4

Views: 10250

Answers (3)

Al.s
Al.s

Reputation: 310

In case you're looking for a pure HTML/CSS solution

If I understood correctly what you're trying to accomplish, so the way you allocate the div's causing you problems. I would try to place the headline and the location in the same div, and keep the price out-side of it like this:

div {
  position: relative;
  width: 200px;
  height: 200px;
  background: yellow;
  text-align: center;
}

p {
  position: absolute;
  left: 75px;
  top: 135px;
}

h3 {
  text-align center;
}
<section>
  <div>
    <h2>this title won't affect the location position</h2>
    <p>location</p>
  </div>
  <h3>outside the div</h3>
</section>

This way no matter what you will place inside the header, the div side will stay the same and so is the location position.

Upvotes: 2

Prabu Srinivasan
Prabu Srinivasan

Reputation: 3

You can just add this script to

$(document).ready(function()                
  {
      var maxHeight = Math.max.apply(null, $("div.titlebox").map(function ()
      {
          return $(this).height();
      }).get());
     
      $("div.titlebox").css('height',maxHeight );
  }
)

Updated Code : http://codepen.io/anon/pen/OPpEKj

Upvotes: 0

jmore009
jmore009

Reputation: 12923

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

Upvotes: 6

Related Questions