简体   繁体   English

将浮动div与父div对齐

[英]Aligning a floated div to the parent div

If it's given a div and a sequence of nested divs with float: left properties, how do you make the right edge of the last div in the sequence "stick" to the right edge of the parent div whenever the the right edge of the parent div moves and in way that the two edges move together even if the content of the last div exceeds the available room? 如果给定一个div和一个带有float: left属性的嵌套div序列,那么无论父级的右边缘如何,如何使序列中最后一个div的右边缘“粘”到父级div的右边缘div移动,并且即使最后一个div的内容超出了可用空间,两个边缘也会一起移动?

+----------------------------------------+
| parent div                             |
| +-------------+ +---------------------+|
| |             | |                     ||
| | div 1       | | div 2             >>||<< the right edge of div 2 
| | float: left | | float: left         ||   aligns with the right edge
| |             | |                     ||   of the parent div
| +-------------+ +---------------------+|
+----------------------------------------+

EDIT: A condition I should have mentioned is that the divs before the last one are allowed to have fixed widths but the width of the last div should depend on its contents and the position of the right edge of the parent div. 编辑:我应该提到的一个条件是,允许最后一个div之前的div具有固定的宽度,但是最后一个div的宽度应取决于其内容和父div右边缘的位置。 Also, the last div is required to be floated left. 另外,最后一个div需要向左浮动。

If you must float:left the last div then only way to align it properly is to set width of both elements OTHERWISE its not possible OR you must remove float:left on second div. 如果必须在最后一个div上进行float:left调整,则正确对齐的唯一方法是设置两个元素的宽度,否则将无法设置,或者必须在第二个div上删除float:left。

http://jsfiddle.net/K9a8p/ http://jsfiddle.net/K9a8p/

your best way to get through this loop is to give the last div a class - lastChild and add the border-right:0 to that class.. else,here's another quick fix 解决此循环的最佳方法是给最后一个div一个类-lastChild并在该类中添加border-right:0 。否则,这是另一个快速解决方案

Your CSS 您的CSS

.container{
border:2px solid black;
height:40px;
width:auto;
overflow:hidden;
}
.ele{
float:left;
height:20px;
width:30%;
overflow:hidden;
border:3px solid red;
}
.container div:last-child{
border-right:0;
}

Your HTML 您的HTML

<div class="container">
<div class="ele" > </div>
<div class="ele" > </div>
<div class="ele" > </div>
</div>

here's a fiddle explaining the same.. 这是一个小提琴,解释了同样的情况。

DEMO 演示

You should note the browser compatibility for :last-child -- 您应该注意:last-child的浏览器兼容性-

here's another answer -- Using the last-child selector 这是另一个答案- 使用最后一个选择器

Internet Explorer 6 has no support for either. Internet Explorer 6均不支持。

IE7 and IE8 both support :first-child and not :last-child but a must be specified. IE7和IE8都支持:first-child,不支持:last-child,但必须指定a。

Both work in all versions of Firefox. 两者均可在所有版本的Firefox中使用。 I have tested Chrome for Windows, Opera 9.64 and Safari 3 and 4 for Windows and they all work in those versions. 我已经测试了Windows版Chrome,Opera 9.64和Windows版Safari 3和Safari 4,它们都适用于这些版本。

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

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