21
<html>

<head>

<style type="text/css">

  .container {
      width: 900px;
      border: 2px solid #333333;
      padding-top: 30px;
      padding-bottom: 30px;
  }

  .container_left {
      border: 2px solid #FF00FF;
      width: 650px;
      float: left;
  }

  .container_right {
      border: 2px solid #0000FF;
      width: 225px;
      float: right;
  }

</style>
</head>

<body>

    <div class="container">
        <div class="container_left">
        <div>LEFT CONTAINER</div>
        <div>LEFT CONTAINER</div>
        <div>LEFT CONTAINER</div>
        </div>

        <div class="container_right">
        <div>RIGHT CONTAINER</div>
        <div>RIGHT CONTAINER</div>
        <div>RIGHT CONTAINER</div>
        </div>
    </div>

</body>
</html>

The result is:
result

I want a result like this:
desired result

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jorge
  • 5,610
  • 18
  • 47
  • 67

7 Answers7

39

Add overflow: hidden; to the .container selector. This will force the container to acknowledge that it has children.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
25

Give the container a

overflow: auto

or

overflow: hidden

see this page on quirksmode.org for details on the issue.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
12

A quick fix is to add overflow: hidden to your .container.

This is not the best solution per say, merely the quickest fix. Your best solution would be to implement and apply clearfix as it doesn't have issues with printing due to overflow.

In the event you use overflow: auto or overflow: hidden and a user attempts to print the page, content that does not fit on the printed page will be clipped because:

  1. scroll-bars do not print
  2. hidden content does not display
Corey Ballou
  • 42,389
  • 8
  • 62
  • 75
  • 1
    +1 because this is the only answer that mentions downsides to the `overflow` method. It's also worth mentioning that this method may result in unwanted clipping/scrollbars where the content would otherwise overflow. – eyelidlessness Jan 07 '10 at 15:39
5

One option is to put in a <div style="clear: both;"></div> just before closing the container div.

<div class="container">

    <div class="container_left">
    <div>LEFT CONTAINER</div>
    <div>LEFT CONTAINER</div>
    <div>LEFT CONTAINER</div>
    </div>

    <div class="container_right">
    <div>RIGHT CONTAINER</div>
    <div>RIGHT CONTAINER</div>
    <div>RIGHT CONTAINER</div>
    </div>

    <div style="clear: both;"></div>

</div>
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
1

to the outer div you might want to use the clearfix css, explained here: http://www.positioniseverything.net/easyclearing.html

John Boker
  • 82,559
  • 17
  • 97
  • 130
1
.clear:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
* html .clear { height: 1%; }
*:first-child+html .clear { min-height: 1px; }

Apply .clear to your parent element.

eozzy
  • 66,048
  • 104
  • 272
  • 428
1

This might be of help "Clearing a float container"

BD.
  • 880
  • 5
  • 10