1. 程式人生 > >H5C3--transform實現任何元素居中對齊

H5C3--transform實現任何元素居中對齊

flow head height translate width post back ive pos

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7 
 8        .box{
 9            width: 440px;
10            height: 500px;
11            background-color: #ccc;
12            overflow
: hidden; 13 position: relative; 14 } 15 .son{ 16 width: 200px; 17 height: 100px; 18 background-color: red; 19 margin: auto; 20 position: absolute; 21 /*定位:參照的是父容器*/ 22 left: 50%; 23 top
: 50%; 24 /*偏移:偏移中的百分比參照的是自身*/ 25 /*定位加變化是解決居中垂直對齊問題的神器、*/ 26 transform: translate(-50%,-50%); 27 } 28 </style> 29 </head> 30 <body> 31 <div class="box"> 32 <div class="son"></div> 33 </div> 34 35 </body>
36 </html>

技術分享圖片

H5C3--transform實現任何元素居中對齊