如何使用绝对位置垂直居中对齐div?如果在单列中找到多个div,则div应该有margin-bottom。
.parent {
position: relative;
background: #FF0000;
width: 100%;
height: 100px;
padding:20px 0px;
}
.children_multiple_in_column {
position: absolute;
background: #000;
width: 150px;
height: 20px;
bottom: 50%;
top: 50%;
margin-bottom: 50px;
color: white;
z-index=1;
}
.children_single_in_column {
position: absolute;
background: #000;
width: 150px;
height: 20px;
left: 60%;
bottom: 50%;
top: 50%;
color: white;
z-index=1;
}
JSFiddle就在这里:http://jsfiddle.net/richersoon/m8kp92yL/8/
结果应该是这样的:
请忽略水平线并不重要。
答案 0 :(得分:0)
将多个项目包裹在div中并使用transform: translateY(-50%); top: 50%;
垂直对齐。
.parent {
position: relative;
background: #FF0000;
width: 100%;
height: 100px;
padding: 20px 0px;
}
.parent>div {
position: absolute;
background: #000;
width: 150px;
top: 50%;
color: white;
transform: translateY(-50%);
z-index: 1;
}
.children_single_in_column {
left: 60%;
}
.multiple>div {
margin-bottom: 10px;
}
.multiple>div:last-child {
margin-bottom: 0;
}
<div class="parent">
<div class="multiple">
<div class="children_multiple_in_column">Monday Task 1</div>
<div class="children_multiple_in_column">Monday Task 2</div>
</div>
<div class="children_single_in_column">Friday Task 1</div>
</div>
示例:JSfiddle