当前位置:网站首页>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
边栏推荐
- Is investment and finance suitable for girls? What financial products can girls buy?
- Linux安装部署LAMP(Apache+MySQL+PHP)
- Splunk configuration 163 mailbox alarm
- [yolov5.yaml parsing]
- redis主从模式
- Vscode shortcut key
- Error modulenotfounderror: no module named 'cv2 aruco‘
- Programmers are involved and maintain industry competitiveness
- Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
- Codeforces Round #804 (Div. 2)
猜你喜欢
Principle of persistence mechanism of redis
【TFLite, ONNX, CoreML, TensorRT Export】
HiEngine:可媲美本地的云原生内存数据库引擎
[untitled]
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
ABAP table lookup program
11.(地图数据篇)OSM数据如何下载使用
【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
石油化工企业安全生产智能化管控系统平台建设思考和建议
随机推荐
How to protect user privacy without password authentication?
Is investment and finance suitable for girls? What financial products can girls buy?
Prevent browser backward operation
Halcon 模板匹配实战代码(一)
POJ 3176 cow bowling (DP | memory search)
Dynamic SQL of ibatis
Principle of redis cluster mode
Error modulenotfounderror: no module named 'cv2 aruco‘
COMSOL -- establishment of 3D graphics
COMSOL -- three-dimensional graphics random drawing -- rotation
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
redis主从模式
【TFLite, ONNX, CoreML, TensorRT Export】
【上采样方式-OpenCV插值】
Pytorch MLP
一次生产环境redis内存占用居高不下问题排查
谜语1
【 YOLOv3中Loss部分计算】
COMSOL -- 3D casual painting -- sweeping
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix