tags:

views:

40

answers:

1

Hi there,

how can I have 2 block elements ("block" because I want to apply a background image to one of them) aligned (one on the left, one on the right), where:

  • the width of the left element is defined by the text line it contains (can vary...)
  • the width of the right element takes up the rest of the total width
  • the total width is fixed (given by some parent element)

Like so:

<div id="some_parent_element_with_fixed_width">
    <div class="left">Here should be some text of varying length...</div>
    <div class="right">Here should be displayed a x-repeat background image on the entire remaining width...</div>
</div>

Thanks a lot for any cross-browser solutions to this! Tom

A: 

Use float:left on the left element, that will make it take up only the size of it's content. Use overflow:hidden on the right element, that will automatically use the rest of the space as the default for the width property of a block element is auto.

.left { float: left; }
.right { overflow: hidden; background: url(someimage.gif) repeat-x; }
Guffa
Thanks, but the problem with this is: At least in FF, the element on the right (width:auto;) does not respect the left-floated element: it takes up the entire width of the parent element (instead of just the remaining width).
TomDogg
@TomDogg: Yes, you are right, the floating element doesn't actually push the other element away, but it does push it's content away. If you place another div in the right element and apply the background to that, it should work.
Guffa
@Guffa: "...but it does push it's content away" is true, but unfortunately, the background image of the right element within the new/additional DIV still runs on the entire width...
TomDogg
@TomDogg: Right, my bad, it's the same for the inner element. What you need is `overflow:hidden` on the right element. Tested and working. :)
Guffa
Thanks for your updates - it works great!
TomDogg