Super Ragged Floats

CSS has enabled immense flexibility in the positioning of images on HTML pages. If used correctly, it can help create page designs match that of print. Using CSS, it is now possible to wrap text tightly around images, similar to that seen in printed books. Of course it isn't exactly news that CSS can be used for wrapping text. I have seen large number of sites using sliced images for wrapping text around ragged outlines. I didn't want that so I present an alternative that I just discovered.

Using Image Slices

Some time back, Eric Meyer had put up a neat tutorial on creating ragged floats, wrapping text around an irregular outline of an image. This, he achieved by slicing the image into horizontal strips and placing them one below the other. With my example it would be like this -









And so on. See a full example of this method.
(Borders have been shown to distinguish the images)

To achieve wrapping of text around the ragged outlines of the image, he stacked up the image slices and floated them using this class -

  img.flowing { 
      float: left; 
      clear: left; 
      margin: 0 2em 0 0;
  }

That's it! Pretty simple and clean. But it has its own disadvantages. what if I do not want to slice images? What if I want finer control on the wrapping? Do I need to re-slice the images with different heights?

An Alternative

In simple words, we create a div with background as this image. Then use spacers or ideally divs again to wrap the text around the background image's irregular outline. The following is the id definition for the container div -

  #toycycle{
      background-image: url(toycycle.jpg);
      background-repeat: no-repeat
      background-attachment: scroll;
      margin: 0px;
      padding: 0px;
  } 

This tells the browser to put toycycle.jpg as the background image for the div. The scroll value fixes the image relative to the div position and allows it to move alongwith the div. Also the no-repeat value prevents tiling of the image.

For arranging the outline, you can either use spacers or divs. For both, the CSS class remains the same as the earlier method --

  .flowing { 
      float: left; 
      clear: left; 
      margin: 0px 2em 0px 0px;
  }

The float:left attribute allows the image to stick to left side of the div and the clear:left attribute clears up any text on the left side of the image. You can increase or decrease the space between images and text by changing the margin attribute. In case of divs, add a font-size:8px; attribute to negate any effects of font size changes on the divs. Now, you use the id and class in this way --

<div id="toycycle">
  <img src="0.gif" alt="0" class="flowing" border="0" width="105" height="20" />
  <img src="0.gif" alt="0" class="flowing" border="0" width="110" height="20" /> 
  <img src="0.gif" alt="0" class="flowing" border="0" width="115" height="20" /> 
  <img src="0.gif" alt="0" class="flowing" border="0" width="110" height="20" /> 
  <img src="0.gif" alt="0" class="flowing" border="0" width="98"  height="20" />
...
  <img src="0.gif" alt="0" class="flowing" border="0" width="220" height="20" />
  <p> Text goes here </p>
</div>

In case of divs --

<div id="toycycle">
  <div class="flowing" style="width:105px; height:20px;">&nbsp;</div>
  <div class="flowing" style="width:110px; height:20px;">&nbsp;</div>
  <div class="flowing" style="width:115px; height:20px;">&nbsp;</div>
  <div class="flowing" style="width:110px; height:20px;">&nbsp;</div>
  <div class="flowing" style="width:98px; height:20px;" >&nbsp;</div>
...
  <div class="flowing" style="width:220px; height:20px;">&nbsp;</div>
  <p> Text goes here </p>
</div>

Place the transparent spacer gifs before the text and place them only as much required. Assuming the background image has a height of 400 pixels, if you take each image 20pixels high, you'll need about 20 spacers/divs stacked up to cover up the whole image. Adjust the width of each image accordingly. You can experiment with height to achieve smooth text flow over the image. I have observed that if the height is more or less equal to the text, you get smooth fits.

See a fully functional example of this alternative technique, with divs and with spacer gifs.

Why use this method?

  • It does away with the need of slicing images.
  • Easy to change parameters in case the text does not fit well.
  • More compatible with 5.x browsers. Eric's method has a problem with IE5.0 placing the images above text and Opera 5.x placing text above the images. This technique shows up almost similar in most browsers including the latest ones.
  • It degrades better than Eric's method(if you use divs, which I think are preferrable).

