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.
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?
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;"> </div> <div class="flowing" style="width:110px; height:20px;"> </div> <div class="flowing" style="width:115px; height:20px;"> </div> <div class="flowing" style="width:110px; height:20px;"> </div> <div class="flowing" style="width:98px; height:20px;" > </div> ... <div class="flowing" style="width:220px; height:20px;"> </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.
Happy wrapping!
Comments
w00t! w00t! first post!
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
Re: ALT Tags
Attribute, not tag
Re: Attribute, not tag
> "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 does in all my other browsers!
hans-frederic!
Attribute, not tag
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
Re: great work
Why not spans?
ie 5.X mac???
if anybody comes up with a solution i would be happy to hear it!
hans!
Div vertical sizing
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
good
Brilliant! Simply Brilliant!
I don't like it