如何正确清除这些浮动的 div?
How to properly clear these floated divs?
我以为我理解 :after
clearfixing 的方法 CSS - 但我似乎无法让它正常工作。出于某种原因,包含浮动元素的 buttonContainer
之后的 siblingContainer
似乎没有清除?
HTML:
<div class="buttonContainer">
<div class="action_button">Button 1</div>
<div class="action_button">Button 2</div>
</div>
<div class="siblingContainer"></div>
CSS:
.action_button {
font-family: Arial;
font-size: 9pt;
font-weight: bold;
background-color: #f2c059;
color: white;
border-radius: 5px;
display: inline-block;
padding: 5px;
cursor: pointer;
}
.buttonContainer {
margin: 5px;
}
.buttonContainer div {
margin: 0 5px;
float: right;
}
.buttonContainer div:last-child:after {
content: "";
display: block;
clear: both;
}
.siblingContainer {
height: 500px;
background: gray;
}
fiddle:
http://jsfiddle.net/np0c6feh/1/
也可以通过清除父 dic 而不是子 dic 来完成。
而不是
.buttonContainer div:last-child:after {
content: "";
display: block;
clear: both;
}
使用
.buttonContainer:after {
content: "";
display: block;
clear: both;
}
我以为我理解 :after
clearfixing 的方法 CSS - 但我似乎无法让它正常工作。出于某种原因,包含浮动元素的 buttonContainer
之后的 siblingContainer
似乎没有清除?
HTML:
<div class="buttonContainer">
<div class="action_button">Button 1</div>
<div class="action_button">Button 2</div>
</div>
<div class="siblingContainer"></div>
CSS:
.action_button {
font-family: Arial;
font-size: 9pt;
font-weight: bold;
background-color: #f2c059;
color: white;
border-radius: 5px;
display: inline-block;
padding: 5px;
cursor: pointer;
}
.buttonContainer {
margin: 5px;
}
.buttonContainer div {
margin: 0 5px;
float: right;
}
.buttonContainer div:last-child:after {
content: "";
display: block;
clear: both;
}
.siblingContainer {
height: 500px;
background: gray;
}
fiddle: http://jsfiddle.net/np0c6feh/1/
也可以通过清除父 dic 而不是子 dic 来完成。
而不是
.buttonContainer div:last-child:after {
content: "";
display: block;
clear: both;
}
使用
.buttonContainer:after {
content: "";
display: block;
clear: both;
}