Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill remaining space with left div

Tags:

css

css-float

Can anyone tell me how to make a left div fill the remaining space, without fixing the right div size.

I want the exact opposite of the following example:

.left {float: left; border: 1px solid blue;}
.right {overflow: hidden; border: 1px solid blue;}

The right div should take only the size it needs, the left div should take all the remaining space.

like image 305
user1565557 Avatar asked Jan 26 '13 14:01

user1565557


People also ask

How do I fill a div with remaining space?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do you make a div take up the whole height?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.

How do I make divs side by side?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.


2 Answers

The right div with a fixed width must float:right; then the left div must stay as it is so it can take its full available width, but since you want the right div to be fixed, you must place it first.

HTML:

<div id="parentDiv">
    <div id="rightFixedDiv"></div>
     <div id="leftDynamicDiv></div>
</div>

CSS:

#rightFixedDiv
{
   float:right; 
   border-style:solid; 
   width:100px; 
   height:200px;
}
#leftDynamicDiv
{
   border-style:solid; 
   background-color:blue; 
   overflow:hidden; 
   height:200px;
}

Check it out, fixed width of 100px: http://jsfiddle.net/dkGbd/ fixed width of 200px: http://jsfiddle.net/eESTZ/

Now if you want the opposite, place the left div first, give it a float:left;

Working example: http://jsfiddle.net/UShek/

like image 175
Ali Bassam Avatar answered Oct 04 '22 17:10

Ali Bassam


Most of what you have to do is reverse the css and html order of what you did here. The floating element must always be first.

<div class="right">this is the right div</div>
<div class="left">left div</div>

Then the CSS would be

.right {
  float: right;
  background: #fef;
}
.left {
  display: block;
  background: #ffe;
}

Update: Here is the fiddle

like image 42
Kyle Avatar answered Oct 04 '22 16:10

Kyle