November 13 2005

Image floats, without the text wrap!

How many times do you have an image floated left in a block of content, but want to keep that content from wrapping around your image?

You’ve tried to accomplish this before, but what you really need is something that plays nicely with any HTML & any amount of content you, or more realistically, your database can throw at it.

The example below can easily fall apart without the proper CSS in place:

The HTML:

<div class=”callout”>
<h3>team report, replaced image</h3>
<h2>Team Report 10-06-05</h2>
<p>Eric Koston and Rick McCrank are still in Toronto, having a killer time filming for the upcoming éS video with filmer Scuba Steve.</p>
</div>

And the CSS to get started:


.callout {
width:275px;
}

.callout h3 {
width:115px;
height:65px;
float:left;
text-indent:-8008px;
background:transparent url(team-report.gif) no-repeat 0 0;
}

The important part is that you have your <h3> (in this case, an image replacement) floated left. Any html that comes after your <h3> will now wrap around it. Take a paragraph for example:

That’s the easy part. Now, how do we keep that paragraph from wrapping? Your first instinct might be to increase the height of .callout h3, we’ve all tried to get away with that method in the past. You’ll tell yourself - the content will never be more than five lines long. Deep down inside - you know this isn’t the case & that you’ll be editing that bit of CSS in the near future. And then a month after that; and then the following month and then… well you’ll be editing that css until the entire site gets redesigned; which you know isn’t happening anytime soon.

So, we need a future proof method to keep this content tight, the way your pixel princess intended it to be.

One solution might be to put a width on the paragraph tag and float it right. Let’s try:

.callout p {
width:160px;
float:right;
}

Resulting in:

Not a bad start. With this in place, any content in a paragraph tag will never wrap under your image. Unfortunately, chances are this content is variable & is being pulled from a database. Who knows what HTML will be spit out. It’ll fall apart if say, instead of a paragraph, you had an unordered list:

This doesn’t quite hold up very well, does it? So let’s change the .callout p { } declaration to .callout * { } instead. The * selector will select P’s, UL’s & any HTML that falls under .callout :

.callout * {
width:160px;
float:right;
}

Our new declaration solves that quite nicely indeed. There’s just one problem. What if some HTML appears deeper? Take these potential list items for example:

Some basic HTML, will render as this:

This really screws things up. Cleary we only want .callout * { } to apply to parent elements, and not their children, no matter what they are. So let’s overwrite any child’s width & float values with:

.callout * * {
width:auto;
float:none;
}

Quite literally, the .callout * * { } declaration says: if any html element contains any HTML element, do { width:auto;float:none;}.

There it is, exactly what we want & it’ll work in all current browsers (including IE!). Throw any HTML at it, with any varying amount of content & you’ve got some lean & mean & pretty basic CSS to prevent it from wrapping under your floated image.

Your final css:

.callout {
float:left;
width:275px;
}


.callout h3 {
width:115px;
height:65px;
float:left;
text-indent:-8008px;
background:transparent url(team-report.gif) no-repeat 0 0;
}


.callout * {
width:160px;
float:right;
}


.callout * * {
width:auto;
float:none;
}

You can view the source of float, no text wrap or see it in use on éSfootwear.com.

Did you find this post helpful?

interweb, tech

68 Comments

joe d // November 13 2005

Nice writeup. I take back all the mean things I say about u.

haveboard // November 13 2005

good stuff

Ryan Merket // November 13 2005

Great tutorial, thanks for sharing!

bjoern // November 14 2005

I dont understand that in your markup. For what reason did you take a headline element which will be replaced via CSS?

This messes up your semantic code (h3 before h2, without any meaningful content) and the same result could also be accomplished by adding this background-stuff to the .callout-, some padding-left and all that children-tweaking css would be obsolete.

Your article headline does not clearly reflect your articles meaning. For me, your markup is not really an image float with text-wrap.

Cristian // November 14 2005

very well written article. useful stuff.

thanks a lot

trovster // November 14 2005

