2

I have a floated div with "sidebar" text. Its parent container has text as well.
I don't want to have text below my floated "sidebar" div:

example http://img864.imageshack.us/img864/6058/screenshot2011052613084xv.png

How can I fix this?

<div id="parent">
    <div id="floated" style="float:right">Foo bar</div>
    <h2>Foo</h2>
    <p>Text!</p>
</div>
mskfisher
  • 3,291
  • 4
  • 35
  • 48
thom
  • 21
  • 2

5 Answers5

3

If it doesn’t mess up anything else, you can use overflow: hidden or overflow: auto to fix this:

<div id="parent">
    <div id="floated" style="float:right">Foo bar</div>
    <div class="next-to-float" style="overflow: hidden;">
        <h2>Foo</h2>
        <p>Text!</p>
    </div>
</div>

See http://jsfiddle.net/pauldwaite/YL5P3/

I’ve written about this more fully here, including code to make it work in IE 6: xHTML/CSS: How to make inner div get 100% width minus another div width

I still don’t really understand the reasoning behind why overflow: hidden does this, but I understand that it does follow from the CSS spec.

Community
  • 1
  • 1
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
  • And hide his text? I think that solution is *not* optimal :) – Michael Jasper May 26 '11 at 16:27
  • 1
    It doesn't hide the text. This is a good solution. I always used to do what Robert Koritnik suggest, but this is simpler. – Midas May 26 '11 at 16:33
  • Make sure you use a doctype, else this wont work. And this doesn't work in IE6, so perhaps the margin solution is better. – Midas May 26 '11 at 16:36
  • @Paul: Sorry to downvoted you. This actually works. I don't know why it works but is seems to support even flexible width floated elements which makes this solution **the best solution**. If you change your answer in some way I will be able to revoke my down-vote. And rather upvote. Is this behaviour the same over major browsers? Where does the CSS spec say that this should work as it does with floated elements. Interesting... – Robert Koritnik May 26 '11 at 16:51
  • @Michael: it would indeed hide any text that overflows the element, but I don’t think that’s likely to occur in this example. I have added `overflow: auto` as another option; I’m not entirely sure that works in IE though. – Paul D. Waite May 26 '11 at 16:57
  • 1
    @Robert: that’s alright, it is a very obscure part of CSS. It is pretty handy if you don’t know the width of the element that’s floated, but in general, I think you would, and setting a margin with an explicit width (like you suggested I think) makes the CSS a bit more self-explanatory IMHO. This works in most browsers; in IE 6 you can achieve the same behaviour with `zoom: 1` and a margin fix. I’m led to understand that this behaviour does follow from the CSS spec, but it’s quite obscure. – Paul D. Waite May 26 '11 at 16:59
  • Upvoted your other answer which actually seems to be a duplicate of this questions. – Robert Koritnik May 26 '11 at 17:00
  • @Robert: yeah, it’s one of those questions that can come up in a million different ways that don’t sound anything like each other. Describing layouts with words is a nightmare. – Paul D. Waite May 26 '11 at 17:02
  • @Paul: Thanks for editing your answer so I was able to convert my down-vote into an up-vote. Your answer should be accepted actually. I learned something completely new (also a bit confusing) as well. – Robert Koritnik May 26 '11 at 17:16
  • @Robert: no worries at all, thank you for re-voting. I still think your solution is a bit clearer (and avoids clipping any overflowing content), so given the likelihood that the floated element will have a fixed width, I’d go with yours. It’s good to know about the `overflow: hidden` option if you do need that flexibility on widths though. – Paul D. Waite May 26 '11 at 19:19
2

Set right margin on non-floated element

JSFiddle

The only requirement is that you must predefine your floated element's width. Then it can have whatever height you like and the non-floated content (when applied right margin) won't stretch under floated element.

How it works?

  1. We have floated element on the right with width = X
  2. We have usual content but set its right margin = X+s where s is some predefined space between your content and floated element so they don't touch.

And that's it.

Since you have multiple content elements (heads, paragraphs) you have to put them inside a container with this right margin setting.

<div id="parent">
    <div id="floated">Foo bar</div>
    <div id="content">
        <h2>Foo</h2>
        <p>Text!</p>
    </div>
</div>

And CSS:

#floated
{
    float: right;
    padding: 1em;
    background: #ccc;
    width: 10em;
}

#content
{
    margin-right: 13em; /* 10em width + 2 x 1em padding + 1em space */
}

Why is this solution better than setting main content width?

Because setting main content width will only work when you want to limit your document content to a fixed width (like 960 grid). But when you want your content to stretch over the whole browser window width, this solution will work regardless of browser window size.

And a small advice

Avoid using inline styles whenever possible because maintainability of your application/site will become a nightmare.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0

its easy simply add width in [P] tag see here

example

<div id="parent">
    <div id="floated" style="float:right">Foo bar</div>
    <h2>Foo</h2>
    <p style=" width: 500px; ">Text!</p>
</div>

for example your ( id="parent" ) have 800px width

and (id="floated") right-side bar have 200px width

then make your [P] 800px - 200px = 600px

so set your [P] width to 600px

---------- or

if you want some space between text and bar make [P] width 580px

it means 20px for space

fahd4007
  • 544
  • 4
  • 14
0

You can nest 2 div tags inside the container. Float them both and resize them as you need them to be.

Desdenova
  • 5,326
  • 8
  • 37
  • 45
0

Set a bottom margin on the floated element that equals the length of the remainder. Or add a width to the larger element and float it the other direction.

<div id="parent">
    <div id="floated" style="width:200px; float:right">Foo bar</div>
    <div id="content" style="width:600px; float:left">
        <h2>Foo</h2>
        <p>Text!</p>
    </div>
</div>

OR

<div id="parent">
    <div id="floated" style="width:200px; margin-bottom:200px; float:right">Foo bar</div>
    <h2>Foo</h2>
    <p>Text!</p>
</div>
Michael Jasper
  • 7,962
  • 4
  • 40
  • 60
  • When content is an undefined amount of text it's hard to predict what the reminder size is so your second solution is not an option really. – Robert Koritnik May 26 '11 at 16:45