How to make a div fill a remaining horizontal space?
I have 2 divs: one in the left side and one in the right side of my page. The one in the left side has fixed width and I want the one of the right side to fill the remaining space.
The one on the right side is the navigation and I want it to to fill the remaining space on it right side:
#search {
width: 160px;
height: 25px;
float: left;
background-color: #ffffff;
}
#navigation {
width: 780px;
float: left;
/*background-color: url('../images/transparent.png') ;*/
background-color: #A53030;
}
<div id="search">Text
</div>
<?php include("navigation.html"); ?>
<div id="left-column">
</div>
这似乎完成了你要去的。
#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>
The problem that I found with Boushley's answer is that if the right column is longer than the left it will just wrap around the left and resume filling the whole space. This is not the behavior I was looking for. After searching through lots of 'solutions' I found this awesome tutorial on creating three column pages.
The author offer's three different ways, one fixed width, one with three variable columns and one with fixed outer columns and a variable width middle. Much more elegant and effective than other examples I found. Significantly improved my understanding of CSS layout.
Basically, in the simple case above, float the first column left and give it a fixed width. Then give the column on the right a left-margin that is a little wider than the first column. That's it. Done. Ala Boushley's code:
Here's a demo in Stack Snippets & jsFiddle
#left {
float: left;
width: 180px;
}
#right {
margin-left: 180px;
}
/* just to highlight divs for example*/
#left { background-color: pink; }
#right { background-color: lightgreen;}
<div id="left"> left </div>
<div id="right"> right </div>
The solution comes from the display property.
Basically you need to make the two divs act like table cells. So instead of using float:left
, you'll have to use display:table-cell
on both divs, and for the dynamic width div you need to set width:auto;
also. The both divs should be placed into a 100% width container with the display:table
property.
Here is the css:
.container {display:table;width:100%}
#search {
width: 160px;
height: 25px;
display:table-cell;
background-color: #FFF;
}
#navigation {
width: auto;
display:table-cell;
/*background-color: url('../images/transparent.png') ;*/
background-color: #A53030;
}
*html #navigation {float:left;}
And the HTML:
<div class="container">
<div id="search"></div>
<div id="navigation"></div>
</div>
IMPORTANT:For Internet Explorer you need to specify the float property on the dynamic width div, otherwise the space will not be filled.
I hope that this will solve your problem. If you want, you can read the full article I wrote about this on my blog.
链接地址: http://www.djcxy.com/p/15502.html上一篇: Div高度100%并扩展以适应内容
下一篇: 如何让div填充剩余的水平空间?