20

I have 2 divs side by side. I don't know the height of them upfront, it changed according to the content. Is there a way to make sure they will always be the same height, even when one of them stretches, only with CSS?

I made a fiddle to show. I want the red and blue divs to be the same height...

http://jsfiddle.net/7RVh4/

this is the css:

#wrapper {
width: 300px;
}
#left {
    width:50px;
    background: blue;
    float:left;
    height: 100%;  /* sadly, this doesn't work... */
}
#right {
    width:250px;
    background: red;
    float:left;
}
Moshe Shaham
  • 15,448
  • 22
  • 74
  • 114
  • wouldn't it be easyer to use tables for this ? – Yordi May 01 '13 at 11:46
  • 5
    This question has been asked multiple times: [**1**](http://stackoverflow.com/q/1205159/983992), [**2**](http://stackoverflow.com/q/526294/983992), [**a lot more...**](http://stackoverflow.com/search?q=float+same+height) – Marcel Gwerder May 01 '13 at 11:48
  • 3
    @Yordi Use tables for tabular data and do not use tables for layout purposes, even if it's *easier* (won't be so easy when you've to change something in your project at some point in the future). In 2013 you shouldn't have to rely on layout tables except in very rare cases where your client has customers in China with IE6/IE7 (argh) and still want somewhat complicated designs that are meant for modern browsers... – FelipeAls May 01 '13 at 11:53
  • @FelipeAls so if not table cells, than what? – Moshe Shaham May 01 '13 at 12:02
  • Since the columns have fixed widths, you can easily fake equal-height columns by giving the parent element a blue and red bg image. – Matt Coughlin May 01 '13 at 12:46

6 Answers6

27

You could try instead of using float, use display: table-cell. You might find some older browsers don't understand this rule however. See below:

#wrapper {
    display: table; // See FelipeAls comment below
    width: 300px;
}

#left {
    display: table-cell;
    width: 50px;
    background: blue;
}

#right {
    display: table-cell;
    width: 250px;
    background: red;
}
antony
  • 2,763
  • 19
  • 23
  • 5
    It's better to also set parent `#wrapper { display: table }` in case some browsers would've problems with implicit/shadow DOM parent things being table-row and table but +1 nonetheless. Compatibility IE8+ – FelipeAls May 01 '13 at 11:48
  • 1
    Thanks FelipeAls. Good to know. – antony May 01 '13 at 11:49
  • 1
    @FelipeAls Could you be a little more specific as to *which* browsers have "problems"? I've seen lots of people claim this, but its always a vague statement. Opera/Firefox had some difficulty back when IE7 was still new, but only with really long pages. Since those versions are long dead, the only real concern in this day and age is IE8 and I've never seen IE8 have any trouble with anonymous table/table-cell elements. – cimmanon May 01 '13 at 12:26
  • 2
    Can you put it in a fiddle? – antony Aug 27 '14 at 11:06
5

Antony answer works ok, but you need all the divs to have the same parent and to have a wrapper, I have a solution that use javascript but works with any kind of element, they just need to have the same selector.

  function setEqualHeight(selector, triggerContinusly) {

    var elements = $(selector)
    elements.css("height", "auto")
    var max = Number.NEGATIVE_INFINITY;

    $.each(elements, function(index, item) {
        if ($(item).height() > max) {
            max = $(item).height()
        }
    })

    $(selector).css("height", max + "px")

    if (!!triggerContinusly) {
        $(document).on("input", selector, function() {
            setEqualHeight(selector, false)
        })

       $(window).resize(function() {
            setEqualHeight(selector, false)
       })
    }


}

    setEqualHeight(".sameh", true) 

http://jsfiddle.net/83WbS/2/

luisZavaleta
  • 1,160
  • 11
  • 21
  • tables are for making TABLES, you lose a lot of flexibility, for example I use my approach with a twitter bootstrap based website, it's impossible for me to modify the twitter bootstrap layout to add tables, my solution is very flexible, you don't need to modify your html. – luisZavaleta May 27 '14 at 06:17
  • 1
    No, look at the top voted response. HTML ``s are for data tables, CSS `display:table`s are for making grid layouts.
    –  May 27 '14 at 06:18
  • Sometimes is not that easy, check this for example: http://goo.gl/X2GAER it seems a perfect table, but check the html and you'll realize you can't convert it to a table-like layout. Buy I get your point, I'll use your solution when possible. – luisZavaleta May 27 '14 at 06:45
  • 1
    If you are using a framework and/or have a flexible responsive layout with other effects, js might be the best way to go. It may seem much more complicated but in general it's a good idea to acknowledge the possibility. Thank you luisZavaleta for taking the time to offer a different solution from others..! – Fei Lauren Nov 21 '14 at 22:38
  • display: table with bootstrap is kinda hard to configure, this is seems to be best solution for me at-least. Just set a global class ".sameHeight" and apply it to the each child of a single wrapper and it works like a charm. Thanks @luisZavaleta – TechYogi Sep 22 '16 at 08:10
2

I would recommend reading this article that explains how to do what you are trying to do. I would put a fiddle up that shows, but its pretty extensive and pure css. http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks

Cam
  • 1,884
  • 11
  • 24
0

There is a much simpler solution I want to point to. Using large padding-bottom: 500em and negative margin-bottom:-500em of the same amount on columns while the wrapper has simply overflow:hidden to cut the columns to the right size.

Found here: HTML/CSS: Making two floating divs the same height

Community
  • 1
  • 1
Hexodus
  • 12,361
  • 6
  • 53
  • 72
0

As indicated by Hexodus you can padding-bottom and margin-bottom, but a better solution would be to use flexbox or grid.

You can check this codepen if you want. I included a footer area because that is something I needed and it required a little bit more of hack.

enter image description here

.section {
  width: 500px;
  margin: auto;
  overflow: hidden;
  padding: 0;
}

div {
  padding: 1rem;
}

.header {
  background: lightblue;
}

.sidebar {
  background: lightgreen;
  width: calc(25% - 1rem);
}

.sidebar-left {
  float: left;
  padding-bottom: 500rem;
  margin-bottom: -500rem;
}

.main {
  background: pink;
  width: calc(50% - 4rem);
  float: left;
  padding-bottom: 500rem;
  margin-bottom: -500rem;
}

.sidebar-right {
  float: right;
  padding-bottom: 500rem;
  margin-bottom: -500rem;
}

.footer {
  background: black;
  color: white;
  float: left;
  clear: both;
  margin-top: 1rem;
  width: calc(100% - 2rem);
}
<div class="section">
<div class="header">
  This is the header
</div>
<div class="sidebar sidebar-left">
  This sidebar could have a menu or something like that. It may not have the same length as the other
</div>
<div class="main">
  This is the main area. It should have the same length as the sidebars
</div>
<div class="sidebar sidebar-right">
  This is the other sidebar, it could have some ads
</div>
<div class="footer">
  Footer area
</div>
</div>
Adriano_Pinaffo
  • 1,429
  • 4
  • 23
  • 46
-3

You can do this without using tables, by using this CSS trick.

Example - http://jsfiddle.net/LMGsv/

HTML

   <div id="wrapper">
            <div id="columns">
                <div id="left">text</div>
                <div id="right">text<br/>another line<br /></div>
            </div>        
    </div>

CSS

#wrapper {
    float:left;
    width: 300px;
}
#columns {
    float:left;
    width:300px;
    background:blue;
}
#left {
    float:left;
    width:50px;
    background: blue;
}
#right {
    width:250px;
    background: red;
    float:left
}
user2313440
  • 371
  • 2
  • 4