如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中;
如果 .parent 的 height 写死了,就很难把 .child 居中,以下是垂直居中的方法。
忠告:能不写 height 就千万别写 height。
1、table自带功能
代码
1 2 3 4 5 6 7 8 9 10
| // html <body> <table class="parent"> <tr> <td class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </td> </tr> </table> </body>
|
1 2 3 4 5 6 7 8
| .parent{ border: 1px solid red; height: 600px; }
.child{ border: 1px solid green; }
|
效果图
2、100% 高度的 after before 加上 inline block
代码
1 2 3 4 5 6 7 8
| // html <body> <div class="parent"> <span class=before></span><div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div><span class=after></span> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| .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; }
|
效果图
优化版
1 2 3 4 5 6 7 8
| // html <body> <div class="parent"> <div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| .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 装成 table
代码
1 2 3 4 5 6 7 8 9 10
| // html <body> <div class="table"> <div class="td"> <div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div> </div> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // css div.table{ display: table; border: 1px solid red; height: 600px; } div.td{ display: table-cell; border: 1px solid blue; vertical-align: middle; } .child{ border: 10px solid black; }
|
效果图
4、margin-top -50%
代码
1 2 3 4 5 6 7 8
| // html <body> <div class="parent"> <div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| // 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; }
|
效果图
5、translate -50%
代码
1 2 3 4 5 6 7 8
| // html <body> <div class="parent"> <div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| // 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%); }
|
效果图
6、absolute margin auto
代码
1 2 3 4 5 6 7 8
| // html <body> <div class="parent"> <div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| // 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; }
|
效果图
7、flex
代码
1 2 3 4 5 6 7 8 9
| <!DOCTYPE html> // html <body> <div class="parent"> <div class="child"> 【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】【垂直居中】 </div> </div> </body>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| // css .parent{ height: 600px; border: 3px solid red; display: flex; justify-content: center; align-items: center; } .child{ border: 3px solid green; width: 300px; }
|
效果图