How to have 2 floating divs have the same height

2020-07-23 04:20发布

问题:

I have a wrapper containing an inner wrapper, and that inner wrapper contains 2 floating divs. The left one contains more content than the right one, so it's height is greater than the one on the right. What I am looking for is that both of the containers would have the same height.

http://jsfiddle.net/Kh2Fh/

My html:

<div id="wrapper">
    <div id="sub-menu">
        <div id="left-column" class="column">
            Agenda</br>
            Here I put some texte                                    
        </div>
        <div id="right-column" class="column">
            sdfdsf                          
        </div>
    </div>
</div>​

My css:

body{
    background-color:#E5E5E5;}

#wrapper{
    background-color:#FFFFFF;
    width:800px;
    margin-left:auto;
    margin-right:auto;
    overflow:auto;}

#sub-menu{
    margin:10px;
    width:780px;
    position:relative;
    float:left;}

.column{      
    float:left;
height:100%;}

#left-column{
    width:500px;
    background-color:yellow;}

#right-column{
    width:280px;
    background-color:magenta;}

回答1:

You cannot do this via CSS alone using floated elements, unless you can guarantee the height of each column (which you generally can't, with such a fluid medium as the web). However, you do have options:

  1. Using display: table-cell: http://jsfiddle.net/8LdQk/3/. Unfortunately, this will not work in IE6 or 7. This blog post detailing its use might be helpful.
  2. Using JavaScript: http://jsfiddle.net/8LdQk/5/.
  3. Using Dan Cederholm's classic faux-columns trick.


回答2:

use a 1 pixel high repeating background image with 500 px wide yellow and 280 px wide magenta. When one column increases in size - you get the illusion of both columns being the same height.

<div class="backgroundColumn">
<div>
Left column
</div>
<div>
right column
</div>
<div class="clear">
</div>
</div>


回答3:

This seems to do the trick for me. Can it be that simple? :)

.column{float:left;height:100px;}


标签: css position