I am trying to create an arrangement whereby there are three divs within a div that fills up 100%-width of a page. The middle div will have a fixed width and the two outer divs will take up 50% each of the remaining space. Is this achievable using CSS?
I have set up a fiddle, that doesn't work, http://jsfiddle.net/6y5hn/ with my attempt at achieving this, with the following code:
<div id="outerbox">
<div id="leftbox">(elastic width) This should occupy all the space in the grey box to the left of the red box</div>
<div id="centerbox">(fixed-size) should be in the center of the grey box</div>
<div id="rightbox">(elastic width) This should occupy all the space in the grey box to the right of the red box</div>
</div>
With the CSS:
#outerbox {
background-color:gray;
margin-top:50px;
width:100%;
height:200px;
}
#centerbox {
background-color:red;
margin-left:auto;
margin-right:auto;
float:left;
height:100px;
width:300px;
}
#leftbox {
background-color:yellow;
height:300px;
float:left;
}
#rightbox {
background-color:pink;
height:300px;
float:left;
}
James
add
width:calc(50% - 150px);
to#leftbox
and#rightbox
(150px = half of the centerbox width)http://jsfiddle.net/6y5hn/2/
browser support: http://caniuse.com/calc
Not sure if this is achievable using only CSS, but here's a handy snippet of JavaScript code that can help you manage fixed and percent-based page widths more effectively:
You can then call the
resize()
function on page load, as well as on page resize. So something like:<body onload="resize()">
From here, because you have a calculated width of the page, you can resize your individual
div
s accordingly:The
centerbox
maintains a fixed with of 300 pixels, whileleftbox
andrightbox
have widths equal to the width of the screen minus 300 pixels, divided by two.Just use float with the a fixed width
http://jsfiddle.net/am46bm43/