Search code examples
htmlcssclearfix

How to properly clear these floated divs?


I thought I understood the :after method of clearfixing with CSS - but I can't seem to get it to work properly. For some reason the siblingContainer that comes after the buttonContainer containing the floated elements doesn't seem to be clearing?

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/


Solution

  • Can also be done by clearing the parent dic rather than the child.

    Instead of

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

    Use

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

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