Fixed Width Div on Left, Fill Remaining Width Div on Right

Fixed width div on left, fill remaining width div on right

Try removing the float:left and width:100% from .header-right — the right div then behaves as requested.

.header {  float: left;  background: #efefef;  background-repeat: no-repeat;  width: 240px;  height: 100px;}
.header-right { overflow: hidden; background-color: #000; height: 100px;}
<div class="header"></div><div class="header-right"></div>

Fixed width div on right, fill remaining width div on left

You can do it like this:

HTML:

<div class="right">right</div>
<div class="left">left</div>

CSS:

.left{
background:red;

}
.right{
float:right;
width:200px;
background:green
}

Check this live example http://jsfiddle.net/QHTeS/2/

Expand a div to fill the remaining width

The solution to this is actually very easy, but not at all obvious. You have to trigger something called a "block formatting context" (BFC), which interacts with floats in a specific way.

Just take that second div, remove the float, and give it overflow:hidden instead. Any overflow value other than visible makes the block it's set on become a BFC. BFCs don't allow descendant floats to escape them, nor do they allow sibling/ancestor floats to intrude into them. The net effect here is that the floated div will do its thing, then the second div will be an ordinary block, taking up all available width except that occupied by the float.

This should work across all current browsers, though you may have to trigger hasLayout in IE6 and 7. I can't recall.

Demos:

  • Fixed Left: http://jsfiddle.net/A8zLY/5/
  • Fixed Right: http://jsfiddle.net/A8zLY/2/

div {
float: left;
}

.second {
background: #ccc;
float: none;
overflow: hidden;
}
<div>Tree</div>
<div class="second">View</div>

How to make a div fill a remaining horizontal space?

This seems to accomplish what you're going for.

#left {  float:left;  width:180px;  background-color:#ff0000;}#right {  width: 100%;  background-color:#00FF00;}
<div>  <div id="left">    left  </div>  <div id="right">    right  </div></div>

make left div to fill all available space, when right div has fixed width

Try this:

HTML

<div id="left">
Menu
</div>
<div style="text-align: center;">
<div id="right">
Content
</div>
</div>

CSS

#left {
background-color: green; /*for demonstration purposes*/
float:left;
}

#right{
background-color: blue;
width: 800px;
}

JS

$(document).ready(function(){
var width = $(document).width();
var rightDivWidth = $("#right").width();
var leftDivWidth = width - rightDivWidth;
$("#left").css("width", leftDivWidth);
});

Demonstration

Two divs (left, right) in parent, right with fixed width, left fill free space, both in same line. (without position relative/absolute)

You could do it like this:

JSFiddle - DEMO

CSS:

.container {
width: 500px;
background-color: bisque;
height: 50px;
display: table;
}
.container .left {
background-color: burlywood;
height: 50px;
display: table-cell;
width: 100%;
}
.container .right {
background-color: chartreuse;
width: 50px;
height: 50px;
display: inline-block;
vertical-align: text-top;
}

Fixed width div on left, fill remaining space with input box on right

One option would be to use calc:

http://jsfiddle.net/sXMGB/3/

.feed_comments_add {
float: left;
background: green;
width: calc(100% - 25px);
}

IE9+

CSS fill left and right divs while the center div has fixed width

Add flex-grow: 1; to both the left and right divs:

.wrapper{
background-color: red;
height: 200px;
width: 100%;
display:flex;
flex-direction:row;
}
.left{
background-color: blue;
}
.center{
background-color: yellow;
width: 200px;
margin-left:auto;
margin-right:auto;
}
.right{
background-color: blue;
}

.left, .right {
flex-grow: 1;
}
<div class="wrapper">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>

Fill the remaining height or width in a flex container

Use the flex-grow property to make a flex item consume free space on the main axis.

This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

A common example is flex-grow: 1 or, using the shorthand property, flex: 1.

Hence, instead of width: 96% on your div, use flex: 1.


You wrote:

So at the moment, it's set to 96% which looks OK until you really squash the screen - then the right hand div gets a bit starved of the space it needs.

The squashing of the fixed-width div is related to another flex property: flex-shrink

By default, flex items are set to flex-shrink: 1 which enables them to shrink in order to prevent overflow of the container.

To disable this feature use flex-shrink: 0.

For more details see The flex-shrink factor section in the answer here:

  • What are the differences between flex-basis and width?

Learn more about flex alignment along the main axis here:

  • In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Learn more about flex alignment along the cross axis here:

  • How does flex-wrap work with align-self, align-items and align-content?


Related Topics



Leave a reply



Submit