导读
页面布局三大核心:
- 盒子模型
- 浮动
- 定位
所谓盒子模型,就是把html页面中的布局元素看作是一个盒子,也就是装内容的容器。盒子模型由边框(border)、外边距(margin)、内边距(padding)和实际内容(content)组成。
边框
属性
|
作用
|
border-width
|
边框粗细
|
border-style
|
边框样式
|
border- color
|
边框颜色
|
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>盒子模型边框</title> <style> div { width: 300px; height: 200px; border-width: 5px; /*solid 实线 dashed 虚线 dotted 点线 */ border-style: solid; border-color: blue; border-top: 1px solid red; /* border:5px solid blue; */ /* border-top: 5px solid red; border-bottom: 10px solid green; */ } </style> </head> <body> <div></div> </body> </html>结果演示
表格边框
border-collapse表示相邻边框合并到一起
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>综合案例</title> <style> table { width: 500px; height: 200px; } table, td, th { border: 1px solid blue; border-collapse: collapse; } </style> </head> <body> <table align="center" border="1" cellspacing="0"> <tr> <th>排名</th> <th>关键词</th> <th>趋势</th> <th>今日搜索</th> <th>最近七日</th> </tr> <tr> <td>1</td> <td>鬼吹灯</td> <td>345</td> <td>123</td> <td> <a href="https://www.baidu.com" target="_blank">百度</a> <a href="https://www.sougou.com" target="_blank">搜狗</a> </td> </tr> </table> </body> </html>结果演示
边框会使盒子的实际大小变大
如果保证盒子跟效果图大小保持一致,则让width/height减去多出来的大小即可。
内边距
属性
|
作用
|
padding-left
|
左内边距
|
padding-right
|
右内边距
|
padding-top
|
上内边距
|
padding-bottom
|
下内边距
|
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>内边距</title> <style> div { width: 300px; height: 300px; border: 10px solid green; padding-left: 30px; } </style> </head> <body> <div> 作者目前是一名大三学生,大学期间参加了校、省组织的一些计算机竞赛,分别获得过各竞赛二等奖、三等奖;拿过特等奖学金;在学校课程计划没有开始的情况下,凭自学一次拿下计算机二级Java优秀等级证书。 </div> </body> </html>结果演示
padding复合写法
内边距会使盒子的实际大小变大
新浪导航栏练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>新浪导航栏</title> <style> .nav { border-top: 3px solid #ff8500; border-bottom: 1px solid #969aa1; height: 41px; background-color: #fcfcfc; font-size: 12px; line-height: 41px; } .nav a { display: inline-block; text-decoration: none; color: #4c4c4c; /*用padding撑开盒子,不适合给宽度,因为文字个数不一样*/ padding-left: 20px; padding-right: 20px; } .nav a:hover { color: #ff8500; background-color: #dad5d5; background-size: 40px; } </style> </head> <body> <div class="nav"> <a href="#">新浪导航</a> <a href="#">手机新浪网</a> <a href="#">移动客户端</a> <a href="#">微博</a> </div> </body> </html>结果演示
padding不会撑开盒子的情况
- 如果盒子没有指定width或者height属性,则此时padding不会撑开盒子大小。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>padding不会影响盒子大小的情况</title> <style> h1 { height: 200px; background-color: pink; padding: 30px; } div { width: 300px; height: 100px; background-color: red; } div p { /* width: 100%; */ padding: 30px; background-color: blue; } </style> </head> <body> <h1></h1> <div> <p></p> </div> </body> </html>结果演示
外边距
定义:控制盒子与盒子之间的距离
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>外边距</title> <style> div { width: 200px; height: 200px; background-color: green; } .bj { margin-bottom: 20px; } </style> </head> <body> <div class="bj">1</div> <div>2</div> </body> </html>
外边距复合写法和内边距写法一样并且margin不会撑开盒子
外边距典型应用
外边距可以让块级盒子水平居中,但是必须满足两个条件
- 盒子必须指定了宽度(width)
- 盒子左右的外边距都设置为auto
行内元素或者行内块元素水平居中给其父元素添加text-align:center 即可。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>块级盒子水平居中</title> <style> .header { width: 900px; height: 200px; background-color: green; /*上下边距为0,左右边距为auto*/ margin: 0 auto; /*行内元素或者行内块元素*/ text-align: center; } </style> </head> <body> <div class="header"> <span>里面的文字也要居中</span> </div> </body> </html>结果演示
外边距合并
问题1:
- 当上下相邻的两个块元素(兄弟关系)相遇时,如果上面的元素有下外边距margin-bottom,下面的元素有上外边距margin-top,则他们之间的垂直间距不是他们间距之和,而是去两个值中的最大值最为外边距。
解决方案:
- 尽量只给一个盒子添加margin值
问题2(常见):
- 对于两个嵌套关系(父子关系)的块元素,父元素和子元素同时有上外边距时,父元素会塌陷较大的外边距值。
解决方案:
- 给父元素添加上边框或者上内边距(推荐)
- 给父元素添加overflow:hidden
清除内外边距
网页元素很多都带有默认的内外边距,而且不同浏览器默认的也不一致,因此在布局前,首先要清除网页元素的内外边距。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=\, initial-scale=1.0"> <title>清除内外边距</title> <style> /*这句话也是我们css的第一行代码*/ * { /* 清除外边距 */ margin: 0; /* 清除内边距 */ padding: 0; } </style> </head> <body> 123 <ul> <li>sfas</li> </ul>结果演示
行内元素尽量只设置左右的内外边距
综合案例
电商评价综合案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>电商评价综合案例</title> <style> .shop { width: 300px; height: 415px; background-color: rgb(255, 166, 0); margin: 160px auto; } .pingjia { font-size: 14px; margin: 20px; height: 20px; } .shuliang { color: rgb(133, 138, 143); font-size: 12px; margin-left: 20px; margin-top: 50px; } .lianjie { font-size: 12px; text-decoration: none; color: black; margin: 20px; } .jiage { color: red; font-size: 12px; } .shuxian { margin: 2px; color: aliceblue; font-size: 12px; color: rgb(133, 138, 143); } </style> </head> <body> <div class="shop"> <img src="images/a1.jpg" width="100%"> <p class="pingjia">快递牛,整体不错蓝牙可以说秒连。红米给力</p> <p class="shuliang"> 来自于 117384232 的评价 </p> <a class="lianjie" href="#">Redmi AirDots真无线蓝...</a> <span class="shuxian">|</span> <span class="jiage">99.9元</span> </div> </body> </html>结果演示
快报模块
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>快报模块</title> <style> * { /* 清除外边距 */ margin: 0; /* 清除内边距 */ padding: 0; } .hezi { width: 250px; height: 165px; border: 1px solid rgb(156, 148, 148); margin: 120px auto; } .biaoti { font-weight: normal; text-align: left; font-size: 14px; padding: 8px 15px; } .libiao { border-top: 1px dotted rgb(156, 148, 148); } .lianjie { text-decoration: none; color: rgb(99, 103, 105); font-size: 12px; display: block; margin: 7px 0; text-indent: 2em; } .lianjie:hover { text-decoration: underline; } </style> </head> <body> <div class="hezi"> <p class="biaoti">品优购快报</p> <div class="libiao"> <a class="lianjie" href="#">【特惠】爆款耳机5折秒!</a> <a class="lianjie" href="#">【特惠】母亲节,健康好礼低至5折!</a> <a class="lianjie" href="#">【特惠】爆款耳机5折秒!</a> <a class="lianjie" href="#">【特惠】9.9元洗100张照片!</a> <a class="lianjie" href="#">【特惠】长虹智能空调立省1000</a> </div> </div> </body> </html>结果演示