如何使.left类出现在标题下方但保持标题位置固定?

时间:2013-05-28 17:54:41

标签: css css-float css-position

我是css的新手并且正在努力学习定位。我已经设置了以下样式表:

div{
    border-radius: 5px;
    border: 1px solid black;
}

#header{
    width: 550px;
    position: fixed;
    background-color: #e8e8e8;
    height: 100px;
    z-index: 1;
}

.left{

    float: left;
    display: inline-block;
    position: relative;
    background-color: #000000;
    height: 300px;
    width:275px;
}

.right{
    float: right;
    background-color: #0000ff;
    width: 275px;
   height: 300px;
}

#footer{
    clear: both;
    background-color: orange;
    width: 550px;
    height: 50px;
}

这是html:

<!DOCTYPE html>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
    <title></title>
</head>
<body>
    <div id="header"></div>
    <div class="left"></div>
    <div class="right"></div>
    <div id="footer"></div>
</body>
</html>

是否与标题选项卡的绑定固定位置有关,使得.left类出现在最顶层。如何使.left类出现在标题下方,但将标题位置保持固定?

由于

2 个答案:

答案 0 :(得分:1)

位置fixed将内容从传统流程中拉出,就像位置absolute一样。假设您想要将其修复到顶部,您还需要添加top: 0;以便它知道应该在哪里坚持。这里有一些更新的代码可以解决你的问题。

样式:

div {
    border-radius: 5px;
    border: 1px solid black;
}

#header {
    width: 550px;
    position: fixed;
    top: 0;
    background-color: #e8e8e8;
    height: 100px;
    z-index: 1;
}

#content {
    border: 0;
    margin-top: 110px;
}

#content:after {
    content: "";
    height: 0;
    clear: both;
    display: block;
}

.left, .right {
    height: 300px;
    width: 275px;
}

.left {
    float: left;
    background-color: #000000;
}

.right {
    float: right;
    background-color: #0000ff;
}

#footer{
    background-color: orange;
    width: 550px;
    height: 50px;
}

另外,我建议将浮动元素放入自己的容器中,并将margin-top添加到该容器中。您还会看到我为#content div添加了一个明确的修复,以便在页脚上没有必要。

<div id="header"></div>
<div id="content">
    <div class="left"></div>
    <div class="right"></div>
</div>
<div id="footer"></div>

答案 1 :(得分:0)

从所谓的文档流中删除绝对定位的元素。其他元素不再关心它们 - 对它们来说,它们并不存在。换句话说:你描述的行为应该是那样的。

但是,您可以使用margin-top将其他元素向下推到页面上。

.left, .right {
    margin-top: 100px;
}