Center absolute div vertically with no defined height
This question already has an answer here:
You can place .child
at top: 50%;
and move it up with transform: translateY(-50%);
, which will centre your element. This will put your element 50% from the top of the parent, and move it 50% up of the child element's height.
.parrent{
position: relative;
}
.child {
background: red;
width: 50px;
height: 50px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
See working solution on this fiddle
You can use this snippet:
https://jsfiddle.net/ocbzdkpd/1/
Basically you need:
top: 50%;
transform:translateY(-50%);
Because doesn't matter the sizes of the boxes.
Flexbox可以帮助你。
.parent{
display:flex;
align-items:center; // vertical alignment
}
.child {
background: red;
width: 50px;
height: 50px;
position: absolute;
}
<div class="parent">
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>
<div class="child"></div>
</div>
链接地址: http://www.djcxy.com/p/41514.html
下一篇: 垂直居中绝对div,没有定义的高度