Skip to main content
April 1, 2026Dan Rodney/9 min read

Clearing Floats: Overflow Hidden & Clearfix

Master CSS Float Clearing Techniques for Professional Layouts

CSS Float Clearing Methods

Overflow Hidden

Forces parent elements to recognize floated children and adjust height automatically. Quick and effective for most scenarios.

Clearfix Technique

Uses CSS pseudo-elements to create invisible clearing content. Prevents content hiding issues that can occur with overflow methods.

Clear Property

Ensures elements appear below floated content rather than wrapping beside them. Essential for proper footer positioning.

Topics Covered in This HTML & CSS Tutorial:

Master two essential techniques for managing CSS float layouts: using overflow hidden to clear floats and implementing the robust clearfix methodology for professional web development.

Exercise Preview

preview clearfix

Exercise Overview

In this hands-on exercise, you'll tackle one of web development's classic challenges: managing CSS float behavior and preventing layout collapse. While modern CSS Grid and Flexbox have largely superseded floats for layout, understanding float clearing remains crucial for maintaining legacy codebases and achieving specific text-wrapping effects. You'll learn both the overflow hidden technique and the more robust clearfix methodology—skills that demonstrate your mastery of fundamental CSS concepts.

Exercise Setup Process

1

Navigate to Project Files

Open the Clearfix folder located in Desktop > Class Files > Advanced HTML CSS Class in your code editor

2

Open Starting Files

Launch volunteer.html from the Clearfix folder and preview it in your browser to see the initial layout

3

Identify Layout Goals

Recognize that the form and volunteer opportunities should display as columns on larger screens

Getting Started

  1. We'll be switching to a new folder of prepared files for this exercise. In your code editor, close all open files to avoid confusion and ensure a clean workspace.
  2. For this exercise we'll be working with the Clearfix folder located in Desktop > Class Files > Advanced HTML CSS Class. If you're using a modern editor like Visual Studio Code, Sublime Text, or WebStorm, open this entire folder as your project workspace for better file management.
  3. Open volunteer.html from the Clearfix folder.
  4. Preview volunteer.html in a browser to understand the current layout.

    • You'll see a volunteer registration form positioned above a Volunteer Opportunities section.
    • This single-column layout works well on mobile and tablet devices, but on larger desktop screens, we can improve usability by creating a two-column layout that maximizes screen real estate.

Creating 2 Columns & Clearing the Float

Now we'll transform this single-column layout into a responsive two-column design, then address the float-related issues that arise.

  1. Let's examine the HTML structure. Return to volunteer.html in your code editor.

    • Within the main tag, you'll find a div with class form-column containing the registration form, followed by an aside element with volunteer opportunities.
    • This semantic structure is ideal for creating columns—the main content (form) and supplementary content (opportunities) are clearly separated.
  2. Open main.css from the css folder within the Clearfix directory.
  3. Navigate to the bottom of the file and locate the min-width: 930px media query. Below the existing h2 rule, add these column definitions:

    .form-column {
       width: 60%;
       float: left;
    }
    aside {
       width: 35%;
       float: right;
    }

    This creates a 60/35 split with 5% natural spacing between columns—a comfortable reading width for both sections.

  4. Save the file and reload the page in your browser.

    • You should now see the form on the left and Volunteer Opportunities on the right in a clean two-column layout.
    • However, you'll notice the footer text now appears beside the right column instead of below both columns.
    • More problematically, the footer's gray top border has jumped up behind the column headers, creating a visual mess.

    This behavior demonstrates a fundamental characteristic of floated elements: they're removed from the normal document flow and don't contribute to their parent container's height calculations. The footer has moved up to fill what the browser perceives as empty space. We need to force the footer to clear past both floated columns.

  5. Return to main.css in your code editor.
  6. In the min-width: 930px media query, below the aside rule, add this clearing rule:

    footer {
       clear: both;
    }
  7. Save the file and reload the page in your browser.
  8. Scroll down to verify the footer is now positioned below both columns where it belongs.

    While the footer placement is correct, there's still an issue: the expected spacing above the footer is missing. The main tag should provide bottom padding, but it's not visible because the main container has collapsed. The clear property moved the footer down but didn't prevent the parent element collapse—a common pitfall when working with floated layouts.

Float Layout Issues

Floated elements don't take up vertical space in the document flow, causing parent containers to collapse and subsequent elements to move up unexpectedly.

Column Width Distribution

