3

I used to add <br clear=all> at the end of a div, without a height value in CSS, to set it's height.

The divs shrink and grow depending on the content.

Is there a more eloquent way of setting the div heights of all boxes to the height of the tallest box, without using <br clear=all>? Baring in mind the divs change height depending on what is inside them.

Here's an example of the code:

<div class="welcomeText">
  <div class="houseTiles2">
    <h1>TITLE<br><br></h1>
    <p><br>Body copy 1</p><br clear="all">
  </div>
  <div class="houseTiles">
    <h1 class="tileTitle">Title 2</h1><br>
    <p class="tileDesc">Text text text text text text text text</p>
 <br clear="all">
  </div>
  <div class="houseTiles">
    <h1 class="tileTitle">Title 3</h1><br>
    <p class="tileDesc">Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</p><br clear="all">
  </div><br clear="all">
</div>
GT22
  • 503
  • 1
  • 8
  • 25
  • Do you mean to have all the divs the same height or just use a clearfix for making the parent the height of the highest div? – Jayx Jan 19 '16 at 11:55
  • Do you mean clearfix ? – Sooraj Jan 19 '16 at 11:57
  • I've added code above. Thanks – GT22 Jan 19 '16 at 12:04
  • @Jayx I don't want to add content below the DIVs. There are three left floated DIVs with differing heights. I want to set them to all have the height of the tallest DIV. – GT22 Jan 19 '16 at 12:05
  • The best way is stop to use floats to layout. – Marcos Pérez Gude Jan 19 '16 at 12:11
  • it's like you got de-frozen straight from *2006*... **any** Google query will give you an answer to this question. – vsync Jan 19 '16 at 12:16
  • 1
    @GT22 Using `clear="all"` doesn't make them the same height. Why do you think it does? – Chuck Le Butt Jan 19 '16 at 12:19
  • 1
    @vsync Thanks for your useless comment. – GT22 Jan 19 '16 at 12:42
  • @MarcosPérezGude Thanks for your useless comment. – GT22 Jan 19 '16 at 12:43
  • @GT22 Anything is useless as long as you don't take it possitively and do something with it. :) – Daan Jan 19 '16 at 13:08
  • @DaanHeskes - yeah, like take 5 seconds to google it ;) – vsync Jan 19 '16 at 13:28
  • @vsync how am I supposed to google something I don't know? People like you are the exact problem with this website. I came asking for help, not to hear your childish comments. In future supply an answer and try to help the poster. It will make you a better person and a more productive member of this site. – GT22 Jan 19 '16 at 15:39
  • @GT22 - wow...maybe you should have asked "how to google questions?" ;) just google what you wrote in the title, something like `css br clear height` gives lots of good results – vsync Jan 19 '16 at 16:12

3 Answers3

4

You should be using

<br style="clear: both">

as the clear attribute is depreciated. It's not very eloquent, though.

A more eloquent of achieving the same thing is to use a class:

.clear {
  clear: both;
}

Another way is to put the following class on your containing element:

.container {
  overflow: hidden; /* can also be "auto" */
}

Then there's the "clearfix" method, which you place on the containing elements:

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

You can read more there: http://www.sitepoint.com/clearing-floats-overview-different-clearfix-methods/

EDIT: If you want the divs to be the same height, and you're targeting IE10+, you can use the flexbox. Very easy: http://learnlayout.com/flexbox.html

There's an interactive tutorial on flexbox here: http://cvan.io/flexboxin5/

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
1

The best possibility is to use clearfix method :

.welcomeText:before, .welcomeText:after {
   content:"";
   display:table;
}

.welcomeText:after {
  clear:both;
}

At the beginning of CSS we used a div with "clear:both" property

But now with the CSS we can use pseudo element that are like elements inside the div but created with CSS.

You can read any article about clearing float. for example : this one explains the problem with floats : http://diywpblog.com/when-and-how-to-use-a-clearfix-css-tutorial/

But this is not the answer you are attended, because you want that all child div take the same height as the parent div.

You can use display:table on the parent and display:table-cell on it's children, but remove float:left;

Arnaud Gueras
  • 2,014
  • 11
  • 14
-1

Making the div's have the same height automatically isn't possible with CSS (unless you give a fixed height).

However, it is with jQuery (to make the div's the same height):

var heights = [];

$(".houseTiles, .houseTiles2").each(function() {
    heights.push($(this).height());
});

var biggestheight = Math.max.apply(null, heights);

$(".houseTiles, .houseTiles2").each(function() {
    $(this).css("height",biggestheight);
});

As shown in this JSFiddle demo.

For a floated element to have a height you can give them the following CSS:

.houseTiles, .houseTiles2 {
    display:table;
    overflow:hidden;
}

That also clears the float, as you're doing with clear=all or clear:both.

Daan
  • 2,680
  • 20
  • 39
  • Seems like I misunderstood the question or got something wrong? Anyone to have improvements instead of just downvoting is more than welcome. – Daan Jan 19 '16 at 13:06
  • You understood the question, it's just us who doesn't understand because the question all talking about clearing... – Arnaud Gueras Jan 19 '16 at 13:34