好的,我基本上建立了流畅的布局。 我的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
重新调整大小。
答案 0 :(得分: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>
答案 1 :(得分:1)
#container
也必须浮动(或使用溢出:自动/隐藏)来实现它。
我强烈建议您使用一些众所周知的流体解决方案:http://www.noupe.com/css/9-timeless-3-column-layout-techniques.html
答案 2 :(得分: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.*/
}