<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style type="text/css">
html,body{
width: 100%;
height: 100%;
}
#box{
width: 50%;
height: 50%;
background: paleturquoise;
text-align: center;
line-height: 50%
}
</style>
<body>
<div id="box">
hehe
</div>
</body>
</html>
1,
line-height
的百分比是相对font-size
的,所以你如果需要使用line-height
居中的话就使用绝对的px
值。2,当然有其他方案,比如楼上的方案,还有css3的新特性,还有提前知道高度,把
line-height
设置为对应的高度就可以