tags:

views:

142

answers:

4

I can float an image on the left and have the text wrap around it no problem just by specifying float: left on the image. Like this:

<div id='foo'>

  <img src='bar' alt='baz' style='float: left;' />
  Lorem ipsum...

</div>

However if the image is wrapped in a div like the following i cannot achieve the same effect without declaring a fixed with on both the div#image_container and the div#text_container

<div id='image_container'>
  <img src='blah' alt='blah' />
</div>

<div id='text_container'>
  Lorem ipsum... long text
</div>

Is there a way to keep the flexibility of the first solution and avoid declaring a width and have the div#image_container float next to the div#text_container?

A: 
#text_container,
#image_container {
  display: inline;
}

should do it. Try this:

<html>
    <head>
    <style>
        #top {
            float: left;
            display: inline;
            border: 1px solid green;
        }

        #bottom {
            float: right;
            display: inline;
            border: 1px solid red;
        }
    </style>
    </head>
    <body>
    <div id="top">
        I'm the top div
    </div>
    <div id="bottom">
        I'm the bottom div
    </div>
</html>

But if the content of your div's is bigger than the width you've left for them (which it probably is) then you will struggle. You should really give it a width but the above might work for you depending on how you want to use it.

Pete Duncanson
Thanks, this does work if the text is shorter than the width (which is not in my case) :(
L. De Leo
A: 

Instead of the text container use a paragraph tag (<p></p>). It will wrap around the content plus it is more accessible and semantic.

OD Ntuk
If it's going to wrap an image, how is that semantic at all?
JoseMarmolejos
I like to wrap code in native html tags. I limit divs to template level coding hence that is why I would use a paragraph tag.
OD Ntuk
+1  A: 

Try setting overflow: hidden on the wrapper div, that should automatically set the div to the width of the image.


OK maybe I misunderstood your question. Do you just want the text to flow around the image? If so, all you should need is this CSS:

#text_container { display: inline; }
DisgruntledGoat
Like Colt .45...Works every time.
tahdhaze09
I still need to set the width on the text_container
L. De Leo
A: 

http://www.alistapart.com/articles/practicalcss/

Later down the page is what looks like a solution to your issue.

tahdhaze09