- 在没使用flex之前,垂直居中最常用的方法就是将容器的line-height设置为和容器一样的高度,但这种方法很生硬,每次需要更改容器高度的时候都需要考虑line-height,使用以下这种方法可以实现自适应的垂直居中!
<style>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<style>
.innner {
display: flex;
background-color: #ea4d22;
height: 100px;
justify-content: center;
flex-direction: column;
}
.innner2 {
background-color: #20e64b;
height: 100px;
text-align: center;
}
.innner3 {
display: flex;
background-color: #18a0c2;
height: 100px;
text-align: center;
justify-content: center;
flex-direction: column;
}
</style>
<div class="innner">垂直居中示例</div>
<div class="innner2">水平居中示例</div>
<div class="innner3">垂直水平居中示例</div>
</body>
</html>
