简体   繁体   English

并排浮动时,一个底对齐两个div如何?

[英]How would one bottom-align two divs when floated side-by-side?

I have two boxes visible on a web page, one floating on the left and the other floating on the right. 我在网页上有两个可见的框,一个漂浮在左侧,另一个漂浮在右侧。 I want these boxes to align along the bottom. 我希望这些框沿底部对齐。 The problem is, there is one box whose height will change for different screen widths because it contains text and when the text wraps the height increases. 问题是,有一个框的高度将针对不同的屏幕宽度而变化,因为它包含文本,并且当文本换行时,高度会增加。 The other box doesn't have any text and can be any height, but it has text above it which will change the box's y position depending on how much text wraps. 另一个框没有任何文本,可以是任何高度,但是它上面有文本,这将根据换行的多少来更改框的y位置。

I would like to bottom align the two boxes by changing the height of the box that contains no text, or increase the padding/margin above it. 我想通过更改不包含文本的框的高度来使两个框底部对齐,或者增加其上方的填充/边距。 I do not want to change the right box's height, and I want the top left text and the top of the right box to remain aligned at the top. 我不想更改右框的高度,并且我希望左上方的文本和右框的顶部在顶部保持对齐。 If possible only using CSS, but a JavaScript solution would be acceptable, preferably no jQuery or other plugins. 如果可能仅使用CSS,但可以接受JavaScript解决方案,最好不使用jQuery或其他插件。

I have defined an outer div with width set to 100% and some padding so that the content on the page is inset from the edges. 我定义了一个外部div ,其width设置为100%并添加了一些padding以便页面的内容从边缘插入。 Inside this outer div is a div that is float ing left , width set to 35% . 在这个外部div是一个left floatdiv ,其width设置为35% Inside that div is a div that contains text, and a sibling to that div is another div that is the box that contains no text whose height is a fixed value that I can change. 在该div是一个包含文本的div ,该div的同级是另一个div ,该div是不包含其height是我可以更改的固定值的文本的框。 Then there is a box that's a child of the outer div , which is float ing right with width set to 60% . 然后有一个盒子是外部div的子div ,该盒子right floatwidth设置为60% This box will increase in height as the browser width decreases due to the wrapping text this box contains - I don't wish to modify its height. 由于此框包含换行符,因此随着浏览器宽度的减小,此框的height将增加-我不希望修改其高度。

<div id="outerDiv" style="width:100%; padding:5%;">
    <div id="leftContainer" style="float:left; width:35%;">
        <div id="topText">some text here...</div>
        <div id="bottomEmptyBox" style="width:100%; height:200px;"></div>
    </div>
    <div id="rightContainerBox" style="float:right; width:60%;">
        <div id="containedText">some text here...</div>
    </div>
</div>

目前的情况

盒子高度增加时解决方案的外观

顶部填充/边距增加时解决方案的外观

Jsfiddle Demo Jsfiddle演示

This would seem to be a good base that achieves most of your objectives 这似乎是实现大多数目标的良好基础

CSS 的CSS

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

#outerDiv {
    display: table;
}
#leftContainer,
#rightContainerBox {
    display: table-cell;
}
#bottomEmptyBox {
    border:1px solid red;
}

#topText {
   padding-bottom: 15px;
}

#rightContainerBox {
    border:1px solid green;
}

Give parent div display: table; 给父div display: table; or display:table-row and child div display:table-cell display:table-row和child div display:table-cell

Also add height auto if necessary. 如果需要,还可以添加自动高度。 Or it will take by default. 否则将默认使用。

Or you can use jQuery plugin if you need much scalable. 或者,如果您需要更大的可扩展性,则可以使用jQuery插件。

http://brm.io/jquery-match-height-demo/ http://brm.io/jquery-match-height-demo/

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

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