I’ve used the star star effect before because it works in IE. But :first-child is so much better.

You could then use *:first-child. However, to avoid that problem of anchors and such I’d keep it more specific with…

.callout ul, .callout ol, .callout p, .callout div {}

kubox // November 14 2005

This has bugged me forever - cheers!

Michael Zipser // November 14 2005

Yes, this is really a good way to style images and text. I can use it in my new project. Thank you!

Derek A // November 14 2005

You could also give the other items a left margin (equal to the header image width)

e.g.,

.callout h2, .callout p, .callout ul
{
margin-left:115px;
}

pup // November 14 2005

Nice work. I came up with another technique that accomplishes something similar about a year ago.

http://www.pupinc.com/files/test/float.html

it’s currently in use in the shacknews.com comments (the comment blocks flow around the ads).

manuel // November 14 2005

please check IE

sosa // November 14 2005

If you don’t mind reading spanish, I have acomplished a similar effect with my “Vaccum Injection Technique”, maybe you want to check it up.

the text:
http://www.nolimit-studio.com/yosoysosa/archives/categorias/tutoriales/inyeccion_de_vacio.php

the examples:
http://nolimit-studio.com/yosoysosa/tutorials/samples/vacio.html

http://nolimit-studio.com/yosoysosa/tutorials/samples/vacio2.html

Take My Advice ... // November 14 2005

[trackback] look at some of the sites for inspiration. And of course a few interesting CSS Articles: Image floats, without the text wrap! - a technique for using floats and ke…

Bill // November 14 2005

Manuel, do you see an error in IE? Looks fine in IE mac & pc(v.6) from here.

Jeff // November 14 2005

Nice, but what if your output contains an ?

Jeff // November 14 2005

let’s try that again — nice, but what if your output contains an h3?

Bill // November 14 2005

Very true Jeff - Perhaps it’s a better idea to put a class on the H3, instead of the .calllout h3, good eye!

Mani Sheriar // November 15 2005

Thanks for this - it’s thourough and useful. I like how you thought out all the possibilities. Good stuff!

Pelle // November 15 2005

I am really wondering why someone would choose this method over giving the right text a left margin? It seems superior because then you only have to specify the margin to be the same as the width - no advanced substraction!!

Dustin // November 16 2005

This is actually more complicated than it has to be.
All you need is one two paragraphs. one with the image, the other with the content. float the image left, and give the content a left margin… Yea, what Pelle said above…that’s the same thing I’m saying.

In essense, it would look like this:

p.img { float:left;width:100px; }
p.content { margin-left:110px; }

piece of cake.

Jerome Dahdah // November 16 2005

Very interesting method. I’ve never considered using * * until now, so thanks for the inspiration.

One thing that bothers me with this method, however:

.callout * {
width:160px;
float:right;
}

The fixed widths suggest that all images will always have the same width, otherwise the method would break. While it’s good to specify general image sizes in a web styleguide, it’s not always going to work, e.g. when you have a client using a CMS, unless you have built in some failsafe such as server-side image manipulation or hard-coded widths on your image-tags. And besides, it’s not always desirable. I’d like more flexibility.

Is there any way to get this method working completely without specifying any widths? Because currently, as others have suggested above, the method with specifying a left margin and positioning the image/h3 absolutely (or by giving it a negative left-margin, and several other methods), or specifying a left padding and placing the image as a background image, is simpler, and it does the same job.

cpawl // November 16 2005

In essense, it would look like this:

p.img { float:left;width:100px; }
p.content { margin-left:110px; }

piece of cake.

Dustin

Exactly. I don’t understand what the big discovery is here. It seems that this method is a step backwards rather than forward. There seems to be quite a few methods that can achieve the same job.

It would be nice for someone to test various methods… this one seems like an overkill… but what the hell do I know.

Bill // November 17 2005

The method I’ve outlined is merely an attempt to cover as many variations of html as possible, using as few css declarations as possible. I found it to be the best solution to the problems we were facing.

