实现任意元素的居中
作者:
小透明进击战 | 来源:发表于
2019-06-25 20:28 被阅读0次
- 水平居中一般使用margin:0 auto;但是这个无法实现垂直居中。
- 通过结合定位和transform来实现任意元素的水平垂直居中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.out {
width: 400px;
height: 400px;
background-color: firebrick;
margin: 200px auto;
position: relative;
}
.inner {
width: 60px;
height: 60px;
background-color: goldenrod;
position: absolute;
left: 50%;
top: 50%;
/*translate中百分比是以自己本身元素的宽高为参考*/
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="out">
<div class="inner"></div>
</div>
</body>
</html>
本文标题:实现任意元素的居中
本文链接:https://www.haomeiwen.com/subject/pvzxcctx.html
网友评论