Lyraa's Blog

absolute 元素垂直水平居中

2017-07-10

关于元素居中问题是个古老的话题,常见方法有 table + table-cell || flex + justify-content + align-items.
今天又学到一种利用 position: absolute 来实现水平垂直居中的方法。

.container {
position: relative;
height: 100%;
width: 100%;
}

.inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 50%;
height: 50%;
margin: auto;
}
Tags: CSS

扫描二维码,分享此文章