Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Super Ragged Floats

Rated 3.82 (Ratings: 20) (Add your rating)

Log in to add a comment
(19 comments so far)

Want more?

 
Picture of nileshch

Nilesh Chaudhari

Member info | Full bio

User since: December 09, 2002

Last login: December 09, 2002

Articles written: 1

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!

Nilesh currently works for Paladion Networks as a Assoc. Security Consultant. In his spare time, Nilesh does photography and maintains his weblog at nilesh.org/weblog. Additionally, he is excited about emerging web technologies and constantly experiments with them in spare time.

w00t! w00t! first post!

Submitted by glaven on December 18, 2002 - 00:31.

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.

login or register to post comments

ALT tags

Submitted by luomat on December 18, 2002 - 07:30.

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.

login or register to post comments

Re: ALT Tags

Submitted by nileshch on December 18, 2002 - 17:15.

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

login or register to post comments

Attribute, not tag

Submitted by hyphen on December 19, 2002 - 00:19.

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.

login or register to post comments

Re: Attribute, not tag

Submitted by asjo on December 19, 2002 - 16:14.

> 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

login or register to post comments

great work ...

Submitted by hfraser on December 19, 2002 - 16:23.

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

login or register to post comments

Attribute, not tag

Submitted by hyphen on December 19, 2002 - 19:08.

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...

login or register to post comments

Oh please make it stop

Submitted by luomat on December 19, 2002 - 19:15.

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

login or register to post comments

Re: great work

Submitted by nileshch on December 19, 2002 - 21:30.

(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?

login or register to post comments

Why not spans?

Submitted by unapersson on December 20, 2002 - 09:45.

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.

login or register to post comments

ie 5.X mac???

Submitted by hfraser on December 20, 2002 - 15:55.

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!

login or register to post comments

Div vertical sizing

Submitted by SamC on December 29, 2002 - 10:12.

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

login or register to post comments

Use SPAN instead of DIV?

Submitted by dahlbyk on January 15, 2003 - 18:32.

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

login or register to post comments

css efficiency and span vs div

Submitted by GetSomeSleep on January 16, 2003 - 21:58.

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.

login or register to post comments

Other browsers

Submitted by dahlbyk on January 16, 2003 - 22:33.

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

login or register to post comments

good

Submitted by loveinglulu on January 17, 2003 - 02:53.

it is so useful for me

login or register to post comments

good

Submitted by loveinglulu on January 17, 2003 - 02:54.

it is so useful for me

login or register to post comments

Brilliant! Simply Brilliant!

Submitted by design7 on July 18, 2003 - 00:48.

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!

login or register to post comments

I don't like it

Submitted by porneL on February 18, 2005 - 02:06.

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

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.