<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .demo { line-height: 44px; margin-bottom: 20px; text-align: center; background-color: #0078e7; color: #fff; } .flex-equal, .flex-center, .justify { display: -webkit-box; display: -ms-flexbox; display: -webkit-flex; display: flex; } .flex-center { -webkit-box-pack: center; -ms-flex-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-box-align: center; -ms-flex-align: center; -webkit-align-items: center; align-items: center; } .demo-center .children { background: #0078e7; color: #fff; width: 150px; line-height: 5; text-align: center; } .demo-center { border: 1px solid #ccc; margin: 20px; height: 200px; } .translate-center { position: relative; } .demo-center .children { background: #0078e7; color: #fff; width: 150px; line-height: 5; text-align: center; } .translate-center .children { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } .table-equal { display: table; table-layout: fixed; width: 100%; } .table-equal li { display: table-cell; } .container { width: 100%; height: 80px; background: #C2300B; padding-top:10px; text-align:center; } .center{ display:inline-block; border:2px solid #fff; } .center{ _display:inline; } /*针对ie6 hack*/ .center a{ float:left; border:1px solid #fff; padding:5px 10px; margin:10px; color:#fff; text-decoration:none; } #vc { display:table; background-color:#000; width:100%; height:200px; overflow:hidden; margin-left:50px; _position:relative; } #vci { vertical-align:middle; display:table-cell; text-align:center; _position:absolute; _top:50%; _left:50%; } #content { color:#fff; border:1px solid #fff; display:inline-block; _position:relative; _top:-50%; _left:-50%; } </style> </head> <body> <h2>flex居中</h2> <div class="flex-center demo-center"> <div class="children">子元素水平垂直居中</div> </div> <h2>translate居中</h2> <div class="translate-center demo-center"> <div class="children">子元素水平垂直居中子元素水平垂直居中</div> </div> <h2>div宽度不固定的div如何设置水平居中</h2> <div class="container"> <div class="center"> <a href="#">1</a><a href="#">2</a><a href="#">3</a> </div> </div> <h2>table居中高度不固定的div垂直居中</h2> <div id="vc"> <div id="vci"> <div id="content"> 我们垂直居中了,我们水平居中了,真的是可以居中的吗,<br /> 你信不信我反正是新了<br /> 年轻化互联网团队! </div> </div> </div> </body> </html>