我不理解为什么float right或left的div在最后一个div被声明且具有相对位置或定义了背景颜色时,不会在其上方。
以下是HTML代码:
<html>
<body>
<div class="container">
Main container <br/><br/>
<div class="header">This is the header</div>
<div class="text-right">Right text</div>
<div class="footer">This is the footer</div>
</div>
</body>
以下是CSS代码:
.header {
background-color:blue;
border: solid;
color: white;
border-color:black;
height: 100px;
}
.text-right{
float: right;
border: solid;
background-color: green;
}
.container{
padding: 10px;
border: solid;
}
.footer{
padding-top: 50px;
border: solid;
background-color: yellow;
position: relative;
}
我知道我可以使用.clear: both规则来解决这个问题,但我的主要观点是:为什么当我在 .footer 规则中设置背景颜色或位置或两者都设置时,浮动div会在页脚下面呢?
非常感谢!