views:

41

answers:

2

Hi

I have a page with 2 columns side by side (defined both by float: left).

My problem is, when I scale the browser to be smaller, the right column will be dropped below the left column.

how can I avoid this? and still to make them be side-by-side no matter the screen size.

Thank you

A: 

Make the parent container a fixed width or set the overflow to auto. For example if your two columns are 200px wide

<div style="width: 400px; overflow: auto;">
  <div style="float: left; width: 200px;"></div>
  <div style="float: left; width: 200px;"></div>
</div>
Ben Rowe
Thanks. can't I make the parent to be auto width?
oshafran
unless you specify the width, the default of block elements is to automatically adjust the width according to the available space. You could use javascript to calculate the width after loading the page.
Ben Rowe
@oshafran Because floats are weird. http://stackoverflow.com/questions/3480086/need-explanation-why-putting-right-floated-element-as-first-element-in-div-fixes/3481528#3481528
banzaimonkey
You could set the wrapper width to 100% and overflow:auto. This should cause the wrapper to resize with the browser and cause a scrollbar to appear. Untested
lnrbob
@Inrbob true, may cause other problems however.
Ben Rowe
+2  A: 

Alternatively if you want them to resize with the browser will need to define the width in percentages. So:

.div1 {
float:left;
width:49%;
background:red;
}

.div2 {
float:left;
width:49%;
background:orange;
}

Some people would use 50% here, I tend not to

lnrbob
+1 for 49%. Good protection against stray pixels. Note that percentages and fixed measurements (i.e. margins, padding, borders) don't play well together.
banzaimonkey