我有2个div:一个在左边,一个在右边。左边的宽度是固定的,我想用右边的填充剩余的空间。

#搜索{ 宽度:160 px; 高度:25 px; 浮:左; background - color: # ffffff; } #{导航 宽度:780 px; 浮:左; background - color: # A53030; } < div id = "搜索" > < / div >文本 < div id = "导航" > < / div >导航


当前回答

我有一个非常简单的解决办法! / / HTML

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

/ / CSS

#left {
float:left;
width:50%;
position:relative;
background-color:red;
}
#right {
position:relative;
background-color:#00FF00;}

链接:http://jsfiddle.net/MHeqG/

其他回答

尝试添加相对位置,删除右边的宽度和浮动属性,然后添加0值的左和右属性。

此外,您还可以添加左距规则,其值基于左元素的宽度(如果您需要在两者之间留出空间,则要加上一些像素)来保持其位置。

这个例子对我来说很有用:

   #search {
        width: 160px;
        height: 25px;
        float: left;
        background-color: #FFF;
    }

    #navigation {  
        display: block;  
        position: relative;
        left: 0;
        right: 0;
        margin: 0 0 0 166px;             
        background-color: #A53030;
    }

我想知道为什么没有人把position: absolute和position: relative放在一起使用

所以,另一个解决方案是:

HTML

<header>
  <div id="left"><input type="text"></div>
  <div id="right">Menu1 Menu2 Menu3</div>
</header>

CSS

header { position: relative;  }
#left {
    width: 160px;
    height: 25px;
}
#right {
    position: absolute;
    top: 0px;
    left: 160px;
    right: 0px;
    height: 25px;
}

Jsfiddle例子

.container { 宽度:100%; 显示:表; vertical-align:中间; } .left { 宽度:100%; 显示:表格单元; text-align:中心; } 铃声{ 宽度:40像素; 高度:40像素; 显示:表格单元; 浮:正确; } < div class = "容器" > 左< div class = "左" > < / div > < div class = "对" >对< / div > < / div

试试这个。这对我很管用。

如果你不需要兼容某些浏览器的旧版本(例如IE 10 8或更低版本),你可以使用calc() CSS函数:

#left {
   float:left;
   width:180px;
   background-color:#ff0000;
}

#right {
   float: left;
   width: calc(100% - 180px);
   background-color:#00FF00;
}

/* * css * /

#search {
 position: absolute;
 width: 100px;
}
.right-wrapper {
  display: table;
  width: 100%;
  padding-left:100px;
}
.right-wrapper #navigation {
 display: table-cell;
 background-color: #A53030;
}

/* * html * /

<div id="search"></div>
<div class="right-wrapper">
   <div id="navigation"></div>
</div>