设置文字水平居中很简单,只需在需要居中的元素的父元素中加入text-align: center;即可
text-align: center;
设置垂直居中则需设置元素的高度和行高为同一高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试</title>
<style>
.middle {
/* 水平居中 */
text-align: center;
width: 150px;
/* 高度和行高一样才能垂直居中 */
height: 80px;
line-height: 80px;
background-color: rgb(43, 159, 87);
display: inline-block;
}
</style>
</head>
<body>
<div class="middle"><span class="content">水平居中</span></div>
</body>
</html>
效果如图所示