How can I make a div fill up the remaining width?
<div id="Main" style="width: 500px;"> <div id="div1" style="width: 100px;"></div> <div id="div2"></div> <div id="div3" style="width: 100px; float: right;"></div> </div>
How can I get div2
to fill up the remainder?
Set display to inline-block to make the div width fit to its content. Use position: relative and left: 50% to position its left edge to 50% of its containing element. Use transform: translateX(-50%) which will "shift" the element to the left by half its own width.
Try out something like this:
<style> #divMain { width: 500px; } #left-div { width: 100px; float: left; background-color: #fcc; } #middle-div { margin-left: 100px; margin-right: 100px; background-color: #cfc; } #right-div { width: 100px; float: right; background-color: #ccf; } </style> <div id="divMain"> <div id="left-div"> left div </div> <div id="right-div"> right div </div> <div id="middle-div"> middle div<br />bit taller </div> </div>
divs will naturally take up 100% width of their container, there is no need to explicitly set this width. By adding a left/right margin the same as the two side divs, it's own contents is forced to sit between them.
Note that the "middle div" goes after the "right div" in the HTML
This solution is even simpler than the one provided by Leigh
. It is actually based on it.
Here you can notice that the middle element (in our case, with "content__middle"
class) does not have any dimensional property specified - no width, nor padding, nor margin related property at all - but only an overflow: auto;
(see note 1).
The great advantage is that now you can specify a max-width
and a min-width
to your left & right elements. Which is fantastic for fluid layouts.. hence responsive layout :-)
note 1: versus Leigh's answer where you need to add the margin-left
& margin-right
properties to the "content__middle"
class.
Here the left & right elements (with classes "content__left"
and "content__right"
) have a fixed width (in pixels): hence called non-fluid layout.
Live Demo on http://jsbin.com/qukocefudusu/1/edit?html,css,output
<style> /* * [1] & [3] "floats" makes the 2 divs align themselves respectively right & left * [2] "overflow: auto;" makes this div take the remaining width */ .content { width: 100%; } .content__left { width: 100px; float: left; /* [1] */ background-color: #fcc; } .content__middle { background-color: #cfc; overflow: auto; /* [2] */ } .content__right { width: 100px; float: right; /* [3] */ background-color: #ccf; } </style> <div class="content"> <div class="content__left"> left div<br/>left div<br/>left div<br/>left div<br/>left div<br/>left div<br/> </div> <div class="content__right"> right div<br/>right div<br/>right div<br/>right div<br/> </div> <div class="content__middle"> middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br />bit taller </div> </div>
Here the left & right elements (with classes "content__left"
and "content__right"
) have a variable width (in percentages) but also a minimum and maximum width: hence called fluid layout.
Live Demo in a fluid layout with the max-width
properties http://jsbin.com/runahoremuwu/1/edit?html,css,output
<style> /* * [1] & [3] "floats" makes the 2 divs align themselves respectively right & left * [2] "overflow: auto;" makes this div take the remaining width */ .content { width: 100%; } .content__left { width: 20%; max-width: 170px; min-width: 40px; float: left; /* [1] */ background-color: #fcc; } .content__middle { background-color: #cfc; overflow: auto; /* [2] */ } .content__right { width: 20%; max-width: 250px; min-width: 80px; float: right; /* [3] */ background-color: #ccf; } </style> <div class="content"> <div class="content__left"> max-width of 170px & min-width of 40px<br />left div<br/>left div<br/>left div<br/>left div<br/>left div<br/>left div<br/> </div> <div class="content__right"> max-width of 250px & min-width of 80px<br />right div<br/>right div<br/>right div<br/>right div<br/> </div> <div class="content__middle"> middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br />bit taller </div> </div>
Tested on BrowserStack.com on the following web browsers:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With