Get Great Web Design Tips, WordPress Development Help,
and Genesis Framework Tutorials!

Quick Tip: Clear CSS Floats

I notice a lot of people like to float elements all the time but don’t clear them correctly. I’ll shed a little insight on this in the future but for now, here’s a quick snippet of code that I use all the time to clear css floats.

The css snippet is as follows:

.clear:before,
.clear:after {
   content:"";
   display:table;
}
.clear:after {
   clear:both;
}
.clear {
   zoom:1; /* For IE 6/7 (trigger hasLayout) */
}

The semi-serious explanation of how and why to use this.

Most people when floating and clearing an element write code similar to the following:

<div id="parentDiv">
   <div class="float:left"></div>
   <div class="float:right"></div>
   <div class="clear"></div>
</div>

The issue with this is you have to add an unnecessary empty tag everywhere. When you have a lot of elements floating you can see how this can quickly start to get frustrating.

So instead, to fix your floating woes, add the above css to your style sheet, and rework the code as follows:

<div id="parentDiv clear">
   <div class="float:left"></div>
   <div class="float:right"></div>   
</div>

Tada! Just add the clear class to the parent div. Now you just learned an incredibly easy way to clear css floats!

Resource: css tricks

If you liked this post, say thanks by sharing it:

This Site Runs on the Genesis Framework

Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!

Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.

Comments

  1. download says:

    whats your twitter?

Speak Your Mind

*

CommentLuv badge

Before you post, please prove you are sentient.

what is 6 + 9?