具有固定和液体列的CSS布局

时间:2012-04-19 23:44:39

标签: html css fluid-layout liquid-layout

我在创建部分流动的布局时遇到问题。布局必须有100%的宽度和高度,但它不应该有滚动条(溢出:隐藏;)。

enter image description here

上图显示了我想要实现的目标。如你所见:

  1. 标题必须固定 - 110px,宽度为100%。
  2. 两个div通过容器div包裹。蓝色的需要固定宽度130px& 100%高度,而绿色高度需要液体宽度和100%高度。
  3. 这是我目前的代码:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0px;
                color: white;
            }
            html, body {
                height: 100%;
                width: 100%;
            }
    
            .spacer {
                clear: both;
            }
    
            #header {
                background: black;
                width: 100%;
                height: 100px;
                float: left;
            }
    
            #content {
                height: 88%;
                width: 100%;
                padding: 0px;
                margin: 0px;
                position: relative;
            }
    
            #left {
                background: #1664a0;
                height: 100%;
                width: 100px;
                float: left;
            }
    
            #right {
                background: #4aa016;
                height: 100%;
                float: left;
                width: 91%;
            }
    
        </style>
    </head>
    <body>
    
    <div id="header">
        My Header
    </div>
    <div class="spacer"></div>
    <div id="content">
        <div id="left">Left container</div>
        <div id="right">Right container</div>
    </div>
    
    </body>
    </html>
    

    此代码存在以下几个问题:

    1. 它不适用于各种分辨率(800x600,1024x768,1280x1024等)
    2. “content”div并不总是将页面填充到最后。
    3. 如果您将页面调整为较低分辨率,绿色div将低于蓝色div。
    4. 我想我可能会在这里犯错误 TERRIBLY 错误,但我不是设计师所以有人能指出我解决这个问题的“正确方法”吗?

2 个答案:

答案 0 :(得分:4)

看看http://jsfiddle.net/bmqPV/2/

左边设置为100px,右边设置为91%,因此如果100px大于9%,则会转到下一行。

编辑,这是一个新的小提琴http://jsfiddle.net/bmqPV/4/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0px;
            color: white;
        }
        html, body {
            height: 100%;
            width: 100%;
        }

        .spacer {
            clear: both;
        }

        #header {
            background: black;
            width: 100%;
            height: 100px;
            position: absolute;
            top: 0px;
            left: 0px;
            z-index:3;
        }

        #content {
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0px;
            position: relative;
        }

        #left {
            background: #1664a0;
            height: 100%;
            width: 100px;
            float: left;
        }

        #right {
            background: #4aa016;
            height: 100%;
            width: 100%;
        }
        #wrapper
        {
            position: relative;
            height: 100%;
            width: 100%;}
        .contentcontainer {
            padding-top:100px;
        }
    </style>
</head>
<body>
<div id="wrapper">
    <div id="header">
        My Header
    </div>
    <div id="content">
        <div id="left">
            <div class="contentcontainer">Left container</div>
        </div>
        <div id="right">
            <div class="contentcontainer">Right container</div>
        </div>
    </div>
</div>
</body>
</html>​

答案 1 :(得分:0)

您可以通过简单的 CSS 来获得结果,并在 #content &amp; #right 为了更好地理解,请参阅简单的代码: -

    <!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
{
            padding: 0;
            margin: 0px;
            color: white;
        }
        html, body {
            height: 100%;
            width: 100%;
        }


        #header {
            background: black;
            height: 100px;
        }

        #content {
           width:100%;
           border:5px solid red;
           overflow:hidden;
           position:relative;

        }

        #left {
            background: #1664a0;
            height: 100%;
            width: 130px;
            float: left;
        }

        #right {
            background: #4aa016;
            height: 100%;
            float: left;
            width:100%;
            position:absolute;
            margin-left:130px;
        }

</style>
</head>
<body>
<div id="header">
    My Header
</div>

<div id="content">
    <div id="left">Left container</div>
    <div id="right">Right container</div>
</div>

</body>
</html>

参见演示: - http://jsbin.com/ajasey/17/edit