Happy wrapping!

Comments

w00t! w00t! first post!

no seriously folks, i do have something say.

instead of alt="0" on each of your images, you should instead do alt="". that way aural browsers won't read, 0 0 0 0 0 0 0 0 0 0 0. am i right about this or what?

anyways, let me know if i'm wrong.

chris.

ALT tags

Yes, ALT tags are meant to explain what the picture adds to the page.... the answer is "nothing" then you should leave it blank (whether that means alt='' or alt=' ' is up for debate) Interesting method.... might have to try that sometime.

Re: ALT Tags

Yes, they should be blank. And they are. Check the page source. I don't remember how they got there in the source. :-)

Attribute, not tag

For the record, it's an alt "attribute," not an alt "tag." A tag is the thing with the angle brackets. An attribute is the thing with an equal sign and quotes.

Re: Attribute, not tag

> For the record, it's an alt "attribute," not an alt
> "tag." A tag is the thing with the angle
> brackets.

And if you want to be really, really nitpicky, it's not a "tag" it's an "element".

:-),

Adam

great work ...

(but your examples do not work in ie 5.X osx
but does in all my other browsers!
hans-frederic!

Attribute, not tag

And if you want to be really, really nitpicky, it's not a "tag" it's an "element".

No, an element doesn't have the angle brackets (OTOH, an attribute doesn't have equals-sign/quotes). But this discussion is getting off-topic...

Oh please make it stop

We're slashdotting this article into an argument of terminology? Someone please make it stop.

Re: great work

(but your examples do not work in ie 5.X osx but does in all my other browsers!
I do not have access to a Mac and so I couldn't test it there. If you are able to correct the problem, can you put the code here for everyone's benefit?

Why not spans?

You can set them to "display: block" and they will fall back in a cleaner way on older non-CSS browsers. A block of twenty DIV tags is likely to make the content following the image disappear off the bottom of the page.

ie 5.X mac???

Well i tried ! sorry can't get it to work on ie5 mac!
if anybody comes up with a solution i would be happy to hear it!

hans!

Div vertical sizing

Why not scale the DIV height to match the line height used in the text? It seems like this would create better flow control - but would setting div height to match the text create more problems than it solves? There's another benefit not mentioned in the article: only 1 file is loaded rather than N slices. Same amount of data, less overhead to the server and fewer requests to the client (not that I worry about the client overhead). Sam

Use SPAN instead of DIV?

I recommend replacing the DIVs with empty SPANs (no nbsp should be needed). For compatibilty you may want to add 'display: block' to .float (I don't have time to test comprehensively...it works in Mozilla). If SPAN is used instead of DIV, browsers that don't understand CSS will render nothing instead of 20+ blank lines. Again, I haven't tested this in anything other than Mozilla/Win, but it should work.

Cheers ~ K

css efficiency and span vs div

divs (or images) may be easier to use in Dreamweaver, because I think DW allows you to resize them with the mouse. That way you can line them up visually with the background image. However, I agree that using spans is better for backwards compatibility.

On a nitpicky note, you can reduce the amount of code if you use something like this:

#toycycle {
      background-image: url(toycycle.jpg);
}
div.flowing {   /* reusable class for other images on your site */
      background-repeat: no-repeat
      background-attachment: scroll;
      margin: 0px;
      padding: 0px;
} 
div.flowing img {  /* or div.flowing div */
      border: 0;
      float: left; 
      clear: left; 
      margin: 0px 2em 0px 0px;
 }

<div id="toycycle" class="flowing">

Then you don't need to repeat class="flowing" and border="0" for every img.

Other browsers

I hadn't thought about how easy DW would make sizing the div's. Maybe there actually is a use for WYSIWYG editors! Also, good suggestions on the CSS.

Has anyone come up with something to get this to work on IE5/Mac?

Cheers ~ K

good

it is so useful for me

good

it is so useful for me

Brilliant! Simply Brilliant!

Rarely do I find tips as useful as this one. This will bring web design very close to the look of print design. We are closing the gap! Thanks for the tip!

I don't like it

Too many small images. That is overkill. I think it would be possible to use one image and 'slice it' using background-position.