代码之家  ›  专栏  ›  技术社区  ›  marknt15

css:如何使左浮动div动态调整高度?[复制品]

  •  16
  • marknt15  · 技术社区  · 15 年前

    这个问题已经有了答案:

    如何使用静态内容使浮动左div的高度自动调整为与动态内容的右浮动div的高度相同?

    所以我要完成的是,左div和右div的高度是一样的(左div自动调整到右div的高度)

    下面是示例代码。

    提前谢谢:)

    干杯, 作记号

    <html>
    <head>
        <style type="text/css">
            body {
                font-family:verdana;
                font-size:12px;
            }
            .parent {
                border:1px solid red;
                width:530px;
                /*min-height:100%;*/
                padding:5px;
            }
            .left {
                border:1px solid blue;
                width:200px;
                float:left;
                position:relative;
                padding:3px;
            }
            .right {
                border:1px solid green;
                width:300px;
                float:right;
                position: relative;
                padding:3px;
            }
            .clr { clear:both; }
            .footer {
                border:1px solid orange;
                position: relative;
                padding:3px;
                margin-top:5px;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="left">float left div here only static content</div>
            <div class="right">
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
            </div>
            <div class="clr"></div>
            <div class="footer">Footer here</div>
        </div>
    </body>
    </html>
    
    5 回复  |  直到 13 年前
        1
  •  13
  •   marknt15    15 年前

    这里是工作的css解决方案(感谢pdr为 link ):

    <html>
    <head>
        <style type="text/css">
            html, body {
                font-family:verdana;
                font-size:12px;
            }
            .parent {
                border:1px solid red;
                width:530px;
                padding:5px;
                overflow:hidden;
            }
            .left {
                border:1px solid blue;
                width:200px;
                float:left;
                position:relative;
                padding:3px;
            }
            .right {
                border:1px solid green;
                width:300px;
                float:right;
                position: relative;
                padding:3px;
            }
    
            /* a solution but doesn't work in IE */
            /*
            .left, .right {
                min-height: 100px;
                height: auto;
            }
            */
    
            .left, .right {
                padding-bottom:8000px;
                margin-bottom:-8000px;
                background:none repeat scroll 0 0 lightblue;
            }
    
            .clr { clear:both; }
            .footer {
                border:1px solid orange;
                position: relative;
                padding:3px;
                margin-top:5px;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="left">float left div here only static content</div>
            <div class="right">
                float right div dynamic content here<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
                dynamic row<br />
            </div>
            <div class="clr"></div>
            <div class="footer">Footer here</div>
        </div>
    </body>
    </html>
    
        2
  •  5
  •   Nasser Hadjloo    15 年前

    如果我是你,我用一个简单的css技巧。我会在这样的css类中为.left和.right指定高度

    .Left, .Right
    {
        min-height: 200px; /*because of .Left*/
        height: auto;
    }
    

    请注意,上面的代码在高度超过200px时使用,然后它将覆盖200px值。

    希望能帮上忙


    用javascript完成答案

    <script>
    function increaseHeight()
    {
       Document.getElementById('LeftDivID').style.height = Document.getElementById('RightDivID').style.height;
    }
    </script>
    

    当你增加了右div的大小时就必须调用它

        3
  •  2
  •   Marius    15 年前

    A List Apart 有一些关于这个主题的优秀文章,例如 Faux Columns Multi-column layouts climb out of the box

        4
  •  1
  •   pdr    15 年前

    这里列出了许多选项

    http://www.ejeliot.com/blog/61

    一般来说,我想你应该问问自己是否真的想要两个专栏。可能您最好在父div中使用静态内容,在子div中使用动态内容([编辑]或反之亦然)。

        5
  •  1
  •   shahjapan    15 年前

    尝试下面的代码,我尝试了firefox,但希望它能在大多数浏览器上运行

        <html>
    <head>
        <style type="text/css">
            body {
                font-family:verdana;
                font-size:12px;
            }
            .parent {
                border:1px solid red;
                width:530px;
                /*min-height:100%;*/
                padding:5px;
            }
            .parent_new {
                border:1px solid red;
                width:530px;            
                padding:5px;
                display: table-cell;
            }
            .row_level
            {
                display:table-cell;
            }
            .cell-level {
                display:table-cell;
                border:1px solid red;
            }
            .left {
                border:1px solid blue;
                width:200px;
                float:left;
                position:relative;
                padding:3px;
            }
            .left {
                border:1px solid blue;
                width:200px;
                float:left;
                position:relative;
                padding:3px;
                display:table-row;
            }
            .right {
                border:1px solid green;
                width:300px;
                float:right;
                position: relative;
                padding:3px;
            }
            .clr { clear:both; }
            .footer {
                border:1px solid orange;
                position: relative;
                padding:3px;
                margin-top:5px;
            }
        </style>
    </head>
    <body>
        <div class="parent_new">
            <div class="row_level">
            <div class="cell-level">float left div here only static content
            </div>
            <div class="cell-level">
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
                float right div dynamic content here<br />
             </div>
            </div>
            <div class="clr"></div>
            <div class="footer">Footer here</div>
        </div>
    </body>
    </html>