7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. HTML & CSS

CSS Fundamentals: Containing Children

Read Time: 5 mins

I've received multiple requests for simpler CSS tutorials that teach the tricky fundamentals. This will serve as the first entry in a series that will receive new additions sporadically each month. Today, we'll be reviewing the overflow: hidden, and clearfix tricks to force a parent div to contains its children.

The Overflow: Hidden Trick

Have you ever noticed that when you float all of the children elements within a div, the parent takes up zero space? For example, in your code editor, adding the following within the body tag.

Now, let's add a bit of CSS to simulate a typical website.

Above, we're simply setting background colors and floating the sidebar and main divs to the left and right, respectively. Note the "padding-bottom: 2em;". This should allow us to see the red background at the very bottom, right? View the page in your browser, and you'll see:

No Overflow AppliedNo Overflow AppliedNo Overflow Applied

Where did the red background go? Why isn't it displaying?

The Solution

When you float all of the children, the parent essentially takes up no space. To better illustrate this fact, let's set an arbitrary height of 50px to the container, and then reduce the opacity of the children divs so that we can see the red background beneath.

Refresh your browser, and you'll see:

Not ContainedNot ContainedNot Contained

How odd. We've specified a height of 50px for our container div, yet the main and sidebar divs blatantly overflow its boundaries, like spoiled bratty divs.

Return to your stylesheet, and apply one style:

After another refresh, we see:

Not ContainedNot ContainedNot Contained

Well that partially helps. Now, we don't have to worry about the pubescent children disobeying their parent. Having said that, this really doesn't help our situation.

"Try to avoid specifying heights as much as possible. There's usually a smarter method.

The solution is to rip out the height property from our container. Remove the following property.

One last refresh, and our problem seems to be fixed.

Overflow AppliedOverflow AppliedOverflow Applied
You can also remove the opacity properties. They were just for demonstration purposes.

The Rub

The method demonstrated above will work in most cases. However, let's introduce another variable. What if we want to position an image on the border of our container, so that it overlaps. You've seen this effect many times. For the sake of the example, we'll just use an image of a circle with a transparent background. On a real site, this might represent a "Buy Now" or "Sign Up" button -- something cheesy like that.


Positioning the Circle

Using CSS, let's position the image in the top right portion of our "website", overlapping the edges. This is what we want:

Breaking BoundariesBreaking BoundariesBreaking Boundaries

First, we reference the image within our HTML.

Next, return to your stylesheet, and add the following styles.

Positioning Context

One might think that this will place the image just over the right edge of the container div. However, he'd be wrong.

Because we have not set a positioning context, the window will be used instead.

No positioning context setNo positioning context setNo positioning context set

Obviously, this is not what we want. To apply a positioning context to our container div, simply add "position: relative;" to #container. Once we've done so, the image will no longer use the window as a reference.

Lost HalfLost HalfLost Half

What's the Problem Now?

But now, we have a new problem! Because we set overflow:hidden to our container div, we've somewhat shot ourselves in the foot. How do we break boundaries and take names if overflow is set to hidden? Should we simply accept that this particular website won't be taking names today? Absolutely not. In these cases, it's worth using a different method.

The Clearfix Trick

With this method, we'll use CSS to add content after our container div. This created content will then clear our div, thus forcing it to contain its children. Now obviously we don't want to see this content, so we need to be sure to hide it from the viewer.

Return to your stylesheet, remove "overflow: hidden;" from your container div, and add the following:

This might appear complicated, but I assure you that it's quite simple.

  • _height: Triggers "haslayout" in Internet Explorer, by using the underscore trick to target IE6 directly.
  • content: After the container div, append a period.
  • visibility: We don't want to see the period, so hide it from the page. (Equal to setting opacity: 0;)
  • display: Forces the period to display as a block-level, rather than inline.
  • clear: The important property. This clears the main and sidebar divs. This is the same as adding an unsemantic <div style="clear: both;"> to our page.
  • height: Don't take up any space.
  • font-size: Just a precaution for Firefox. This browser sometimes adds a bit of space after our parent element. Setting the font-size to zero fixes this.


Though the overflow:hidden trick is preferable, it's not always ideal. You need to use the best solution for the task at hand. The important thing is to learn each method, so that you have the tools to solve the puzzle.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Scroll to top
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.