【问题标题】:How to properly clear these floated divs? [duplicate]如何正确清除这些浮动 div? [复制]
【发布时间】:2015-11-07 22:21:25
【问题描述】:

我以为我了解:after 使用 CSS 进行 clearfixing 的方法 - 但我似乎无法让它正常工作。出于某种原因,包含浮动元素的 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;
}

小提琴: http://jsfiddle.net/np0c6feh/1/

【问题讨论】:

    标签: html css clearfix


    【解决方案1】:

    也可以通过清除父 dic 而不是子 dic 来完成。

    代替

    .buttonContainer div:last-child:after {
      content: "";
      display: block;
      clear: both;
    }
    

    使用

    .buttonContainer:after {
      content: "";
      display: block;
      clear: both;
    }
    

    http://jsfiddle.net/AgentStephens/np0c6feh/3/

    【讨论】:

    • 这个和我的一样吧?好的,所以您正在为最后一个孩子添加 clearfix!不错。
    • @PraveenKumar 它和你现在的一样,但我发布的时候不是这样的,哈哈
    • 好吧好吧。当然。 :)
    • 这就是我要找的,谢谢!另外 - 我接受这个作为答案,因为虽然@PraveenKumar 确实首先发布了他的第一个答案不是我想要的,并且在发布这个答案后他改变了它。
    • @jessikwa 是的,没关系。但是我没有复制。顺便说一句。
    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多