Indeed, if what you’re trying to accomplish can be achieved with Dustin’s css, than there’d be no need for something like this.

marko Petkovic // November 18 2005

Smart solution. I like that way of thinking.

John // November 23 2005

I agree with cpawl
here is another method that works just fine.

p.img{
float: left;
width: 124px;
margin: 10px 10px 10000px 10px;
}

dave // December 13 2005

The simplified methods (as per Dustin’s suggestion) work fine when you only have one image and a paragraph of text…
… but the second paragraph obviously can’t contain other markup such as lists or headings, so in those instances where a whole mess of html content needs to sit alongside the image (lists, headings, paragraphs etc - or even a form), Bill’s method is pretty lean in that you don’t have to nest that content in another container.
Each to their own depending on the requirements of the situation, I guess…

Ari // August 27 2006

you’re all speaking greek? How’s a novice to learn anything? Leave off the jargon…we already know you’re smart. :)
P.S: I appreciated the problem solving…very interesting methods

53 CSS-Techniques You Couldn’t Live Without | Smashing Magazine // January 18 2007

[…] 38. Image Floats without the Text Wrap […]

潘 -Idea of Life » 沒有就活不下去的53個CSS技巧 // January 20 2007

[…] 38. Image Floats without the Text Wrap […]

GoofyDawg // January 24 2007

This seems like a lot of work that you could do with a single CSS definition:

.fltBox {
width:350px;
background:url(floater.gif) 5px 9px no-repeat;
background-color:#FFF;
padding:6px;
padding-left:91px;
border:1px solid black;
}

BTW, floater.gif is an 80px X 50px image.

To use, just do this:

<b>Non-wrapping text!</b>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis aliquam bibendum urna. In fringilla, enim quis dictum rutrum, nisl nisi </p>
</div>

No negative margins, no extra classes, and yes, it’ll work on any HTML you throw at it. Plus it’s semantic.

GoofyDawg // January 24 2007

Oops… missed my opening div tag:

<div class=”fltBox”>

Bill // January 26 2007

Goofydog - You’re right, that method would work fine . The only thing you’d be missing out on is getting your image in there as an H3, which likely, isn’t a big deal.

You are wrong however in thinking that an H2 can’t come directly after an H3. It doesn’t matter what order they appear in. My H2 is more important than my H3, no matter where they sit in my HTML, therefore you’d mark them up as such.

lillbra » Blog Archive » links for 2007-01-28 // January 28 2007

[…] Image floats, without the text wrap! - ghettocooler.net Floata en bild utan att texten lägger sig under den. (tags: css webdesign tutorial) […]

Zhu Zhongwei » Blog Archive » CSS使用技巧[转载] // February 16 2007

[…] 38. Image Floats without the Text Wrap […]

Blog » Blog Archive » For Webmaster only // February 23 2007

[…] 38. Image Floats without the Text Wrap […]

CodeBit.cn // April 23 2007

good !
thanks !

53 ultramodern css techniques | A Blog you can use // April 23 2007

[…] 38. Image Floats without the Text Wrap […]

anoop // April 27 2007

NIce write up. But what happens when there are a lot of question marks. I find that wrapping do not work in this case ???? ????? ??? ???? ??? ??? ???? ?? ??? ???? ???? ?????

CSS Float Theory: Things You Should Know | Smashing Magazine // May 1 2007

[…] Image floats, without the text wrap! […]

Vent Swap // May 1 2007

This’s what’s called the magic of the CSS!
Thanks a lot.

Dave // May 4 2007

It seems that some of the “just do it with two <p> tags” type of comments wouldn’t work for me &em; I prefer to code the XHTML as purely semantic as possible, and then code the CSS with no or very very little change to the XHTML. To me, adding extra tags and paragraphs and divs is not really a quality solution.

Therefore, I really like the *, ** method here. It’s all CSS with no change required to the XHTML.

» 25 Code Snippets for Web Designers (Part4) // May 4 2007

