<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[CSS Discuss - Image Floats, Without the Text Wrap]]></title>
	<link rel="self" href="http://cssdiscuss.com/feed/atom/topic/3/"/>
	<updated>2010-02-01T10:15:49Z</updated>
	<generator version="1.3.4">PunBB</generator>
	<id>http://cssdiscuss.com/topic/3/image-floats-without-the-text-wrap/</id>
		<entry>
			<title type="html"><![CDATA[Image Floats, Without the Text Wrap]]></title>
			<link rel="alternate" href="http://cssdiscuss.com/post/3/#p3"/>
			<content type="html"><![CDATA[<p>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?&nbsp; </p><p>You&#039;ve tried to accomplish this before, but what you really need is something that plays nicely with any HTML &amp;amp; any amount of content you, or more realistically, your database can throw at it.&nbsp; </p><p>The example below can easily fall apart without the proper CSS in place:<br /><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/example.gif" alt="http://ghettocooler.net/img/floats-no-wrap/example.gif" /></span></p><p>The HTML:</p><div class="codebox"><pre><code>&lt;div class=&quot;callout&quot;&gt;
&lt;h3&gt;team report, replaced image&lt;/h3&gt;
&lt;h2&gt;Team Report 10-06-05&lt;/h2&gt;
&lt;p&gt;Eric Koston and Rick McCrank are still in Toronto, having a killer time filming for the upcoming éS video with filmer Scuba Steve.&lt;/p&gt;
&lt;/div&gt;</code></pre></div><br /><p>And the CSS to get started:</p><div class="codebox"><pre><code>.callout {width: 275px;}
.callout h3 {
  width:115px;
  height:65px;
  float:left;
  text-indent:-8008px;
  background:transparent url(team-report.gif) no-repeat 0 0;
  }</code></pre></div><p>The important part is that you have your &lt;h3&gt; (in this case, an image replacement) floated left.&nbsp; Any html that comes after your &lt;h3&gt; will now wrap around it.&nbsp; Take a paragraph for example:</p><p><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/step-one.gif" alt="http://ghettocooler.net/img/floats-no-wrap/step-one.gif" /></span></p><p>That&#039;s the easy part.&nbsp; Now, how do we keep that paragraph from wrapping?&nbsp; Your first instinct might be to increase the height of .callout h3, we&#039;ve all tried to get away with that method in the past.&nbsp; You&#039;ll tell yourself - the content will never be <em>more</em> than five lines long.&nbsp; Deep down inside - you know this isn&#039;t the case &amp;amp; that you&#039;ll be editing that bit of CSS in the near future.&nbsp; And then a month after that; and then the following month and then... well you&#039;ll be editing that css until the entire site gets redesigned; which you know isn&#039;t happening anytime soon.</p><p>So, we need a future proof method to keep this content tight, the way your pixel princess intended it to be.</p><p>One solution might be to put a width on the paragraph tag and float it right.&nbsp; Let&#039;s try:</p><div class="codebox"><pre><code>.callout p {
  width:160px;
  float:right;
  }</code></pre></div><p>Resulting in:</p><p><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/step-two.gif" alt="http://ghettocooler.net/img/floats-no-wrap/step-two.gif" /></span></p><p>Not a bad start.&nbsp; With this in place, any content in a paragraph tag will never wrap under your image.&nbsp; Unfortunately, chances are this content is variable &amp;amp; is being pulled from a database.&nbsp; Who knows what HTML will be spit out.&nbsp; It&#039;ll fall apart if say, instead of a paragraph, you had an unordered list:</p><p><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/step-three.gif" alt="http://ghettocooler.net/img/floats-no-wrap/step-three.gif" /></span></p><p>This doesn&#039;t quite hold up very well, does it?&nbsp; So let&#039;s change the .callout p { } declaration to .callout * { } instead.&nbsp; The * selector will select P&#039;s, UL&#039;s &amp;amp; any HTML that falls under .callout :</p><div class="codebox"><pre><code>.callout * {
  width:160px;
  float:right;
  }</code></pre></div><p><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/step-four.gif" alt="http://ghettocooler.net/img/floats-no-wrap/step-four.gif" /></span></p><p>Our new declaration solves that quite nicely indeed.&nbsp; There&#039;s just one problem. What if some HTML appears deeper?&nbsp; Take these potential list items for example:</p><ul><li><p>An <a href="http://#">anchor tag</a> perhaps?</p></li><li><p>Or a <strong>strong tag</strong> perhaps?</p></li></ul><p>Some basic HTML, will render as this:</p><p><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/step-five.gif" alt="http://ghettocooler.net/img/floats-no-wrap/step-five.gif" /></span></p><p>This really screws things up. Cleary we only want .callout * { } to apply to parent elements, and not their children, no matter what they are.&nbsp; So let&#039;s overwrite any child&#039;s width &amp; float values with:</p><div class="codebox"><pre><code>.callout * * {
  width:auto;
  float:none;
  }</code></pre></div><p><span class="postimg"><img src="http://ghettocooler.net/img/floats-no-wrap/step-six.gif" alt="http://ghettocooler.net/img/floats-no-wrap/step-six.gif" /></span></p><p>Quite literally, the .callout * * { } declaration says: if <em>any</em> html element contains <em>any</em> HTML element, do { width:auto;float:none;}.</p><p>There it is, exactly what we want &amp; it&#039;ll work in all current browsers (including IE!).&nbsp; Throw any HTML at it, with any varying amount of content &amp;amp; you&#039;ve got some lean &amp; mean &amp; pretty basic CSS to prevent it from wrapping under your floated image.</p><p>Your final css:</p><div class="codebox"><pre><code>.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;
  }</code></pre></div><p>View the final HTML: <a href="http://ghettocooler.net/img/floats-no-wrap/">CSS Image floats, no text wrap</a>.</p><p><strong>Did you find this post helpful? Discuss it below. Guest posting is currently enabled.</strong></p>]]></content>
			<author>
				<name><![CDATA[bkeller]]></name>
				<uri>http://cssdiscuss.com/user/2/</uri>
			</author>
			<updated>2010-02-01T10:15:49Z</updated>
			<id>http://cssdiscuss.com/post/3/#p3</id>
		</entry>
</feed>
