I have one parent div with two childs div, with the one on the left has a fixed width of 300px with border of 1px, the one on the right would be flexible and now I explain what I have done:
When on my screen my browser window is maximized, I have measuered the inner width in px of the parent, the percentage of a child div, if I am right, is relative to its parent, so I have the left div with a fixed 300px, and a right div with a flexibile width, therefore I have calculated its percentage width in this way:
1264px - is the inner width of the parent
960px - is the inner width of the box on the right with 1px borders, but I want to calculate it in percentage:
if 1264px is the 100% of the parent, what is the percentage width of the child with a width of 960px? Set this proportion:
1264px : 100% = 960px : x
x = (960 * 100)/1264 = 75,94936708860759%
so, with a maximized browser window I obtain the right width of the right div, but, when I begin to reduce the window, the right div goes below, but I do want that the right div remains in the same line but with a flexibile/elastic width.
I hope I have explained rightly the problem! Can you help me please, thanks!
Looks like you are over thinking it with those calculations. Forget floats and try display: table.
If the parent is 100%, the left is fixed at 300px, the right will take whatever space remains.
Hi, @SamA74 already gave you a good solution, and then @coothead another. But there are more alternatives.
If you for some reason need the floats or maybe want the right div still being able to drop below, you can fix your CSS by removing the float and width on “rightdiv” and make it aware of the floated “leftdiv”. You can try this:
For a box in the flow the default overflow value is “visible” and content will show outside the box if it dosn’t fit.
With other values of overflow, like e.g. “hidden” or “auto” the browser needs to decide if the content fits or not. Now if it have to deal with a float, it must decide if the float is inside or outside the box, and make the box include the float or stay away from it.