简体   繁体   English

是否可以底部对齐浮动div?

[英]Is it possible to bottom align a floated div?

I want to avoid using position: absolute; 我想避免使用position: absolute; because I want to keep the functionality where when the parent container shrinks, the divs position themselves one under the other, instead of overlapping. 因为我想保留父容器缩小时的功能,div将它们自己定位在另一个之下,而不是重叠。

 #container { border:1px solid; } #left { float:left; width:100px; height:100px; background:red; } #right { float:right; background:blue; } ul { padding:0; margin:0; } ul li { float:right; margin-left:20px; } 
 <div id="container"> <div id="left"> </div> <div id="right"> <ul> <li>item1</li> <li>item1</li> <li>item1</li> <li>item1</li> <li>item1</li> <li>item1</li> </ul> </div> <div style="clear:both;"></div> </div> 

Edit: I've updated the code sample to more closely resemble what my own actually looks like. 编辑:我已经更新了代码示例,使其更像我自己的实际外观。 Unfortunately I'm not working with fixed widths/heights, though for this particular problem, the red div can have a fixed width/height. 不幸的是,我没有使用固定宽度/高度,但对于这个特殊问题,红色div可以有固定的宽度/高度。

Updated Fiddle: http://jsfiddle.net/zdL60bLu/10/ 更新小提琴: http//jsfiddle.net/zdL60bLu/10/

Basically, I'd like the blue div to be aligned to the bottom right, and when the window size shrinks, to keep the functionality of the blue div moving below the red div, which doesn't happen with position absolute. 基本上,我希望蓝色div与右下方对齐,当窗口大小缩小时,保持蓝色div的功能在红色div下方移动,而绝对位置不会发生这种情况。

Is this doable? 这可行吗?

This layout can be done with flexbox. 这种布局可以使用flexbox完成。

It fulfills both your requirements: 它满足您的要求:

  • The blue div is aligned to the bottom right and 蓝色div与右下角对齐

  • When the window size shrinks, the blue div moves below the red div 当窗口大小缩小时,蓝色div移动到红色div下方

    Except that when you set display:flex on a container, floats have no effect on the items. 除了在容器上设置display:flex时,浮动对项目没有影响。 [so if you like you can even leave the float:left; [所以如果你喜欢你甚至可以离开float:left; and float:right on the left and right elements - but know that they do nothing] float:right的元素 - 但知道它们什么都不做]

    Note that float, clear and vertical-align have no effect on a flex item. 请注意,float,clear和vertical-align对flex项目没有影响。

    ( CSS-tricks ) CSS技巧

Updated Fiddle (Resize to see the effect) 更新小提琴 (调整大小以查看效果)

 #container { border: 1px solid; display: flex; justify-content: space-between; flex-flow: row wrap; } #left { width: 100px; height: 100px; background: red; } #right { float: right; /* redundant */ background: blue; align-self: flex-end; } ul { padding: 0; margin: 0; } ul li { float: right; margin-left: 20px; } 
 <div id="container"> <div id="left"> </div> <div id="right"> <ul> <li>item1</li> <li>item1</li> <li>item1</li> <li>item1</li> <li>item1</li> <li>item1</li> </ul> </div> </div> 

This is a little hacky, but it works: Give the right container a margin-top of the height of the left container minus the height of the right container (in this case, 50px). 这有点hacky,但它的工作原理是:给右侧容器一个左侧容器高度的上边距减去右侧容器的高度(在这种情况下为50px)。 Then give the left container a negative margin-bottom of the same amount (-50px). 然后给左容器一个相同数量的负边距 - 底部(-50px)。 Here is it working: http://jsfiddle.net/zdL60bLu/9/ 这是它的工作: http//jsfiddle.net/zdL60bLu/9/

Code added: 代码添加:

#left {
    margin-bottom: -50px;
}
#right {
    margin-top: 50px;
}

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

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