what i have is 3 divs, 1 for left 1 for center and 1 for right
what i need is 3 columns – the left is always there, the center as well but it’s width should be adaptive if the right is there or not
<html>
<head>
</head>
<body>
<div style="width:460px; padding:0px 20px;">
<div style="float:left; background: red; width:100px;">
red
</div>
<div style="float:left; background: yellow; max-width:400px">
yellow
</div>
<div style="float:left; background: green; width:100px;">
green
</div>
</div>
</body>
</html>
what am i’m doing wrong?
The best I could come up with, in order to avoid JavaScript solutions and to use CSS and HTML only, is to use class-names for the columns, and to re-order your HTML in order that the right-most column is first in the html:
With the CSS adjacent-sibling selector,
firstSibling + secondSibling, this can be used to amend the width of the middle column:JS Fiddle demo.
If you float the
.middlecolumn right, instead of left as in the previous example, then you can simplify the adjacent-sibling selector, and the HTML is, effectively, visually reversed (which is slightly easier to understand/work with than the above example wherein the two columns come first, in reverse order, and then the middle column comes at the end), giving:And the CSS:
JS Fiddle demo.
References: