CSS - 将 div 对齐到其父项 div 的底部
CSS - Align div to bottom respective his parent div
我正在尝试将 div#alignBottom1 和 #alignBottom2 向下对齐,但不移除父项左侧的浮动 div,也不使用绝对位置或顶部边距。
我该怎么办?
这是我的代码:
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
感谢您的帮助!
如果将父容器变成 flexbox,就可以很容易地做到这一点。
在您的示例中,我为父项提供了一个高度值,以便您可以看到使用 flexbox 并将内容对齐到其父项末尾的效果。
#alignBottom {
display: flex;
flex-flow: column nowrap;
justify-content: flex-end;
height: 100px; /* giving the element a height to exaggerate the effect */
}
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div id="alignBottom" class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
CSS flexbox 以多种方式帮助对齐容器内的内容,只需几行代码。这可能对你有用。
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-6:nth-child(2){
/* adding this */
display: flex;
flex-direction: column;
justify-content: flex-end;
/* adding some height to the container for better visibility od effect */
height: 80px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
我正在尝试将 div#alignBottom1 和 #alignBottom2 向下对齐,但不移除父项左侧的浮动 div,也不使用绝对位置或顶部边距。
我该怎么办?
这是我的代码:
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
感谢您的帮助!
如果将父容器变成 flexbox,就可以很容易地做到这一点。
在您的示例中,我为父项提供了一个高度值,以便您可以看到使用 flexbox 并将内容对齐到其父项末尾的效果。
#alignBottom {
display: flex;
flex-flow: column nowrap;
justify-content: flex-end;
height: 100px; /* giving the element a height to exaggerate the effect */
}
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div id="alignBottom" class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
CSS flexbox 以多种方式帮助对齐容器内的内容,只需几行代码。这可能对你有用。
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-6:nth-child(2){
/* adding this */
display: flex;
flex-direction: column;
justify-content: flex-end;
/* adding some height to the container for better visibility od effect */
height: 80px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>