Form Column60%
Aside Column35%
Margin Space5%

Using Overflow Hidden

Let's explore a more elegant approach to clearing floats that addresses the root cause of container collapse.

  1. Return to main.css in your code editor.
  2. Delete the footer rule you just added from the min-width: 930px media query—we're going to solve this more effectively.
  3. Locate the main.content-wrapper rule within the min-width: 930px media query and add the overflow property:

    main.content-wrapper {
       padding: 40px 30px;
       overflow: hidden;
    }

    Professional Insight: The overflow property forces an element to acknowledge and accommodate all its content, including floated children. When set to hidden, it creates what's called a "block formatting context," making the container expand to contain its floated children. This is more robust than clearing individual elements because it addresses the structural issue at the container level.

  4. Save the file and reload the page in your browser.
  5. Scroll down to confirm the footer now sits properly below both columns with appropriate spacing above it.

Using the overflow property forces an element to look at its content and adjust its height even if it only contains floated elements
This is the key principle behind why overflow: hidden works as a clearfix solution

Overflow Hidden Method

Pros
Simple single-property solution
Forces parent to recognize floated children
Maintains proper spacing and padding
No additional HTML markup required
Cons
Can hide content that extends beyond boundaries
Problems with absolutely positioned elements
May crop important visual elements

When Overflow Hidden Does Not Work

While overflow hidden is powerful, it has limitations. Let's create a scenario that demonstrates when you need alternative clearing methods.

  1. Currently, the icons in the aside column sit above their accompanying text. Let's improve the visual hierarchy by floating these icons so text wraps around them naturally. Target the icons using their existing opportunity-icon class.
  2. Return to main.css in your code editor.
  3. Find the .opportunity-icon rule in the general styles section (outside any media queries) and enhance it:

    .opportunity-icon {
       width: 100px;
       float: left;
       margin-right: 20px;
    }
  4. Save the file and reload the page in your browser.
  5. In the right column, text now flows naturally around the icons, creating a more integrated design.
  6. Now resize your browser from wide to narrow, stopping just after the layout reverts to single-column mode.
  7. Scroll down and examine the School Tutor section carefully.

    float collapse

    Notice how the icon extends beyond the gray background, breaking the visual consistency. This happens because the floated icon doesn't contribute to the section's height calculation—the container only considers the text height. With no suitable HTML element available for clearing, let's attempt the overflow hidden approach.

  8. Return to main.css and locate the aside section rule.
  9. Add the overflow property to prevent section collapse:

    aside section {

    Code Omitted To Save Space

    position: relative;
       overflow: hidden;
    }
  10. Save and reload to test the change.
  11. While this fixes the container collapse and provides proper padding below the icon, we've created a new problem: the Flexible time badge is being clipped because it's positioned outside its container bounds.

    This demonstrates the primary limitation of overflow hidden: any content that extends beyond the container boundaries gets cropped. When you have absolutely positioned elements or other content that intentionally breaks container bounds, you need a different solution.

  12. Remove the overflow: hidden; property from the aside section rule—we'll implement a better approach.
Content Clipping Issue

The Flexible label gets hidden when using overflow: hidden because it extends beyond the section boundaries using absolute positioning.

Using Clearfix

The term clearing floats encompasses various techniques for managing float-related layout issues. While CSS Grid and Flexbox have largely replaced floats for layout in modern web development, understanding float behavior remains essential for legacy code maintenance and specific design requirements.

Float was originally designed for text wrapping around images—think magazine-style layouts where text flows around photographs. However, it became the primary method for creating multi-column layouts before modern CSS layout methods existed. This dual purpose often creates confusion about when and how to clear floats properly.

The clear property ensures that elements appear below floated content rather than beside it, and can resolve container collapse issues. However, clear requires an actual HTML element to work with. When no suitable element exists, or when overflow hidden would crop important content, clearfix provides an elegant solution.

