demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

float: center With Columns

Float centre examplefloat:centre does not actually exist, for reasons I will explain shortly. But it is possible to fake it. This is such a neat trick that I wish I had come up with it, but credit goes to Chris Coyier; what is shown here is merely my variation on his technique.

I’m often asked “if there’s float: left and right, why isn’t there a float: centre ?” A few answers:

  1. text-align: centre already covers most cases.

  2. text wrapped on both sides of an image makes lines in a paragraph extremely difficult to scan, unless the text is treated very carefully. Usually this means division of text into columns, which is a separate issue entirely.

  3. float should really be called wrap. “float: left" simply means “put this element on the left side of its container, and wrap everything that follows to its right”. In this context, our float: centre would really be wrap: both, which brings up an entire suite of issues (how do you determine how “deep” the element is inside its container element?).

To use this technique, we build a separate <div> for each column, and divide our text between them. In my variation, the image we want to use between the columns is placed in the start of the first <div>, and floated right.

  1. <div id=”leftcol”>
  2. <p><img src=”kyoto-monk.jpg” style=”width: 240px; height: 240px; float: right;
  3. margin-left: 20px;” alt=”Monk in front of kabuki theatre, Kyoto”
  4. title=”Monk in front of kabuki theatre, Kyoto” />
  5. Kyoto, located on the island of Honshu, was once Japan’s imperial capital. The
  6. city is Japan’s cultural centre, best known for its gardens, traditional
  7. architecture, university, and the Nintendo game company, which has its
  8. headquarters in the city.</p>
  9. </div>
  10. <div id=”rightcol”>
  11. <p>Kyoto was largely spared from the physical destruction of WWII. It was
  12. removed from its place at the top of the target list for the atomic bomb by the
  13. intervention of the US Secretary of War Henry L. Stimson, who had honeymooned
  14. there. Photo by localJapanTimes.</p>
  15. </div>

Then we give each <div> an equal width by setting each to display: table-cell:

  1. div#leftcol, div#rightcol { display: table-cell; padding: 1em; }

So far, we have a fairly conventional layout: two column divs, with one holding a floated image. The trick lies in two parts: first, we displace the image by using a negative margin-right on it. This displacement is just over half the width of the image:

  1. <img src="kyoto-monk.jpg" style="width: 240px; height: 240px;
  2. margin-right: -150px; margin-left: 20px;" />

The second part of the trick is to use generated content for the second <div> that is sized to slightly more than half the width and a little more then the full height of the image, to provide the impression of “margin” around it in the right <div>:

  1. div#rightcol:before { content: " "; float: left; width: 140px; height: 260px; }

This pushes a placeholder into the second <div>, which the image can then occupy.

The one major disadvantage of Chris’s technique is that it cannot be used with CSS3 "newspaper style" columns (at least not in any way I can determine, although I hold out hope that the CSS3 Fluid Box model or JavaScript addressing may offer some solutions). Two smaller limitations are (1) the image must be at the top of the <div>; you cannot, unfortunately, place the illustration in an arbitrary vertical location; and (2) (and this almost goes without saying) it does not work in IE7 or earlier.

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.