实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>CSS四种对齐方式</title> <style type="text/css"> /*单行文本居中*/ .box1{ width: 200px; height: 200px; background-color: lightblue;/*水平居中*/ text-align: center; } /*多行文本居中*/ .box2{ width: 200px; height: 200px; background-color: #FF7300; text-align: center;/*水平居中*/ display: table-cell;/*把盒子变成表格*/ vertical-align: middle;/*垂直居中*/ } .box2 span{ width: 100px; height: 100px; line-height: 200px;/*垂直居中*/ } /*块级元素居中*/ .box3{ width: 200px; height: 200px; background-color: green; text-align: center;/*水平居中*/ display: table-cell;/*把盒子变成表格*/ vertical-align: middle;/*垂直居中*/ } .box3 p{ width:100px; margin: auto; } </style> </head> <body> <div class="box1"> <a href="">PHP中文网</a> </div> <div class="box2"> <span>PHP世界上最好的语言</span><br> <span>PHP-China</span> </div> <div class="box3"> <p>hello world</p> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例