[…] Image Floats with the text wrap - How many times do you have an image floated left in a block of content, but want to keep that content from wrapping around your image? […]

Paul // May 4 2007

You can do this without widths or dimensions being applied. Just use overflow:auto on the content container that sits next to a floated image and that’s it.

You need the height:1% hack for ie6 and under but apart from that it works most everywhere that overflow is not an issue for you.

http://www.pmob.co.uk/temp/image-overflow2.htm

The images can come for a database and be any size you like within reason. Of course if the content container can’t be squashed anymore then a scrollbar will appear, but in a fixed width container this won’t be an issue.

Just thought I’d share in passing :)

Shao69 // May 4 2007

Good Job

it’s really nice and easier. Thanks a lot from Lyon (France)

Web设计者的25个CSS代码片段 // May 5 2007

[…] Image Floats with the text wrap - How many times do you have an image floated left in a block of content, but want to keep that content from wrapping around your image? […]

tobto // May 7 2007

thanks! css has vast possibilities

CalinSoft » Blog Archive » 53 Tecnicas ultramodernas con hojas de estilo (CSS) // May 9 2007

[…] Imagenes flotantes sin el ajuste de texto […]

Web-Design Blog » Blog Archive » CSS Float Theory: Things You Should Know // May 10 2007

[…] Image floats, without the text wrap! How many times do you have an image floated left in a block of content, but want to keep that content from wrapping around your image? […]

Web Design Glasgow // May 14 2007

Very useful, particularly the using * *{ - simple, but I hadn’t thought of it. Thanks from Glasgow.

CalinSoft — 53 Tecnicas ultramodernas con hojas de estilo (CSS) // July 23 2007

[…] Imagenes flotantes sin el ajuste de texto […]

bilgiservisim » 53 mükemmel css örneği // September 3 2007

[…] 38. Resmin yerini CSS ile ayarla […]

Richard // September 15 2007

Take care if your text area is less than 1/2 of the total container width. Once the elements slip under the image, they will float up against each other!

Design For Masters » CSS Float в теории и на практике // September 15 2007

[…] Плавающие изображения без обтекания текстом […]

53 CSS-Techniques at NeoStyle // September 27 2007

[…] 38. Image Floats without the Text Wrap […]

» Essential Web Design Bookmarks SharpProgrammer.Com // October 7 2007

[…] Image floats, without the text wrap! - ghettocooler.net […]

UizL » Blog Archive » 53 Tecnicas CSS // October 20 2007

[…] Imagenes flotantes sin el ajuste de texto […]

53个CSS-不可或缺的技巧 - ZeroZ // December 28 2007

[…] 38. Image Floats without the Text Wrap //没有被正文包围的浮动图片 […]

101 CSS Techniques Of All Time- Part 1 // January 13 2008

[…] CSS Image Text Wrap […]

440design | 使えるCSSテクニック101選 part1 // January 16 2008

[…] Image floats, without the text wrap! […]

Wayne // February 5 2008

I ended up using goofydog’s recommendation for my purpose. I wanted to create a “talkback” section in posts where I encourage people to comment. The idea was I wanted to pose my question to the reader but wrap the question in an image where it says “This is where YOU join in” and a graphic on the left for consistency. None of the solutions worked right for me, so I used the one goofydog recommended, and then added a “p” inside of that with a padding-top.

I’m sure there’s a better way to do it, but that’s what worked for me. Now if I could just get tinymce to stop messing with my tags :)

Auris // February 20 2008

I also used goofydog’s recommendation which works great for 1 box & 1 image. However for multiple boxes I need to have a different image in each box.

Any ideas on how I can achieve this with just the one piece of CSS code?

CSS Float Theory: Things You Should Know // April 21 2008

[…] Image floats, without the text wrap! How many times do you have an image floated left in a block of content, but want to keep that content from wrapping around your image? […]

Recursos y Tutoriales » Blog Archive » 53 tecnicas CSS muy bueno // May 6 2008

[…] 38. Image Floats without the Text Wrap […]

Leave a Comment