如何仅通过CSS制作动态布局

时间:2012-07-06 05:24:56

标签: html css

我想创建一个这样的布局:

  1. 左栏(黄色):宽度150px 常量 - 高度动态
  2. 中间栏(天蓝色):宽度动态 - 高度动态
  3. 右列(绿色):宽度150px 常量 - 高度动态
  4. 页脚(红色):宽度动态 - 高度常量
  5. 我正在寻找一个仅限CSS的解决方案。

    jsFiddle here

5 个答案:

答案 0 :(得分:5)

http://fiddle.jshell.net/2bSaP/show/

HTML:

<div id="container">
    <div id="yellow"></div>
    <div id="blue"></div>
    <div id="green"></div>
    <div id="red"></div>
</div>

CSS:

body {
    margin: 0;
}

#yellow {
    background: yellow;
    position: absolute;
    width: 150px;
    top: 0;
    left: 0;
    bottom: 155px;
}

#blue {
    background: rgb(98, 196, 255);
    position: absolute;
    top: 0;
    right: 155px;
    left: 155px;
    bottom: 155px;
}

#green {
    background: green;
    position: absolute;
    width: 150px;
    top: 0;
    right: 0;
    bottom: 155px;
}

#red {
    background: brown;
    position: absolute;
    height: 150px;
    left: 0;
    right: 0;
    bottom: 0;
}

答案 1 :(得分:0)

用于  现在你忘了在你的css中position定义position value

就像这样

#blue{
position: absolute;
}

答案 2 :(得分:0)

不确定这是否是您想要的,但我摆脱了#Blue的所有身高规则,只是添加了height: 100%,然后#Red更高z-index (例如55)。

现在中心方块似乎总是蓝色。

http://jsfiddle.net/qXKZh/38/

希望这有帮助

答案 3 :(得分:0)

我真的不确定你要完成什么。它应该看起来像MusikAnimal的jsfiddle的样子吗?

如果是这样,我会采用不同的结构(完整的正确高度和宽度......):

<div id="for3columns" style="height: 500px;">
  <div id="yellow_column" style="float: left; width: 20%; height: 100%"></div>
  <div id="blue_column" style="float: left; width: 60%; height: 100%"></div>
  <div id="green_column" style="float: left; width: 20%; height: 100%"></div>
</div>
<div id="lower_red" style="clear:both; width: 100%; height: 100px;"></div>

答案 4 :(得分:0)

理解你的问题有点困难,但从我收集的内容来看,我能够找到这个解决方案:Fiddle

HTML

<div class='container'>
    <div class='col col-left'></div>
    <div class='col col-mid'></div>
    <div class='col col-right'></div>
    <div class='col col-bottom'></div>
</div>​

CSS

.container {
    width: 960px;
    margin: 0 auto;
}

.col {
    margin: 0;
    padding: 0;
    float: left;
}

.col-left {
    width: 25%;
    height: 200px;
    background: #f90a7b;
}

.col-mid {
    width: 50%;
    height: 200px;
    background: #e3f606;
}

.col-right {
    width: 25%;
    height: 200px;     
    background: #46c704;
}

.col-bottom {
    width: 100%;
    height: 200px;
    background: #0654e0;
}​