HTML& CSS Floats有帮助(非常基础)

时间:2014-02-22 22:23:30

标签: css-float

我一直在做codeacademy HTML / CSS课程并且直到我不得不'建立简历'结束时才理解它。我在练习开始时将我的代码与示例进行了比较,但我无法理解为什么我的.right类位于最右边而没有正确排列。页眉和页脚的宽度也相同(95%),但页脚明显较小,并且不会像标题一样在屏幕上伸展。

有什么想法吗?

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

div {
    border-radius: 5px;
}

#header {
    width: 95%;
    height: 50px;
    background-color: lightblue;
    position: fixed;
    z-index: 1;
}

.left {
    position: relative;
    background-color: lightgreen;
    height: 400px;
    width: 20%;
    float: left;
    margin-top: 60px;
}

.right {
    position: relative;
    background-color: lightgray;
    height: 400px;
    width: 74%;
    float: right;
    margin-top: 60px;
    margin-bottom: 10px;
}

#footer {
    position: relative;
    background-color: gray;
    width: 95%;
    height: 60px;
    clear: both;
}

1 个答案:

答案 0 :(得分:0)

由于.right,你的float: right;坐在最右边,告诉div向右浮动,直到它碰到某些东西(屏幕/浏览器边缘或另一个div)。如果您希望它与您的左侧div紧密相连,请尝试float: left;,这会将该div浮动到您现有的.left div上。