css不提供单行文字垂直居中功能
但是可以用小技巧使它垂直居中。
让高度和文字行号相同。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1{
height: 40px;
line-height: 40px;
background-color: red;
/* 相同居中 */
}
.div2{
height: 40px;
line-height: 30px;
background-color: red;
/* 小于居上 */
}
.div3{
height: 40px;
line-height: 50px;
background-color: red;
/* 大于居下 */
}
</style>
</head>
<body>
<div class="div1">1</div>
<br/>
<div class="div2">2</div>
<br/>
<div class="div3">3</div>
</body>
</html>