I have a simple two column layout with fixed widths.
<div id='box'>
<div id='ontop'>Ontop</div>
<div id='column1'>Column1</div>
<div id='column2'>Column1</div>
</div>
The CSS:
#box { width: 20em; }
#column1 {width: 12em;}
#column2 {float: left; width: 8em;}
I would like to have column1 expand to “100%” if column2 is empty or doesn’t exist.
Is it possible to do this just with CSS and without Javascript?
edit: changed #column1 width. example had an error, added div ontop.
This is actually easier if your elements are in the reverse order.
If they must stay in the order you specified, then using the table display properties will do it (without #column1 flowing around #column2 when #column2 is shorter):
Note that these solutions only cover the case of #column2 not being there. If you want to see if an element is empty (or not empty), you’ll want to use the
:emptypseudo class chained with:not:#column2:not(:empty)(this might not be the exact syntax).Edit: other options can include looking to see if #column1 is an only child (
#column1:only-child) or first/last child when it should be the last/first child (#column1:first-child/#column1:last-child).