1. 将文字水平居中,只需要在盒子里添加 text-align: center就可以了.
<style>
div{
text-align: center;
}
</style>
<body>
<div>将文字水平居中</div>
</body>
2. 将盒子垂直居中,只需要将文字行高等于盒子的高度就行.
<style>
div{
width: 500px;
height: 500px;
line-height: 500px;
background-color: red;
}
</style>
<body>
<div >将文字垂直居中</div>
两个属性都包含的时候,文字就会在盒子的中心了