Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make one div's height depended of another div's height?

Tags:

html

css

height

Well I have such simple divs structure:

<div id="wrapper">
    <div id="1" class="block">
    1            
    </div>
    <div id="2" class="block">
    2
    </div>
</div>

JS Fiddle

Content of #1 can dynamically changed by javascript and its height can be changed depended of content. What I want is to make #2 the same height as #1. I understand that I can use javascript for this purpose but I suggest there must be some not such tricky way to make those divs' heights equal using only CSS and/or changing divs' positioning some how.

like image 580
Dmytro Avatar asked Dec 15 '12 19:12

Dmytro


1 Answers

To expand on my comment, you can't do it semantically. You have to use a little trick to fake the 100% height. It's called faux columns, and you can read more here.

In your case, we can fake it by adding a few background divs:

<div class="background bg1"></div>
<div class="background bg2"></div>

Then changing your CSS like so:

#wrapper {
    border: 1px solid black;
    position: relative;
}

#wrapper:after {
    display: block;
    content: " ";
    height: 0;
    clear: both;
}

.block {
    position: relative;
    float: left;
    vertical-align: top;
    width: 200px;
    text-align: left;
    min-height: 200px;
    padding-left: 20px;
    z-index: 2;
}

.background {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 200px;
    z-index: 1;
}

.bg1 {
    background-color: #eee;
}

.bg2 {
    left: 200px;
    background-color: #aaa;
}​

Here's a working jsFiddle.

like image 145
Christian Avatar answered Oct 12 '22 11:10

Christian