【问题标题】:What is the issue with this simple HTML, CSS layout? [duplicate]这个简单的 HTML、CSS 布局有什么问题? [复制]
【发布时间】:2015-09-08 08:35:16
【问题描述】:

虽然这是一个非常简单的 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元素重叠?

我上传了在 PC 上的 Chrome 中执行的代码的屏幕截图。这是 Google Drive 预览链接:https://drive.google.com/open?id=0B4av_i4gqoZmRFpXbzVZekR0aGs&authuser=0

提前致谢。

【问题讨论】:

    标签: html css layout


    【解决方案1】:

    在您的 CSS 文件中添加此样式:

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

    这应该可以解决问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      相关资源
      最近更新 更多