【问题标题】:CSS layout with fixed and liquid columns具有固定列和流动列的 CSS 布局
【发布时间】:2012-05-01 13:52:09
【问题描述】:

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

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

  1. 标题必须固定 - 110 像素,宽度为 100%。
  2. 通过容器 div 包装的两个 div。蓝色需要固定宽度 130 像素和 100% 高度,而绿色需要固定宽度和 100% 高度。

这是我当前的代码:

<!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. “内容”div 并不总是将页面填满。
  3. 如果您将页面大小调整为较低的分辨率,绿色 div 会低于蓝色 div。

我想我可能在这里做错了TERRIBLY,但我不是设计师,所以有人可以指出解决这个问题的“正确方法”吗?

【问题讨论】:

  • 你为什么要float#header???

标签: html css fluid-layout liquid-layout


【解决方案1】:

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

您将左侧设置为 100 像素,右侧设置为 91%,因此如果 100 像素大于 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>​

【讨论】:

  • 是的,我在 firebug 中更新它而不是在 fiddle,所以我不得不重新发布,对不起
  • 哇!!谢谢!这解决了右列问题!有没有办法删除当您调整为较低分辨率时出现的滚动条?我很确定它是因为#content{height: 88%;} 而出现的,但我不知道如何设置#content 来填充页面?
  • @tftd,您始终可以将overflow: hidden 放入html, body,但这取决于您希望内容在其容器太小时时的行为方式。
  • @JeffB 我在几个教程中读到过这个,人们建议设置min-width/min-height 来解决这个问题。我希望这是正确的做法?
  • 是的 min-height 和 min-width 会有所帮助,如果您不希望网站小于某个大小。
【解决方案2】:

您可以通过简单的 CSS 来实现您的结果,并在 #content#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多