【发布时间】:2023-03-08 15:16:01
【问题描述】:
我有两个浮动 div,在它们之后我有一个带有 margin-top 的块元素。不幸的是,margin-top 因为浮动而不起作用。是否可以在代码中添加margin-top 而无需额外标记?
我尝试使用 :after,但没有帮助。
div {
background-color: red;
}
#left {
float: left;
height: 100px;
background-color: yellow;
}
#right {
float: right;
height: 100px;
background-color: yellow;
}
#content {
margin-top: 50px;
height: 100px;
clear: both;
}
#content:before {
clear: both;
content: "";
display: table;
}
<div id="left">left</div>
<div id="right">left</div>
<div id="content">left</div>
div {
background-color: red;
}
#left {
float: left;
height: 100px;
background-color: yellow;
}
#right {
float: right;
height: 100px;
background-color: yellow;
}
#content {
margin-top: 50px;
height: 100px;
clear: both;
}
#content:before {
clear: both;
content: "";
display: table;
}
<div id="left">left</div>
<div id="right">left</div>
<div id="content">left</div>
【问题讨论】: