Kevin A. Freitas |
E: freitaka@blarg.net P: 253.229.5093 W: kevinfreitas.net/pro |
| Print ProjectsResumeRequest InfoArticlesExtensionsClient Area | |
Fancy text wrapping around an imageBy Kevin A. Freitas Once upon a time, I came across Eric Meyers' Curvelicions technique for achieving fancy text wrapping around an image element. Recently, while reading comments on a post on Asa Dotzler's blog, I was enlightened to a technique proposed by Lauritz Jensen to achieve said text wrapping without slicing up an image into pieces. The only unfortunate requirement for Lauritz's technique is that it requires the image around which text will wrap to have a transparent background. Not one to shrink to limitations, I came up with my own flavor that allows irregular text wrapping without slicing image or requiring transparent image backgrounds so we can be free to use JPGs, PNGs, or GIFs. Enough introduction, let's get it on! The setup
Choose an image with a subject around which you'd like to wrap your text. I'll use a lunar eclipse image from my astrophotography gallery (shameless plug, I know). Since the moon rests on a black background I've used a radial gradient to leave some of the blackness around the image but want to seamlessly fade it so it sits well on this page. This technique can be used for as sophisticated an edge you'd like to wrap around. Also note the height of the image chosen since this will matter later when we do our virtual chop job on it. CSS (though not much)Now with an image chosen we can begin to layout the XHTML and CSS needed to make this happen. First, the CSS:
div.wrap_area { position: relative; }
div.wrap_area img { position: absolute; left: 0px; top: 0px; }
div.wrap_area p { position: relative; }
div.shape_wrap div { float: left; clear: left; height: 20px; }
/*
div.wrap_area img { opacity: .5; filter: alpha(opacity=50); }
div.shape_wrap div { border: 1px solid #f0f; }
*/
Any text inside "wrap_area" will be affected by the fancy wrapping. The "shape_wrap" elements will be used to define the blocks around which the text will wrap. Adjust the "wrap_area" <img> position and the "shape_wrap" float property depending on where you'd like this all to happen. The height of these blocks can be adjusted according to how tightly you'd like your text to wrap around an image. Note: The smaller the height of your blocks, the more XHTML will be necessary to properly wrap. The commented bits can be used for diagnostic purposes -- when you want to see your wrap shape just un-comment those lines. The XHTML is where things get a little messy:
<div class="wrap_area">
<img src="wrap-subject01.jpg" class="no_border" alt="Lunar eclipse photo" />
<div class="shape_wrap">
<div style="width: 250px;"></div>
<div style="width: 280px;"></div>
<div style="width: 305px;"></div>
.
.
.
</div>
<p>Numerous blocks of text</p>
.
.
.
</div>
Now have a look at the sample. For this I choose a wrap block height of 17px only because I was originally going with 15px but made the desired curve with borders on, thus adding one pixel to the top and one to the bottom of each block. 2 + 15 = 17 ;) IE seems to deal with the height of the blocks differently than Firefox since it's basing that height more on font size and less on the CSS-set size. You can find a happy medium or add your own CSS hacks to make IE behave. View normal | structural sample » Tested to work in...
(2004-12-23) |
|
Comments (10) | To Top
2/4/2005 @ 5:49pm
Nice work! I noticed a drawback of sorts using Firefox 1.0; when selecting the text which is wrapped around the image, I cannot seem to select more than one paragraph at a time. It's odd. Try it yourself! Hopefully this is just a temporary bug which will be ironed out of new FF versions.
Also, in IE 6.0 (XP SP2), selecting is fine - except when the mouse is between paragraphs, and IE (temporarily) selects everything below the beginning of your selection.
by Alan Hogan
2/4/2005 @ 5:59pm
Thanks, Alan! Ff selecting works if you hold down the SHIFT key. You can also click once somewhere in the text, hold SHIFT, then click somewhere else. Both those seem to allow selecting between paragraphs. Wonder if this is a reportable bug?
Too bad there's not some "legal" way to do this similar to image mapping. Edge mapping or something. You'd think there's enough demand.
by KevinFreitas
11/17/2005 @ 2:26pm
That sample looks great! While this certainly falls into the "hack" category, I don't think I would mind using it one bit. Nice.
by Scott Johnson
1/7/2006 @ 3:00pm
I'm the owner/webmmaster of CustomHookahs.com and I designed my site from scratch using tables. It looks descent and it got sales, but for the purpose of less code(smaller file size) and for Search Engine Optimization purposes I would like the div and css version instead. This will reduce teh file size and the load time for the user. It will also allow me to have the main content closer to the top of the code so the search engines can find what they are looking for sooner. I would like to know how much you would charge to make the div css version for this page(www.customhookahs.com/Bohemian-Hookahs.aspx)? Also I plan on making a few site changes and it would be convenient if we can make the change while we are making the div and css changes. If you are interested, please let me know and the price you would charge.
by Hookah
3/10/2006 @ 2:18pm
Excellent example! I was looking more for something to wrap text around rectangular or square images and modded this fairly quickly to work for both left and right-positioned images. In doing so I moved the height out of the css shape_wrap into the
by Doug Brydges
3/10/2006 @ 2:20pm
Don't know why the message got clipped... continued... into the XHTML script so that I use the same script several times in the same html file, setting each one according to each image's dimensions.
by Doug Brydges
10/25/2006 @ 10:38am
It works in Opera 9.01, too.
by Matt Alexander
2/19/2007 @ 1:20pm
Interesting way to achieve this effect. And pretty much cross browser compatible
by Keukens
2/26/2007 @ 1:18am
Wow, that's a great approach to solve this problem. I was trying around many different code pieces I found on the web, but this one was the only one which works in opera. Thanks.
by Frank
3/11/2008 @ 9:10am
hi yaaaaaaaaaaaaaaaaaaa :))))
by Sir Not-Appearing-In-This-Flim