实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>0323作业:CSS四种居中对齐方式</title> <style type="text/css"> body { background-image: url(http://www.mi-888.com/php/zuoye/20180323/bg.jpg); background-repeat: no-repeat;/*背景图不重复*/ background-attachment: fixed;/*背景图像固定*/ background-size:cover;/*背景图铺满全屏*/ } .box1 { background-color: pink; width:200px; height:200px; line-height:200px; text-align: center; margin-bottom: 10px; } .box2 { background-color: lightblue; width:200px; height:200px; text-align: center;/*水平居中*/ display: table-cell;/*将当前块显示方式改为表格单元格方式*/ vertical-align: middle;/*设置单元格内元素垂直居中*/ } .box3 { background-color: orange; width:200px; height:200px; text-align: center; display: table-cell; vertical-align: middle; } .box3-1 { background-color:yellow; width: 100px; height: 100px; margin: auto;/*浏览器自动计算*/ line-height: 100px; text-align: center; } .box4 { background-color:lightgreen; width: 200px; height:200px; text-align: center; display: table-cell; vertical-align: bottom; } .box4 ul { padding: 0; } .box4 li { list-style: none; display: inline; } </style> </head> <body> 一、单行文本 <div class="box1"> <a href="">PHP中文网</a> </div> <hr> 二、多行文本 <div class="box2"> <a href="">PHP中文网</a><br> <span>www.php.cn</span> </div> <hr> 三、嵌套DIV <div class="box3"> <div class="box3-1"> <a href="">PHP中文网</a> </div> </div> <hr> 四、底部分页导航 <div class="box4"> <ul> <li><a href="">首页</a></li> <li><a href="">2</a></li> <li><a href="">3</a></li> <li><a href="">4</a></li> <li><a href="">尾页</a></li> </ul> </div> <hr> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
手抄代码: