在一个div里如何控制内容垂直居中?
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>填充</title> <style type="text/css"> #box1{ display:table-cell; text-align:center; vertical-align:middle; width:100px; height:100px; border:1px solid red; } </style> </head> <body> <div id="box1">盒子1</div> </body> </html>
代码: html <div id="parent"> <div id="child">Content here</div> </div> css #parent {display: table;} #child { display: table-cell; vertical-align: middle; }
border: 1px solid #ccc;
display: table-cell;
height: 100px;
vertical-align: middle;
width: 100px;
text-align:center;
楼上这是控制水平居中,垂直居中可以设置子元素高度例如height:80px;然后再设置父元素line-height:80px;
text-align:center;