Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Two divs - one fills space, one shrink-to-fit

Tags:

html

css

layout

Is there a way to have two divs placed next to each other so that:

  • The width of the outer div is unknown
  • The rightmost div atapts its width to its content (shrink-to-fit)
  • The leftmost div fills the remaining space

I see that Paul D. Waite has almost cut it here: xHTML/CSS: How to make inner div get 100% width minus another div width

In my case, the two inner divs need to switch places, and I just can't cut it.

Any ideas?

like image 211
Anders Mogensen Avatar asked May 05 '11 06:05

Anders Mogensen


2 Answers

Simply change float: left to float: right in Paul's example.

HTML:

<div id="outer">
    <div id="adaptive">I will adapt my width to my content.</div>
    <div id="filler">I will fill the remaining space.</div>
</div>

CSS:

#outer { overflow: hidden; width: 100%; }
#adaptive { float: right; }
#filler { overflow: hidden; }

Test jsFiddle

like image 161
kapa Avatar answered Oct 19 '22 03:10

kapa


Here you go:

http://jsfiddle.net/BhAcn/1/

Paul Waite's example fitted to your question

#outer {
    overflow: hidden;/* Makes #outer contain its floated children */
    width: 100%;
}

#inner1 {
    float: right;/* Make this div as wide as its contents */
}

#inner2 {
    overflow: hidden;/* Make this div take up the rest of the horizontal space, and no more */
}
like image 2
Bazzz Avatar answered Oct 19 '22 04:10

Bazzz