Floats - Not Just for Images

An entry about css Publication date 10. September 2007 17:32

I'm sure you've used the style float:left/right to make text flow around an image. However, the float style can also be a powerfull ally in other scenarios - a great example is how easy it is to left-align and right-align text on the same line using it:

<div style="background-color:White; border: solid 1px gray; padding:4px; width:350px">
    <div style="float:left">This text is on the left</div>
    <div style="float:right">This text is on the right</div>
    <div style="clear:both" />
</div>

The above markup renders:

Left and right-aligned text

The trick is really knowing the relationship between the float and clear styles. If we had left out the div with the clear:both style, it would render like this instead:

Left and right-aligned text, uncontained

If you want to know the reason for that behavior, read up on the concept of containing floats :)

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.

NDC 2010

The conference to attend this summer happens June 16th-18th in Oslo, Norway. Are you going? Be sure to catch my talk on AOP while you're there!

 

Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS