实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>作业</title> <style type="text/css"> .box1 { width: 100px; height: 100px; background-color: lightblue; text-align: center; } .box1 a { line-height: 100px; } .box2 { width: 100px; height: 100px; background-color: pink; text-align: center; display: table-cell; vertical-align: middle; } .box3 { width: 100px; height: 100px; background-color: pink; display: table-cell; vertical-align: middle; } .box3 .child { width: 50px; height: 50px; background-color: lightblue; margin: auto; } .box4 { width: 100px; height: 100px; background-color: green; text-align: center; display: table-cell; vertical-align:bottom; } .box4 ul { margin: 0; padding: 0; } .box4 li { list-style: none; display: inline; } </style> </head> <body> 1.子元素是行内元素:如:a,span <br> a.水平居中:在父元素上设置: text-align:center;<br> b.垂直居中:在行内子元素上设置行高与父元素相同: line-height <div class="box1"> <a href="">我爱PHP</a> </div> <hr> 2. 子元素是多行内联文本 a.水平居中:父元素设置text-align:center b.垂直居中:父元素设置:display:table-cell;vertical-align:middle <div class="box2"> <span>我爱PHP</span><br> <span>php爱我</span> </div> <hr> 3. 子元素是块元素:<br> a.水平居中:子元素设置左右自动: margin: auto;<br> b.垂直居中:与多行内联文本处理方式一致:display:table-cell;vertical-align:middle <div class="box3"> <div class="child"></div> </div> <hr> 4. 子元素是不定宽的块元素:最常见的分页导航<br> a.水平居中:子元素转行内元素,父元素加:text-align:center <br> b.垂直居中:可给分页的ul加行高line-height=parent.height c.底边居中:更为常用,与多行内联文本垂直处理方式一致,vertical-align:bottom; <div class="box4"> <ul> <li><a href="">1</a></li> <li><a href="">2</a></li> <li><a href="">3</a></li> <li><a href="">4</a></li> <li><a href="">5</a></li> </ul> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
手抄: