【发布时间】:2019-01-18 11:01:40
【问题描述】:
我在jsfiddle.net/5tzk3/10 使用了 JsFiddle。我将其更改为将 div 显示为方形对话框(水平和垂直居中)。结果在jsfiddle.net/5tzk3/548。
如您所见,水平居中很容易,但我无法将其垂直居中。有谁知道如何用纯 CSS 做到这一点?
编辑代码如下:
<div class="blind">
<div class="wrapper">
<div class="main">
I'm your div with an aspect-ratio of 1:1!
</div>
</div>
</div>
html, body, .blind {
height: 100%;
width: 100%;
}
.blind {
left: 0;
position: fixed;
text-align: center;
top: 0;
}
.wrapper {
display: inline-block;
margin: auto;
position: relative;
width: 50%;
}
.wrapper:after {
content: '';
display: block;
padding-top: 100%;
}
.main {
background-color: rgb(0, 162, 232);
bottom: 0;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
}
【问题讨论】:
标签: css center aspect-ratio