CSS Floats

Often you’ll want an item to be left or right aligned with text wrapped around it, this is what CSS Floats are used for. CSS Floats are used in the majority of CSS layouts, they’re very handy and you should learn how they work inside and out.

The best example to use for CSS floats is floating an image to the left or right and having text wrap around it. Take the following example:

CSS Float

CSS Float


To achieve the above is easy, let’s take a look at the mark-up.
[code]<div>All of your text here. <img src="images/floated.jpg" alt="Floats" class="floatleftexample" /> and the rest of your text here </div>[/code]
And now the CSS…
[code].floatleftexample { float:left; }[/code]
This will force the image to ‘float’ to the left and all text to flow around it.

If you use floats on block elements you can quite easily make very effective columned layouts. Floating two divs to the left and setting widths will give you two adjacent elements. Handy.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Live
  • MSN Reporter
  • MySpace
  • PDF
  • Ping.fm
  • Posterous
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
This entry was posted on Wednesday, January 27th, 2010 at 11:43 pm and is filed under CSS Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post a Comment