如何解释以下的css现象

<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8"/>
    <title>原理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1 user-scalable=no"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="renderer" content="webkit"/>
    <style>
      
        .float {
            float: left;
            height: 100px;
            background: rebeccapurple;
        }
        .box {
            clear: both;
            background: green;
            height: 100px;
            margin-top: 10px;
        }
    </style>
</head>
    
    
<body>
   <div class="float">这是一段文字</div>
    <div class="box">这是一段文字</div>
</body>
</html>

在box元素设置 为clear:both 时 不断增大其 margin-top的值,或者减少margin-top的值
box元素并不会移动
当box元素没有设置clear:both 时 不断增大其 margin-top的值,或者减少margin-top的值
box元素 和 float 元素会一起移动

阅读 1.3k
1 个回答

主要还是外边距折叠搞的鬼,你可以先去了解一下外边距折叠。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题