Super Ragged Floats
First
there was slantastic
and then curvelicious.
Then came ragged
floats and then finally here is Super Ragged Floats, advanced
CSS for your viewing pleasure. If you have used Eric Meyer's method
to wrap around images, you will like this too. Here is a simple
tutorial on how to use CSS for wrapping text, but not by slicing.
An alternative to Eric Meyer's ragged floats. And a live example
can be seen here. »
As everyone knows, Eric Meyer had written a beautiful tutorial on wrapping text around images using pure CSS and a few images. He achieved this by slicing the image into thin horizontal pieces and placing them one below the other. e.g. in my case the slices images would be :
Then place these images (without borders) on the left of the text by assigning them this div:
img.flowing {
float: left;
clear: left;
margin: 0 2em 0 0;
}
That's it! Pretty simple. But what if I do not want to slice my image into a thousand pieces? What if I want my image to load in its entirety and not in bits and pieces? Well, that's exactly what Super-Ragged Floats is all about. We'll see how to wrap text around images, an improvement on Eric Meyer's method.
Note that this method isn't more simpler than Eric's method. In fact, initially, you'll need lot of patience to align the image correctly. Given the effort required, there are many advantages of using this method. First, if you feel that you need finer control on the wrapping of text, it is as easy as find and replace in the code. You don't need to re-slice the image with different heights. Second, partial loading of an image wont look as bad as in the sliced version, if you use progressive/interlaced images. Unfortunately, as with the earlier method, browser support is limited to 6.x and greater versions.
To explain in plain words, we create a <div> with the background being the image to be wrapped. Then using multiple blank 1x1 gifs/pngs, we wrap the text around the image in background. This gives us flexibility in deciding the heights and widths of the transparent images we are using. You can vary the heights and widths for finer wrapping of text. In what I have done above, I have used constant heights (20px), and so a bit of unevenness can be seen on the wrapping edge.
Now down to the exact code. I used the following id definition for the div:
#toycycle{
background-image: url(toycycle.jpg);
background-repeat: no-repeat
background-attachment: scroll;
}
or in short,
#toycycle{
background: url(toycycle.jpg) no-repeat scroll ;
}
This tells the browser to put toycycle.jpg as the background image for the div `toycycle`. 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.
And the image class is
img.flowing {
float: left;
clear: left;
margin: 0 2em 0 0;
}
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. Now, you use the id and class in this way:
<div id="toycycle">
<img src="0.gif" alt="" class="flowing"
border="0" width="105" height="20" />
<img src="0.gif" alt="" class="flowing"
border="0" width="110" height="20" />
<img src="0.gif" alt="" class="flowing"
border="0" width="115" height="20" />
<img src="0.gif" alt="" class="flowing"
border="0" width="110" height="20" />
<img src="0.gif" alt="" class="flowing"
border="0" width="98" height="20" />
...
<img src="0.gif" alt="" class="flowing"
border="0" width="220" height="20" />
<p>Note that this method isn't more simpler than
Eric's method. In fact, initially, you'll need lot
of patience to align the image correctly. Given
...</p>
Place the transparent images before the text and place them only as much required. For example, assuming the background image has a height of 400pixels, if you take each image 20pixels high, you'll need about 20 transparent images stacked up to cover up the whole image. Adjust the width of each image accordingly. You can experiment with this to achieve smooth text flow over the image. See a live example on my About page.
I haven't tested this on browsers other than Mozilla 1.0+, IE6.0 and Opera 7.01. It would be nice if someone tells how this looks on other versions of browsers. Though I think, there wouldn't be a problem with 5.x browsers, given their support for background attributes and ids



10 comments have been added. Add your comments.
1. codey said...
much simpler way.... no css nothing.... pad with a bit of colour of the page on the left of the image and right align it... justify the text.... it creates the illusion of a pseudo run around ;-)
2. Nilesh said...
Nah! That won't give you even a pseudo ragged wrap. It would be like this small image on the post - a straight wrap.
3. Arnab Nandi said...
Since none of us like spacer GIFs, how about substituting the 0.gif with:
<div class="flowing" style="height:XX;width=YY"></div>
[note I've not used <div /> since this is a block level element]
4. Nilesh said...
Arnab, do we think alike? Sometime after I posted that, I was pretty sure divs would do the trick too. And they really do!
5. Nilesh said...
Use divs like this:
.flowing {
clear:left;
float: left;
margin: 0 2em 0 0;
height: 15px;
}
and
<div class="flowing" style="width: YYpx;" > </div>
6. Nilesh said...
Another advantage of divs that I observed - The page feels lighter for rendering and the browser does it fast, not the case with spacers. Although the difference isn't much to notice easily.
7. Mahesh Shantaram said...
Oops. Effectively, you're asking me to decide for or against between using spacer GIFs. I was hoping we left spacers behind in 1995. But yes, you make a valid point: your method is for those who don't know or don't want to split images.
8. Nilesh said...
Mahesh, see the refined version of this method.
9. Nilesh said...
BTW, Mahesh, why is your site down for such a long time? Even your LJ page doesn't come up.
10. Reference from kadyellebee
Read more in ragged floats »
Your Comments
* Please do not put off-topic comments. We reserve right to delete them at our discretion. You can post anonymously. If you are unable to see your posted comment immediately, it may have been queued for moderation. So do not submit it again. HTML formatting is allowed (only a, b, i, br, p, strong, em, ul, li & blockquote are allowed). Do not put paragraph tags. They are automatically inserted.