当前位置:网站首页>Seven ways to achieve vertical centering
Seven ways to achieve vertical centering
2022-07-05 11:58:00 【Xiongba is also】
If .parent Of height Don't write , You only need to padding: 10px 0; Will be able to .child Vertical center ;
If .parent Of height It's dead , It's hard to put .child In the middle , Here's how to center vertically .
advice : Can not write height Just don't write height.
1、table Built in features
Code
// html
<body>
<table class="parent">
<tr>
<td class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</td>
</tr>
</table>
</body>
.parent{
border: 1px solid red;
height: 600px;
}
.child{
border: 1px solid green;
}
design sketch

2、100% High after before add inline block
Code
// html
<body>
<div class="parent">
<span class=before></span><div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div><span class=after></span>
</div>
</body>
.parent{
border: 3px solid red;
height: 600px;
text-align: center;
}
.child{
border: 3px solid black;
display: inline-block;
width: 300px;
vertical-align: middle;
}
.parent .before{
outline: 3px solid red;
display: inline-block;
height: 100%;
vertical-align: middle;
}
.parent .after{
outline: 3px solid red;
display: inline-block;
height: 100%;
vertical-align: middle;
}
design sketch

Optimized version
// html
<body>
<div class="parent">
<div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div>
</div>
</body>
.parent{
border: 3px solid red;
height: 600px;
text-align: center;
}
.child{
border: 3px solid black;
display: inline-block;
width: 300px;
vertical-align: middle;
}
.parent:before{
content:'';
outline: 3px solid red;
display: inline-block;
height: 100%;
vertical-align: middle;
}
.parent:after{
content:'';
outline: 3px solid red;
display: inline-block;
height: 100%;
vertical-align: middle;
}
3、div Pretend to be table
Code
// html
<body>
<div class="table">
<div class="td">
<div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div>
</div>
</div>
</body>
// css div.table{
display: table;
border: 1px solid red;
height: 600px;
}
div.tr{
display: table-row;
border: 1px solid green;
}
div.td{
display: table-cell;
border: 1px solid blue;
vertical-align: middle;
}
.child{
border: 10px solid black;
}
design sketch

4、margin-top -50%
Code
// html
<body>
<div class="parent">
<div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div>
</div>
</body>
// css .parent{
height: 600px;
border: 1px solid red;
position: relative;
}
.child{
border: 1px solid green;
width: 300px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -150px;
height: 100px;
margin-top: -50px;
}
design sketch

5、translate -50%
Code
// html
<body>
<div class="parent">
<div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div>
</div>
</body>
// css .parent{
height: 600px;
border: 1px solid red;
position: relative;
}
.child{
border: 1px solid green;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
design sketch

6、absolute margin auto
Code
// html
<body>
<div class="parent">
<div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div>
</div>
</body>
// css .parent{
height: 600px;
border: 1px solid red;
position: relative;
}
.child{
border: 1px solid green;
position: absolute;
width: 300px;
height: 200px;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
design sketch

7、flex
Code
<!DOCTYPE html>
// html
<body>
<div class="parent">
<div class="child">
【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】【 Vertical center 】
</div>
</div>
</body>
// css .parent{
height: 600px;
border: 3px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.child{
border: 3px solid green;
width: 300px;
}
design sketch

边栏推荐
- 13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
- 【PyTorch预训练模型修改、增删特定层】
- Is investment and finance suitable for girls? What financial products can girls buy?
- 【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
- Principle of persistence mechanism of redis
- 一类恒等式的应用(范德蒙德卷积与超几何函数)
- redis主从模式
- leetcode:1200. Minimum absolute difference
- The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
- Unity Xlua MonoProxy Mono代理类
猜你喜欢

mmclassification 训练自定义数据
你做自动化测试为什么总是失败?

【云原生 | Kubernetes篇】Ingress案例实战(十三)

Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle

pytorch-权重衰退(weight decay)和丢弃法(dropout)

redis主从模式

【TFLite, ONNX, CoreML, TensorRT Export】

Redirection of redis cluster

Multi table operation - sub query
![[configuration method of win11 multi-user simultaneous login remote desktop]](/img/8f/eab81f089b236c4527a9866b2cfc25.png)
[configuration method of win11 multi-user simultaneous login remote desktop]
随机推荐
[deploy pytoch project through onnx using tensorrt]
Implementation of array hash function in PHP
Liunx prohibit Ping explain the different usage of traceroute
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
XML parsing
【上采样方式-OpenCV插值】
abap查表程序
12.(地图数据篇)cesium城市建筑物贴图
Redis master-slave mode
How does redis implement multiple zones?
codeforces每日5题(均1700)-第五天
【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
如何让你的产品越贵越好卖
POJ 3176 cow bowling (DP | memory search)
【TFLite, ONNX, CoreML, TensorRT Export】
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
Multi table operation - Auto Association query
pytorch-线性回归
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
【云原生 | Kubernetes篇】Ingress案例实战(十三)