2列布局(左列固定宽度,右列流体+清除:两者)
只是需要帮助,因为我多年来一直在尝试解决这个问题。我需要什么:
我有一个 2 列布局,其中左列具有固定宽度 220px,右列具有流动宽度。
代码是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Fluid</title>
<style type="text/css" media="screen">
html, body { background: #ccc; }
.wrap { margin: 20px; padding: 20px; background: #fff; }
.main { margin-left: 220px; width: auto }
.sidebar { width: 200px; float: left; }
.main,
.sidebar { background: #eee; min-height: 100px; }
</style>
</head>
<body>
<div class="wrap">
<div class="sidebar">This is the static sidebar</div>
<div class="main">This is the main, and fluid div</div>
</div>
</body>
</html>
完全没有问题。当我在右列中使用 css 语法 clear: Both 时,之后的所有内容都会移到左列下。这是正确的行为,没有什么可反对的。
但我真的需要使用clear:无论是哪种方式,它都停留在右列的上下文中(根本不受左列的影响,并且不会在下面移动)
是否有任何简单的解决方法保留页面设计的基本浮动概念?
更新:请参阅此链接以了解我的内容,因为我的描述可能有点令人困惑。 链接:http://jsfiddle.net/k4L5K/1/
Just need help as I have been trying sort this out for ages now. What I need:
I've got a 2 column layout, where the left column has a fixed width 220px and the right column has a fluid width.
Code is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Fluid</title>
<style type="text/css" media="screen">
html, body { background: #ccc; }
.wrap { margin: 20px; padding: 20px; background: #fff; }
.main { margin-left: 220px; width: auto }
.sidebar { width: 200px; float: left; }
.main,
.sidebar { background: #eee; min-height: 100px; }
</style>
</head>
<body>
<div class="wrap">
<div class="sidebar">This is the static sidebar</div>
<div class="main">This is the main, and fluid div</div>
</div>
</body>
</html>
There's no problem at all. When I use a css syntax clear: both in the right column, all content after gets moved under the left column. This is a right behaviour and nothing against it.
But I relly need to use clear: both in the way, that it stays just in context of the right column (doesn't get affected by the left column at all, and doesn't move underneath)
Is there any simple get around with retaining a basic float concept of page design?
UPDATE: Please see this link to know what I'm on about as it may be a bit confusing from my description.
Link: http://jsfiddle.net/k4L5K/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是更改后的 CSS:
基本上,我所做的就是更改布局的完成方式,以便
.main
div 浮动在右侧。为此,我们必须添加两件事:.wrap
div 上添加 240px 的内边距,.main
div 上添加 -220px 的右边距以正确对齐页面的流动部分。因为我们已将
.main
div 浮动到右侧,所以clear: Both;
现在仅影响.main
div 内的内容,如下所示你想要的。您可以在此处查看演示:http://jsfiddle.net/6d2qF/1/
Here's your altered CSS:
Basically what I've done is change the way your layout is done, so that
.main
div is floated on the right. To do this, we had to add 2 things:.wrap
div, and.main
div of -220px to properly align the fluid part of the page.Because we've floated the
.main
div on the right, theclear: both;
now only affects content inside the.main
div, as you want.You can see a demonstration here: http://jsfiddle.net/6d2qF/1/
这个问题很老了,但这是我最近发现的另一个解决方案。
我们只需要做两件事:
overflow: auto;
添加到.main
divoverflow: hide;
保留文档流> 到.wrap
div,或添加.clear
div 作为.wrap
元素的最后一个子元素这是更新的小提琴: http://jsfiddle.net/k4L5K/89/
The question is quite old but here is the another solution which I've found recently.
We just need to do 2 things:
overflow: auto;
to the.main
divoverflow: hidden;
to the.wrap
div, or adding.clear
div as the last child of.wrap
elementHere is the updated fiddle: http://jsfiddle.net/k4L5K/89/
试试这个:
Try this: