PC固定寬高、通常使用maring負值進行居中
PC不固定寬高、通常使用relative和left進行html
下面三種方案日常都不多被使用的到 verticle-align: middle; display: table-cell; top:0; right:0; bottom:0; left:0;
移動端通常使用(PC方案在移動端均可以使用)web
transform: translate(-50%,-50%); display: flex; justify-content: center; align-items: center;
文本水平垂直居中flex
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文本垂直</title> <style> .xx_wrap { width: 500px; height: 500px; background-color: greenyellow; } .xx_wrap p { line-height: 500px; text-align: center; } </style> </head> <body> <div class="xx_wrap"> <p>文本垂直居中</p> </div> </body> </html>
一、 CSS3垂直居中方案flexbox
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>快級元素水平垂直居中(不固定寬高)</title> <style> * { margin: 0; padding: 0; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } ol, ul { list-style: none; } .box { position: relative; width: 500px; height: 500px; background-color: blueviolet; } .myUl { position: absolute; left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } </style> </head> <body> <div class="box"> <ul class="myUl"> transform: translate(-50%,-50%);居中方案 </ul> </div> </body> </html>
二、flex居中方案url
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>flex水平垂直居中</title> <style> * { margin: 0; padding: 0; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } ol, ul { list-style: none; } .box { width: 500px; height: 500px; background-color: #77BBDD; /** 各類版本兼容**/ display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; /** 垂直居中核心、兼容**/ -webkit-box-pack: center;//09版水平居中 -moz-box-pack: center; -ms-flex-pack: center;//過分版(混合版) -webkit-justify-content: center;//12版水平居中 justify-content: center; -webkit-box-align: center;//09版垂直居中 -moz-box-align: center; -ms-flex-align: center;//過分版(混合版) -webkit-align-items: center;//12版垂直居中 align-items: center; width: 0%;//低版本Android的flex-item沒法等分 display: block; /* 實則只需三行 display: flex; justify-content: center; align-items: center; */ } .box .div1 { width: 200px; height: 200px; background-color: red; } </style> </head> <body> <div class="box"> <div class="div1">flex水平垂直居中</div> </div> </body> </html>
一、快級元素水平垂直居中(固定寬高、最經常使用)code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>水平、垂直居中</title> <style> .xx_wrap { position: relative; width: 500px; height: 500px; background-color: greenyellow; } .xx_wrap p { width: 200px; height: 200px; background-color: red; position: absolute; left: 50%;top: 50%; margin-left: -100px; margin-top: -100px; } </style> </head> <body> <div class="xx_wrap"> <p>快級元素水平垂直居中(固定寬高)</p> </div> </body> </html>
四、快級元素水平垂直居中(不固定寬高)orm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>快級元素水平垂直居中(不固定寬高)</title> <style> * { margin: 0; padding: 0; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } ol, ul { list-style: none; } /* 定位 + 浮動 */ .myUl { float: left; position: relative; left: 50%; background-color: blueviolet; } .myUl li { left: -50%; position: relative; } /* 絕對定位方案 .box {position: relative;} .myUl {position: absolute;left: 50%;background-color: blueviolet;} .myUl li {left: -50%;position: relative;} */ </style> </head> <body> <div class="box"> <ul class="myUl"> <li>快級元素水平垂直居中(不固定寬高)</li> </ul> </div> </body> </html>
二、絕對定位居中(禁IE六、7)htm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>絕對定位水平垂直居中</title> <style> * { margin: 0; padding: 0; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } .wrap { width: 500px; height: 500px; background-color: chartreuse; position: relative; } .wrap .div1 { width: 50%; height: 50%; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; background-color: yellow; } </style> </head> <body> <div class="wrap"> <div class="div1">絕對定位水平垂直居中</div> </div> </body> </html>
三、非快級元素水平垂直居中(vertical-align: middle禁IE六、七、8)it
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>非快級元素水平垂直居中</title> <style> * { margin: 0; padding: 0; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } ol, ul { list-style: none; } .box { width: 500px; height: 500px; background-color: #77BBDD; text-align: center; } .box img { /* 若爲快級元素:則修改成display: inilne-block;便可 */ width: 200px; height: 200px; background-color: red; vertical-align: middle; } .box::after { content: ''; width: 5px; line-height: 500px; background-color: yellow; } </style> </head> <body> <div class="box"> <img src="http://img0.bdstatic.com/img/image/2016ss1.jpg" alt=""/> </div> </body> </html>
五、table-cell水平垂直居中io
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>絕對定位水平垂直居中</title> <style> * { margin: 0; padding: 0; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } .wrap { width: 500px; height: 500px; background-color: chartreuse; display: table; } .wrap .div1 { display: table-cell; vertical-align: middle; } .wrap .div1 p { width: 50%; height: 50%; margin: 0 auto; background-color: burlywood; } </style> </head> <body> <div class="wrap"> <div class="div1"> <p>絕對定位水平垂直居中</p> </div> </div> </body> </html>