使用positiont和transform进行水平和垂直居中
实例代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.center{
border:3px solid green;
width:400px;
height:200px;
position:relative;
}
.center p{
margin:0;
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
}
}
</style>
</head>
<body>
<h1>居中</h1>
<div class="center">
<p>我是水平和垂直居中的。</p>
</div>
<p>这个实例是用positiont和transform来实现文本的垂直居中</p>
</body>
</html>
运行效果:
评论