Often as a Web Producer I get a lovely mock-up with text wrapping around an obvious background image. The content is going to e from a backend CMS and I don't want the editorial staff to worry about inserting <br />
tags etc.
Lorem ipsum dolor sit amet, consectetuer adiping elit, xxxxxxxxxxxxxx
sed diam nonummy nibh euismod tincidunt ut lao xxxxxxxxxxxxxxxxxx
dolore magna aliquam erat volutpat. Ut wisi xxxxxxxxxxxxxxxxxxxxxxxxx
enim ad minim veniam, quis nostrud exerci xxxxxxxxxxxxxxxxxxxxxxxxxxx
tation ullamcorper suscipit lobortis xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ut aliquip ex ea modo consequat. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Duis autem vel eum iriure dolor in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hendrerit in vulputate velit esse xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
molestie consequat, vel illum xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Does anyone know of a solution to make this kind of thing automated via script, css?
Often as a Web Producer I get a lovely mock-up with text wrapping around an obvious background image. The content is going to e from a backend CMS and I don't want the editorial staff to worry about inserting <br />
tags etc.
Lorem ipsum dolor sit amet, consectetuer adiping elit, xxxxxxxxxxxxxx
sed diam nonummy nibh euismod tincidunt ut lao xxxxxxxxxxxxxxxxxx
dolore magna aliquam erat volutpat. Ut wisi xxxxxxxxxxxxxxxxxxxxxxxxx
enim ad minim veniam, quis nostrud exerci xxxxxxxxxxxxxxxxxxxxxxxxxxx
tation ullamcorper suscipit lobortis xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ut aliquip ex ea modo consequat. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Duis autem vel eum iriure dolor in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hendrerit in vulputate velit esse xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
molestie consequat, vel illum xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Does anyone know of a solution to make this kind of thing automated via script, css?
Share edited Nov 4, 2009 at 21:29 John Rudy 37.9k14 gold badges66 silver badges101 bronze badges asked Nov 4, 2009 at 21:26 Steve PerksSteve Perks 5,5304 gold badges30 silver badges41 bronze badges 1- I should add, that the answer I've always used, and I'm quite happy to continue using unless informed otherwise here, is "There's not an easy way to wrap around irregular images, so you either need to change your design or accept that the editors are going to be blaming you rather than me for this". – Steve Perks Commented Nov 4, 2009 at 21:41
4 Answers
Reset to default 6A List Apart did an article on this a while back. It's not totally turn-key, but they show you how to do it in PHP without too much trouble.
If you absolutely had to do this browser-side and you wanted it to be as close to 'pixel perfect' as possible (in terms of specifying things like an exact margin around the image's shape), you'll probably need to use the <canvas>
feature of HTML 5. With it, you would then need to do some "basic" image processing:
this example is for an image that is to the right of the text, like your example
- Run an edge-detection algorithm on the image (probably the Sobel algorithm, because of it's simplicity and speed) to find the borders. Alternatively, if you can guarantee that the image will have a solid background, you could opt to simply threshold the image against that background color to obtain a segmentation of the image's subject from the background.
- Given your
line-height
, iterate through the rows of the image to find the max distance from the image's border to the start of the image's subject (the part we segmented) for each line of text that will border the image.- Take that maximum value and subtract it from the total width of the image to obtain the amount of distance from the right side of the image.
- Push this width onto an array
arr
.
- At the start of the DOM node that contains your text, add
arr.length
<div>
nodes, each of which would havestyle="height: [your text's line-height]; width: [value of arr at current element]; margin-left: [desired margin]; float: right; clear: right"
Edit
I wrote up a jQuery plugin for this technique, you can find it on plugins.jquery. or at it's home page at http://jwf.us/projects/jQSlickWrap
Ouch, difficult one. Assuming your image is transparent at the left side, the closest thing I can think of is a script that finds the transparent pixels, and -- to a certain level of detail -- generates a set of floating divs that overlay the image and keep the text out of the way.
Are you talking about an arbitrary background image, or a fixed one? If you want to do it with any old background then you're looking at doing some serious pixel scanning, which will almost certainly have to happen at the server side and will probably not be worth your time. You're better off just baking the text into the image.
If you only want to do it for a single image your options get better, but still aren't pretty. The first thing that es to mind is using a a monospace font, measuring how many chars can fit at each line, and then having a js function insert breaks at the appropriate string positions based on that.