3

好的,我基本上是在构建一个流畅的布局。我的 HTML 是这样的:

<div id="container">
    <div class="box" id="left">Left</div>
    <div class="box" id="center">This text is long and can get longer</div>
    <div class="box" id="right">Right</div>
    <div class="clear"></div>
</div>

这是CSS:

#container{
    width: 100%;
}
.box{
    float: left;
}
#left, #right{
    width: 100px;
}
#center{
    width: auto; /* ? */
    overflow: hidden;
}
.clear{
    clear:both;
}

我需要知道的是如何在#center重新调整大小时#container重新调整大小而不使元素在彼此下方移动。

4

3 回答 3

2

尝试这些更正(只是简单的浮动元素,无需设置绝对元素或填充)

刚刚添加了一个新的小提琴

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>fluid layout</title>
<style>
    /*class to set the width of the columns */
    .floatbox{
        width:100px;
    }

    #container{
        width: 100%;
        float:left;
    }
    #left{
        float:left;
    }
    #right{
        float:right;
    }
    #center{
        overflow: hidden;
    }
    .clear{
        clear:both;
    }
</style>
</head>
<body>
    <div id="container">
        <!-- floating column to the right, it must be placed BEFORE the left one -->
        <div class="floatbox" id="right">Right</div>
        <div class="floatbox" id="left">Left</div>

        <!-- central column, it takes automatically the remaining width, no need to declare further css rules -->
        <div id="center">This text is long and can get longer</div>

        <!-- footer, beneath everything, css is ok -->
        <div class="clear"></div>
    </div>
</body>
</html>
于 2010-12-14T13:53:14.873 回答
1

#container也必须浮动(或溢出:自动/隐藏)来实现它。我强烈建议您使用一些更知名的流体解决方案:http ://www.noupe.com/css/9-timeless-3-column-layout-techniques.html

于 2010-12-14T13:41:38.583 回答
1

执行此操作并完全避免float出现问题的最简单方法是在容器上使用填充并将左/右元素绝对定位在填充区域中。(演示在 http://www.jsfiddle.net/gaby/8gKWq/1

html

<div id="container">
    <div class="box" id="left">Left</div>
    <div class="box" id="right">Right</div>
    <div class="box" id="centre">This text is long and can get longer</div>
</div>

div的顺序不再重要了..

CSS

#container{
    padding:0 100px;
    position:relative;
}
.box{
   /*style the boxes here*/
}
#left, #right{
    width: 100px;
    position:absolute;
}
#left{left:0;top:0;}
#right{right:0;top:0;}

#center{
   /*anything specific to the center box should go here.*/
}
于 2010-12-14T13:42:22.390 回答