Block Formatting Context Demo

by audetwebdesign

HTML

<h3>Basic Case</h3>
<div class="wrap basic">
    <p>Some text...</p>
    <div class="floated">Floated text</div>
</div>

<h3>Case 1 - Floated Child Element</h3>
<div class="wrap ex1">
    <p>Some text...</p>
    <div class="floated">Floated text</div>
</div>

<h3>Case 2 - Floats Within Floats</h3>
<div class="wrap ex2">
    <p>Some text...</p>
    <div class="floated">Floated text and some extra words...</div>
</div>

<h3>Case 3 - Floats Within overflow: hidden</h3>
<div class="wrap ex3">
    <p>Some text...</p>
    <div class="floated">Floated text</div>
</div>

CSS

.wrap {
    border: 1px dotted blue;
    margin-bottom: 3.0em;
}
.floated {
    background-color: yellow;
    float: left;
}
p {
    background-color: lightgray;
    margin: 5px 0;
}
h3 {
    clear: both;
    margin: 0 0 0.50em 0;
}

.wrap.basic .floated {
    float: none;
}

.wrap.ex2 {
    float: left;
}

.wrap.ex3 {
    overflow: hidden; /* value of 'auto' will also work */
}