这个简单的HTML,CSS布局有什么问题?

时间:2015-06-22 18:53:20

标签: html css layout

虽然它是一个非常简单的HTML,CSS布局,但我遇到了这个问题。它看起来不像我想要的那样。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <style>
        body {
            margin: 0;
            padding: 100px;
            width: 100%;
        }
        div.container {
            border: 3px red solid;
            padding: 10px;
            display: block;
            width: 1000px;
        }
        div.left, div.right {
            float: left;
            margin: 0 10px;
        }
        div.left {
            background: blue;
            height: 300px;
            width: 300px;

        }
        div.right {
            background: green;
            width: 300px;
            height: 300px;
        }
    </style>
</head>
<body>
    <div class="container">This is the main container
        <div class="left">This is the left side</div>
        <div class="right">This is the right side</div>
    </div>

    I want to know why left and right blocks overlaps the container element?
</body>
</html>

为什么左右块与container元素重叠?

我上传了Chrome在Chrome上执行的代码截图。以下是Google云端硬盘预览链接:https://drive.google.com/open?id=0B4av_i4gqoZmRFpXbzVZekR0aGs&authuser=0

提前致谢。

1 个答案:

答案 0 :(得分:1)

在CSS文件中添加此样式:

.container:before,
.container:after {
    display: table;
    content: " ";
}

.container:after {
    clear: both;
}

这应解决问题!