CSS 3列浮动(2个固定,1个动态)

时间:2009-07-18 10:17:48

标签: html css layout

我正在设计一个由3个部分组成的标题。

页面必须是流畅的:min-width:940px; max-width:1200px;

标题的前两部分将是固定大小:

   left       middle        right
<---------><---------><----------------->
   134px      183px       (Fill the remaining space)

我希望根据页面的大小改变正确的部分,我会粘贴到目前为止的内容,但我的问题是让它完全填补空白。

HTML:

<div class="main">

<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>

CSS:

    .main{
        margin:auto;
        min-width:940px;
        max-width:1200px;
        background-color:#000;
    }

    .left{
    float: left;
    width: 134px;
    height: 191px;
    background-color:#0000ff;
    }
    .middle{
    float: left;
    width: 183px;
    height: 191px;
    background-color:#ffff00;
    }

    .right{
    float: left;
    width: 60%;
    height: 191px;
    background-color:#ff0000;
    }

2 个答案:

答案 0 :(得分:10)

试试这个:

<html>
<head>
<title>Three columns</title>
<style type="text/css">
div.main { background-color: #000; }
div.left { float: left; width: 134px; height: 191px; background-color:#0000ff; }
div.middle { float: left; width: 183px; height: 191px; background-color:#ffff00; }
div.right { height: 191px; background-color: #ff0000; margin-left: 317px; }
</style>
</head>
<body>
<div class="main">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
</body>
</html>

答案 1 :(得分:2)

我没有足够的声誉来评论,但我只想赞同前一个答案的正确性。我正在寻找一些略有不同的东西:固定液体固定,所以调整如下:

<html>
<head>
<title>Three columns</title>
<style type="text/css">
div.main { background-color: #000; }
div.left { float: left; width: 134px; height: 191px; background-color:#0000ff; }
div.middle { height: 191px; background-color: #ff0000; margin-left: 134px; margin-right: 183px;}
div.right { float: right; width: 183px; height: 191px; background-color:#ffff00; }
</style>
</head>
<body>
<div class="main">
<div class="left"></div>
<div class="right"></div>
<div class="middle"></div>
</div>
</body>
</html>

需要注意的要点: - 带浮动的边距用于防止身体与边缘重叠。 - 在我的情况下,你需要颠倒html中div的顺序 - 你实际上不需要“中间”的div。一个没有的网站是quirksmode博客(它只是直接在“body”元素上设置边距):http://www.quirksmode.org/blog/archives/browsers/index.html