简体   繁体   English

CSS并排div与Pixel和百分比宽度

[英]CSS side by side div with Pixel and Percent widths

I have two div's (side by side) inside a parent div, i want right div to occupy 100% of remaining space (ie 100% - 200px) and should always stay next to left div (not below left div): 我在父div中有两个div(并排),我希望右div占据剩余空间的100%(即100% - 200px)并且应该始终保持在左边div(不在左边div之下):

 <div id="wrapper" style="width: 100%;"> <div id="left" style="background-color: Blue; height: 100px; float: left; width: 200px;"></div> <div id="right" style="background-color: Aqua; height: 100px; float: left; width: 100%;"></div> <div style="clear: both;"></div> </div> 

Since you have only one fixed width column, float it left and that is it. 由于您只有一个固定宽度的列,因此将其向左浮动即可。 As for the second column, do not specify float and width; 至于第二列,不要指定浮点数和宽度; this makes sure it is 100% wide. 这确保它是100%宽。 But you must add a left margin; 但是你必须添加左边距; otherwise the second column will interfere with the floated column eg 否则第二列将干扰浮动柱,例如

  • aqua background will appear behind blue background (turn off the blue background to see what I mean) 浅绿色背景将出现在蓝色背景后面(关闭蓝色背景,看看我的意思)
  • if second column becomes taller than first one, additional content will start appearing below the first column. 如果第二列变得比第一列高,则其他内容将开始出现在第一列下方。

 <div id="wrapper"> <div id="left" style="background-color: Blue; height: 100px; float: left; width: 200px;"></div> <div id="right" style="background-color: Aqua; height: 100px; margin-left: 200px;"></div> <div style="clear: both;"></div> </div> 

make the parent wrapper float . 使父包装器float Also you would probably want to remove the width: 100% in the second child div. 你也可能想要删除第二个子div中的width: 100% And have its width set by the amount of content inside. 并且其宽度由内部的内容量设置。 Or you could have percentage for both child divs. 或者你可以拥有两个子div的百分比。 Example 30% and 70% . 30%70%

Demo here 在这里演示

Add position properties to your right div. 向右侧div添加位置属性。 Left div 200px and right div occupies remaining space. 左div 200px和右div占用剩余空间。

#right{
    background-color:Aqua;
    height:100px;
    position:absolute;
    top:0;
    right:0;
    left:200px;
}

Check working example at http://jsfiddle.net/EpA5F/1/ 查看http://jsfiddle.net/EpA5F/1/上的工作示例

Ok, so on newer browsers we will be able to use display: box; 好的,所以在较新的浏览器上我们可以使用display:box; and box-flex: 1,... properties. 和box-flex:1,...属性。 I am currently using this in a webproject where only Chrome is required, so this new CSS3 thing, solved a lot of issues for me. 我目前在一个只需要Chrome的webproject中使用它,所以这个新的CSS3东西为我解决了很多问题。

The accepted answer is good, but I had an issue where the right column underlapped my subnavigation as it was floating as well. 接受的答案是好的,但我有一个问题,正确的栏目不足以支持我的子导航,因为它也是浮动的。

With modern browsers you can now have all elements floating and get the same effect with cooler CSS. 使用现代浏览器,您现在可以将所有元素浮动,并使用更酷的CSS获得相同的效果。 Using "width: calc(100% - 380px);" 使用“width:calc(100% - 380px);” means you can float your elements, get them positioned properly, and look cool... 意味着你可以漂浮你的元素,让它们正确定位,看起来很酷......

.container { float: left; width: 100%; }
.container__left { float: left; width: 380px; height: 100px; background: blue; }
.container__right { float: right; width: calc(100% - 380px); height: 100px; background: green; }

Demo http://jsfiddle.net/auUB3/1/ 演示http://jsfiddle.net/auUB3/1/

If you want right div to have constant width: 如果你想要正确的div具有恒定的宽度:

 <div style="position:relative">
   <div class='wrapper'>
      <div style="width: 70%"></div>
      <div style="width: 20%"></div>
      <div style="width: 10%"></div>
      <div style="clear:both"></div>
   </div>
   <div class="constant-width"><div>
 </div>

And CSS 和CSS

 .wrapper{
     margin-right: CONSTANTpx;
 }
 .wrapper div{
     float:left;
 }
 .constant-width{
     top: 0px; right:0px; position:absolute;
     width: CONSTANTpx
 }

Works good without borders 工作良好无国界

JSFiddle 的jsfiddle

your left div should have float left and its pixel width using position relative. 你的左边div应该左边浮动,它的像素宽度使用位置相对。 Your right div should only have position relative and maybe an overflow hidden. 你的右边div应该只有相对位置,也许隐藏溢出。 This should fix your problem. 这应该可以解决您的问题。 No need to use the use the div that clears the float. 不需要使用清除浮动的div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM