tags:

views:

172

answers:

2

I'd like all my content to flow around an image. To do this, I simply did

img#me {
    width: 300px;
    float: left;
    margin-right: 30px;
}

This works for text wraping, but other elements go behind it. For example

<style>
h2 {
   background: black;
   color: white;
}
</style>
<img id="me" src="http://paultarjan.com/paul.jpg" />
<h2>Things!</h2>

Then the h2 background flows right past the 30px margin. How should I do this instead?

Live example: http://paulisageek.com/tmp/css-float.html

A: 

I'm not sure I understand the problem, but I'm pretty sure it comes from the h2 being a block element. If it works for you, the easiest cure would be making it display: inline. Otherwise, give the h2 a specific width, and a float: left, as well.

Pekka
If I make them inline, then they don't extend all the way to the right. I just want the HTML to do as it normally would, except to have a 330px empty area to the left
Paul Tarjan
@Paul I see! Hmm, that's tricky, seeing as this needs to be dynamic, right? It should only behave that way if it's next to your image but not if it's underneath, correct?
Pekka
Exactly. If it is under the image, I want it to have the full width. I didn't expect this to be hard! ;)
Paul Tarjan
@Paul let us know whether Les' answer sorts it! I'm interested to know the outcome.
Pekka
Yup, overflow fixed it! That sounds like it should be the default behavior, but CSS is a weird beast. http://paultarjan.com/
Paul Tarjan
+1  A: 

I wish I could explain why exactly, but

h2 {
   ...
   overflow: hidden;
   ...
}

should fix your problem.

Les