logologo_light
  • Home
  • Maui Web Design
  • Maui Web Design Blog

CSS: Clearing Floats with Overflow

One of the common problems we face when coding with float based layouts is that the wrapper container doesn’t expand to the height of the child floating elements. The typical solution to fix this is by adding an element with clear float after the floating elements or adding a clearfix to the wrapper. But did you know you can also use the overflow property to fix this problem? It’s not a new CSS trick either. It’s been documented before long long ago. Today I would like to revisit the topic along with a few tips.

View Demo Clear Floats

Demo 1:

The demo below shows the issue with floating child elements where the parent container doesn’t expand. To fix this problem, you can simply add the CSS property overflow:auto (or overflow:hidden) to the wrapper container. This is perhaps the simplest way to clear floats.

overflow auto

.container {
overflow: auto;
}

Demo 2:

Overflow:auto can also be used to prevent content from wrapping around the floated element. Let’s say you are designing a comment list. You would, most likely, have an avatar floating on the left and the comment to the right. To prevent the comment from wrapping around the avatar just add overflow:hidden to the comment container. The advantage of using overflow here is that I don’t have to set a float or width to the comment container. The container automatically aligns after the floated avatar image.

overflow auto

.image {
float: left;
}


.text {
overflow: hidden;
}

Drawbacks (see demo)

Although it is a nice trick to clear floats there are some drawbacks:

  • Using overflow:auto will cause a scrollbar if your content is extending the boundary of the container. For example, if you have a long unbreaking text (ie. long url text) or a large image that is bigger then the container a scrollbar will show.
  • To avoid a scrollbar from showing you should use overflow:hidden. Again, there is a drawback to this method as well. Using overflow:hidden will hide any content that exceeds the boundry of the container.

Word-wrap

To solve the long unbreaking text issue, simply add word-wrap:break-word to the container and it will force the text to wrap onto a new line.

.container img {
max-width: 100%;
height: auto;
}

Max-width

To prevent large images from extending the boundar, add max-width:100% and it will resize the image to the max width of the container.

.container {
word-wrap: break-word;
}

By Web Designer Wall

  • Posted by admin
  • On February 9, 2011
  • 0 Comment

Leave Reply Cancel reply

You must be logged in to post a comment.

Recent Posts
  • The EMs have it: Proportional Media Queries FTW!
  • How to Create a Flashy Menu Using CSS3
  • A Farewell to CSS3 Gradients
  • Focus on Typography: Space
  • Position Text Labels: Forms Using CSS
Recent Comments
    Archives
    • March 2012
    • February 2012
    • January 2012
    • November 2011
    • October 2011
    • September 2011
    • July 2011
    • March 2011
    • February 2011
    • October 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    Categories
    • animation
    • box model
    • box shadow
    • Browsers
    • code
    • CSS
    • CSS3
    • Design
    • duration
    • Element
    • fluid layouts
    • fonts
    • forms
    • framework
    • hacks
    • HTLM5
    • HTML
    • inheritance
    • layout
    • media queries
    • mobile web design
    • News
    • positioning
    • presentation
    • Properties
    • responsive web design
    • Scripting
    • Selectors
    • specificity
    • standards
    • techniques
    • transform
    • transformation
    • transition
    • typeface
    • typography
    • Uncategorized
    • W3C
    • Web Design
    • Web Designer
    • website
    Meta
    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    CSS In Depth Part 2: Floats & Positions

    CSS letter-​​spacing and Where to Use It

    Scroll
    Style your website with Aloha!

    Web Styles Hawaii is based in Maui Hawaii, offering affordable web design and development services geared towards keeping your website fresh and current on today’s web. Specializing in Responsive Web Design, Web Styles Hawaii will help you get your business online or modernize your current website using the latest technologies available.

    Recent Posts
    • The EMs have it: Proportional Media Queries FTW! March 29, 2012
    • How to Create a Flashy Menu Using CSS3 February 16, 2012
    • A Farewell to CSS3 Gradients February 8, 2012
    @2026 Web Styles Hawaii