html怎么让div垂直居中
时间: 2024-01-05 10:18:58 浏览: 35
以下是两种HTML让div垂直居中的方法:
1.使用margin属性实现垂直居中:
```html
<style>
.container {
height: 200px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.box {
width: 100px;
height: 100px;
border: 1px solid blue;
}
</style>
<div class="container">
<div class="box"></div>
</div>
```
2.使用position和transform属性实现垂直居中:
```html
<style>
.father {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.child {
width: 100px;
height: 100px;
border: 1px solid blue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="father">
<div class="child"></div>
</div>
```