Clearfix is a CSS technique that creates a virtual clearing element using pseudo-selectors, eliminating the need for extra HTML markup while avoiding the content-cropping risks of overflow hidden. This approach has become an industry standard for robust float management.

  1. Locate the footer rule in the general styles section (not within any media query) and add this clearfix implementation below it:

    .clearfix::after {
       content: "";
       display: table;
       clear: both;
    }

    How This Works: The ::after pseudo-element creates invisible content after the clearfix element. By setting display: table, we create a block-level element that responds to the clear: both property, forcing it below any floated siblings. The empty content string makes this clearing element invisible to users while still functional for layout purposes.

  2. Save the file. Now we need to apply the clearfix class to the collapsing section containers.

  3. Switch to volunteer.html and add the clearfix class to all three opportunity sections:

    <section class="clearfix">
       <div class="time-badge">Evening</div>

    Code Omitted To Save Space

    </section>
    <section class="clearfix">
       <div class="time-badge">Day</div>

    Code Omitted To Save Space

    </section>
    <section class="clearfix">
       <div class="time-badge">Flexible</div>

    Code Omitted To Save Space

    </section>
  4. Save both files and reload the page in your browser.
  5. The Flexible badge should now be fully visible, and all sections should maintain consistent padding. The clearfix has resolved the container collapse without cropping any content.
  6. Test responsiveness by resizing between wide and narrow views—both single-column and two-column layouts should render perfectly.

Overflow Hidden vs Clearfix

FeatureOverflow HiddenClearfix
ImplementationSingle CSS propertyCSS rule + HTML class
Content SafetyMay hide contentPreserves all content
HTML ChangesNone requiredAdd class to elements
FlexibilityLimitedHighly flexible
Recommended: Use Clearfix when you need to preserve absolutely positioned or overflowing content

Implementing Clearfix

1

Create CSS Rule

Add .clearfix::after rule with content, display: table, and clear: both properties

2

Apply Class

Add clearfix class to HTML elements that contain floated content

3

Test Results

Verify that parent containers maintain proper height without hiding content

Another Use of Overflow Hidden

Beyond clearing floats, overflow hidden offers another useful layout technique. Let's create a structured two-column effect within each opportunity section.

  1. Return to main.css in your code editor.
  2. Find the aside section p rule and add the overflow property:

    aside section p {
       font-size: 1rem;
       margin: 0;
       overflow: hidden;
    }
  3. Save the file and reload the page.
  4. The text now maintains consistent width alignment with the first line, creating a clean two-column effect instead of wrapping below the icon.

    The Technical Explanation: When text flows around a floated element, different lines have different available widths—shorter lines beside the float, longer lines below it. The overflow hidden property creates a new block formatting context for the paragraph, which respects the float boundary and maintains consistent width throughout the entire text block. This creates the appearance of two distinct columns rather than wrapping text.

Text Layout Control

Overflow hidden on paragraph elements prevents text from wrapping below floated images, maintaining consistent column widths throughout the text block.

Optional Bonus: Using Border-Radius to Round Images

While we're enhancing this layout, let's explore border-radius for modern visual polish—a technique that's become essential for contemporary web design.

  1. Return to main.css in your code editor.
  2. In the .opportunity-icon rule, add subtle corner rounding:

    .opportunity-icon {

    Code Omitted To Save Space

    margin-right: 20px;
       border-radius: 8px;
    }
  3. Save and reload to see the softened corners on the opportunity icons.

    The subtle rounding creates a more approachable, modern aesthetic. However, circular images often work even better for profile pictures and icons in contemporary design.

  4. Return to main.css for a more dramatic transformation.
  5. Update the border-radius to create perfect circles:

    .opportunity-icon {

    Code Omitted To Save Space

    margin-right: 20px;
       border-radius: 50%;
    }

    Pro Tip: Using 50% border-radius creates perfect circles regardless of the element's dimensions, making it ideal for responsive designs where image sizes might vary.

  6. Save and reload to see the polished circular icons—a design choice that's become standard in modern web applications and gives your layout a professional, contemporary feel.

Border-Radius Techniques

Subtle Rounding

Use small pixel values like 8px for gentle corner rounding that maintains the rectangular shape.

Perfect Circles

Use 50% border-radius to create perfect circular shapes from square images or elements.

Key Takeaways

1Floated elements don't occupy vertical space in document flow, causing parent container collapse issues
2The CSS clear property forces elements to appear below floated content rather than wrapping beside them
3Overflow hidden forces parent elements to recognize floated children and adjust height accordingly
4Clearfix technique uses CSS pseudo-elements to create invisible clearing content without additional HTML markup
5Overflow hidden can hide absolutely positioned content that extends beyond element boundaries
6Clearfix is the preferred method when content preservation is critical and overflow hidden causes clipping
7Overflow hidden on text elements prevents wrapping below floated images for consistent column layouts
8Modern CSS layout methods like Flexbox and Grid are generally preferred over float-based layouts for new projects

RELATED ARTICLES