1. 盒子大小

默认情况下,盒子可见框的大小由内容区、内边距和边框共同决定(不包括margin);
**box-sizing**用来设置盒子尺寸的计算方式(设置width和height的作用范围)

  1. .box {
  2. width: 200px;
  3. height: 200px;
  4. background-color: yellow;
  5. border: 10px red solid;
  6. /* box-sizing: content-box; */
  7. box-sizing: border-box;
  8. }

可选值:

  • content-box默认值,宽度和高度用来设置内容区的大小

    (说白了在这种模式下,用padding和border都是往外扩)
    06.盒模型补充及田径场实战 - 图1

  • border-box 宽度和高度用来设置整个盒子可见框的大小,widthheight包括指的是内容区、内边距和边框的总大小

    1. (用paddingborder都是向里占地)<br />![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210523192848.png#clientId=u78547e9c-11bf-4&crop=0&crop=0&crop=1&crop=1&height=125&id=FmM5g&originHeight=201&originWidth=249&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u840e3517-b397-4b23-927c-d5b15d9fbff&title=&width=155)

2. 轮廓

**outline**用来设置元素的轮廓线,用法和**border**一模一样
轮廓outline和边框border不同点是,轮廓不会影响到可见框的大小,直接在外面加上覆盖的,border是加在盒模型外面挤出去的;

  1. /*border边框*/
  2. .box {
  3. width: 200px;
  4. height: 200px;
  5. background-color: yellow;
  6. border: 10px red solid;
  7. }
  8. /*outline轮廓*/
  9. .box {
  10. width: 200px;
  11. height: 200px;
  12. background-color: yellow;
  13. outline: 10px red solid;
  14. }

06.盒模型补充及田径场实战 - 图2 06.盒模型补充及田径场实战 - 图3
但是我们一般不会使用轮廓去布局,而是常用于鼠标移动上显示这种场景

  1. .box:hover {
  2. outline: 10px red solid;
  3. }
  4. /*从效果动态图也可以很清晰地看出,outline属性并没有改变盒子的大小布局,直接描了个边*/

06.盒模型补充及田径场实战 - 图4

3. 阴影

你可以设置多个由逗号分隔的效果 一个盒状阴影由相对于元素的X和Y的偏移量、模糊和扩散半径以及颜色来描述

**box-shadow**用来设置元素的阴影效果,阴影不会影响页面布局

  • 第一个值-水平偏移量:设置阴影的水平位置
    • 正值向右移动,负值向左移动
  • 第二个值-垂直偏移量:设置阴影的垂直位置
    • 正值向下移动,负值向上移动
  • 第三个值-阴影的模糊半径
  • 第四个值-阴影的颜色 ```css .box { width: 200px; height: 200px; background-color: yellow; /阴影轮廓的设置/ box-shadow: 10px 10px orange; box-shadow: 10px 10px 5px orange; box-shadow: 10px 10px 5px rgba(0, 0, 0, .2);

}

  1. ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210523200020.png#clientId=u78547e9c-11bf-4&crop=0&crop=0&crop=1&crop=1&height=75&id=zau4a&originHeight=231&originWidth=236&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=uf6a2c10a-7122-49ec-b6c4-a5edcc92949&title=&width=77) ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210523200056.png#clientId=u78547e9c-11bf-4&crop=0&crop=0&crop=1&crop=1&height=76&id=kPZEy&originHeight=238&originWidth=241&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u6c951aea-a2da-468d-839b-4cf672df8db&title=&width=77) ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210523201711.png#clientId=u78547e9c-11bf-4&crop=0&crop=0&crop=1&crop=1&height=77&id=fUGdk&originHeight=226&originWidth=228&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u45a07972-742c-4222-b976-e0b3a84e9ef&title=&width=78)
  2. <a name="606a2d65"></a>
  3. ## 4. 圆角
  4. > `border-radius`属性使一个元素的外边框边缘的角变圆
  5. > 你可以设置一个半径来做圆角,或者设置两个半径来做椭圆角
  6. `**border-radius**`** 用来设置圆角,圆角设置的是圆的半径大小**
  7. - `border-top-left-radius`左上
  8. - `border-top-right-radius`右上
  9. - `border-bottom-left-radius`左下
  10. - `border-bottom-right-radius`右下
  11. `border-radius` 可以分别指定四个角的圆角
  12. - 四个值:`左上` `右上` `右下` `左下`
  13. - 三个值:`左上` `右上/左下` `右下`
  14. - 两个值:`左上/右下` `右上/左下`
  15. - 一个值:`左上/右上/右下/左下`
  16. 这里同样不需要死记硬背,只要记住遵循顺时针方向和矩形中心点对称原则<br />与`border`不同的是,`border`是从`上`开始顺时针设置,而圆角是从`左上`开始
  17. ```css
  18. border-radius: 20px;
  19. border-radius:20px 40px;

06.盒模型补充及田径场实战 - 图5

  1. border-top-right-radius: 50px 100px;

06.盒模型补充及田径场实战 - 图6

原理很简单,就是绘制正方形,并将四个圆角半径设置为正方形的一半,或者直接用百分比表示

  1. .box {
  2. width: 200px;
  3. height: 200px;
  4. background-color: yellow;
  5. border-radius: 50%;
  6. }

06.盒模型补充及田径场实战 - 图7

椭圆

只需要对上述样式对一点点的改动,设置**width****height**属性不相等即可

  1. .box {
  2. width: 300px;
  3. height: 200px;
  4. background-color: yellow;
  5. border-radius: 50%;
  6. }

06.盒模型补充及田径场实战 - 图8

5. 田径场实战

  1. <div class="box1">
  2. <div class="box2">
  3. <div class="box3">
  4. <div class="box4">
  5. <div class="box5">
  6. <div class="box6">
  7. <div class="box7">
  8. <div class="box8">
  9. <div class="boxRect">
  10. <div class="boxCirc"></div>
  11. <div class="boxRectLeft1"></div>
  12. <div class="boxRectRight1"></div>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  1. .box1 {
  2. background-color: #DA251E;
  3. /* 这里使用css的一个表达式,方便加减乘除计算 */
  4. width: calc(1719.2px/2 + 360px*2 - 12.2px*0*2);
  5. height: calc(360px*2 - 12.2px*0*2);
  6. margin: 100px auto;
  7. border: 0.5px white solid;
  8. /* 圆角 */
  9. border-radius: calc(360px - 12.2px*0) / 50%;
  10. /* 盒子 */
  11. box-sizing: border-box;
  12. }
  13. .box2 {
  14. width: calc(1719.2px/2 + 360px*2 - 12.2px*1*2);
  15. height: calc(360px*2 - 12.2px*1*2);
  16. margin: 12.2px auto;
  17. border: 0.5px white solid;
  18. border-radius: calc(360px - 12.2px*1) / 50%;
  19. box-sizing: border-box;
  20. }
  21. .box3 {
  22. width: calc(1719.2px/2 + 360px*2 - 12.2px*2*2);
  23. height: calc(360px*2 - 12.2px*2*2);
  24. margin: 12.2px auto;
  25. border: 0.5px white solid;
  26. border-radius: calc(360px - 12.2px*2) / 50%;
  27. box-sizing: border-box;
  28. }
  29. .box4 {
  30. width: calc(1719.2px/2 + 360px*2 - 12.2px*3*2);
  31. height: calc(360px*2 - 12.2px*3*2);
  32. margin: 12.2px auto;
  33. border: 0.5px white solid;
  34. border-radius: calc(360px - 12.2px*3) / 50%;
  35. box-sizing: border-box;
  36. }
  37. .box5 {
  38. width: calc(1719.2px/2 + 360px*2 - 12.2px*4*2);
  39. height: calc(360px*2 - 12.2px*4*2);
  40. margin: 12.2px auto;
  41. border: 0.5px white solid;
  42. border-radius: calc(360px - 12.2px*4) / 50%;
  43. box-sizing: border-box;
  44. }
  45. .box6 {
  46. width: calc(1719.2px/2 + 360px*2 - 12.2px*5*2);
  47. height: calc(360px*2 - 12.2px*5*2);
  48. margin: 12.2px auto;
  49. border: 0.5px white solid;
  50. border-radius: calc(360px - 12.2px*5) / 50%;
  51. box-sizing: border-box;
  52. }
  53. .box7 {
  54. width: calc(1719.2px/2 + 360px*2 - 12.2px*6*2);
  55. height: calc(360px*2 - 12.2px*6*2);
  56. margin: 12.2px auto;
  57. border: 0.5px white solid;
  58. border-radius: calc(360px - 12.2px*6) / 50%;
  59. box-sizing: border-box;
  60. }
  61. .box8 {
  62. background-color: #00923F;
  63. width: calc(1719.2px/2 + 360px*2 - 12.2px*7*2);
  64. height: calc(360px*2 - 12.2px*7*2);
  65. margin: 12.2px auto;
  66. border: 0.5px white solid;
  67. border-radius: calc(360px - 12.2px*7) / 50%;
  68. box-sizing: border-box;
  69. }
  70. .boxRect {
  71. width: calc(1719.2px/2);
  72. height: calc(360px*2 - 12.2px*7*2 - 10px);
  73. margin: 5px auto;
  74. border: 0.5px white solid;
  75. box-sizing: border-box;
  76. }
  77. .boxCirc {
  78. width: 100px;
  79. height: 100px;
  80. margin: calc((360px*2 - 12.2px*7*2 - 10px - 100px)/2) auto;
  81. border: 0.5px white solid;
  82. border-radius: 50%;
  83. box-sizing: border-box;
  84. }
  85. .boxRectLeft1 {
  86. width: 100px;
  87. height: 200px;
  88. margin-top: calc(-360px*2/2 + 12.2px*7*2/2 + 10px - 200px/2);
  89. border: 0.5px white solid;
  90. box-sizing: border-box;
  91. }
  92. .boxRectRight1 {
  93. width: 100px;
  94. height: 200px;
  95. margin-top: calc(-360px*2/2 + 12.2px*7*2/2 + 10px + 50px);
  96. margin-left: calc(1719.2px/2 - 100px);
  97. border: 0.5px white solid;
  98. box-sizing: border-box;
  99. }

效果图

06.盒模型补充及田径场实战 - 图9

绿茵足球场完善

学完了浮动,我们终于可以继续完善绿茵足球场了

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>田径场</title>
  8. <style>
  9. /* 公共部分 */
  10. .box1,
  11. .box2,
  12. .box3,
  13. .box4,
  14. .box5,
  15. .box6,
  16. .box7,
  17. .box8,
  18. .boxRect,
  19. .boxColLine,
  20. .boxRectLeft1,
  21. .boxRectLeft2,
  22. .boxCircLeft,
  23. .boxCirc,
  24. .boxCircRight,
  25. .boxRectRight1,
  26. .boxRectRight2,
  27. .football {
  28. box-sizing: border-box;
  29. border: 0.5px white solid;
  30. }
  31. .box2,
  32. .box3,
  33. .box4,
  34. .box5,
  35. .box6,
  36. .box7,
  37. .box8 {
  38. margin: 12.2px auto;
  39. }
  40. .boxRectLeft1,
  41. .boxRectLeft2,
  42. .boxCircLeft {
  43. float: left;
  44. /* 去除左边框 */
  45. border-left: none;
  46. }
  47. .boxCircRight,
  48. .boxRectRight1,
  49. .boxRectRight2 {
  50. float: right;
  51. /* 去除右边框 */
  52. border-right: none;
  53. }
  54. /* ==========田径场========== */
  55. .box1 {
  56. background-color: #DA251E;
  57. width: calc(1719.2px/2 + 360px*2 - 12.2px*0*2);
  58. height: calc(360px*2 - 12.2px*0*2);
  59. margin: 100px auto;
  60. border-radius: calc(360px - 12.2px*0) / 50%;
  61. }
  62. .box2 {
  63. width: calc(1719.2px/2 + 360px*2 - 12.2px*1*2);
  64. height: calc(360px*2 - 12.2px*1*2);
  65. border-radius: calc(360px - 12.2px*1) / 50%;
  66. }
  67. .box3 {
  68. width: calc(1719.2px/2 + 360px*2 - 12.2px*2*2);
  69. height: calc(360px*2 - 12.2px*2*2);
  70. border-radius: calc(360px - 12.2px*2) / 50%;
  71. }
  72. .box4 {
  73. width: calc(1719.2px/2 + 360px*2 - 12.2px*3*2);
  74. height: calc(360px*2 - 12.2px*3*2);
  75. border-radius: calc(360px - 12.2px*3) / 50%;
  76. }
  77. .box5 {
  78. width: calc(1719.2px/2 + 360px*2 - 12.2px*4*2);
  79. height: calc(360px*2 - 12.2px*4*2);
  80. border-radius: calc(360px - 12.2px*4) / 50%;
  81. }
  82. .box6 {
  83. width: calc(1719.2px/2 + 360px*2 - 12.2px*5*2);
  84. height: calc(360px*2 - 12.2px*5*2);
  85. border-radius: calc(360px - 12.2px*5) / 50%;
  86. }
  87. .box7 {
  88. width: calc(1719.2px/2 + 360px*2 - 12.2px*6*2);
  89. height: calc(360px*2 - 12.2px*6*2);
  90. border-radius: calc(360px - 12.2px*6) / 50%;
  91. }
  92. .box8 {
  93. background-color: #00923F;
  94. width: calc(1719.2px/2 + 360px*2 - 12.2px*7*2);
  95. height: calc(360px*2 - 12.2px*7*2);
  96. border-radius: calc(360px - 12.2px*7) / 50%;
  97. }
  98. /* ==========绿茵足球场========== */
  99. .boxRect {
  100. width: calc(1719.2px/2);
  101. height: calc(360px*2 - 12.2px*7*2 - 10px);
  102. margin: 5px auto;
  103. }
  104. .boxRectLeft1 {
  105. width: 100px;
  106. height: 200px;
  107. margin-top: calc(360px*2/2 - 12.2px*7*2/2 - 10px/2 - 200px/2);
  108. }
  109. .boxRectLeft2 {
  110. width: 50px;
  111. height: 100px;
  112. margin-top: calc(200px/2 - 100px/2);
  113. }
  114. .boxCircLeft {
  115. width: 50px;
  116. height: 100px;
  117. margin-top: calc(360px*2/2 - 12.2px*7*2/2 - 10px/2 - 100px/2);
  118. border-radius: 0 100px 100px 0;
  119. }
  120. .boxCirc {
  121. float: left;
  122. width: 100px;
  123. height: 100px;
  124. margin: calc(360px*2/2 - 12.2px*7*2/2 - 10px/2 - 100px/2) auto;
  125. margin-left: calc(1719.2px/2/2 - 100px - 50px - 100px/2);
  126. border-radius: 50%;
  127. }
  128. .boxCircRight {
  129. width: 50px;
  130. height: 100px;
  131. margin-top: calc(360px*2/2 - 12.2px*7*2/2 - 10px/2 - 100px/2);
  132. border-radius: 100px 0 0 100px;
  133. }
  134. .boxRectRight1 {
  135. width: 100px;
  136. height: 200px;
  137. margin-top: calc(360px*2/2 - 12.2px*7*2/2 - 10px/2 - 200px/2);
  138. }
  139. .boxRectRight2 {
  140. width: 50px;
  141. height: 100px;
  142. margin-top: calc(200px/2 - 100px/2);
  143. }
  144. .boxColLine {
  145. width: 0;
  146. height: 100%;
  147. margin-left: calc(1719.2px/2/2);
  148. /* 边框样式 */
  149. border: 0.25px white dashed;
  150. }
  151. /* ==========足球========== */
  152. .football {
  153. float: right;
  154. width: 10px;
  155. height: 10px;
  156. background-color: black;
  157. margin: 100px;
  158. border-radius: 50%;
  159. }
  160. </style>
  161. </head>
  162. <body>
  163. <div class="box1">
  164. <div class="box2">
  165. <div class="box3">
  166. <div class="box4">
  167. <div class="box5">
  168. <div class="box6">
  169. <div class="box7">
  170. <div class="box8">
  171. <div class="boxRect">
  172. <div class="boxRectLeft1">
  173. <div class="boxRectLeft2"></div>
  174. </div>
  175. <div class="boxCircLeft"></div>
  176. <div class="boxCirc"></div>
  177. <div class="boxRectRight1">
  178. <div class="boxRectRight2"></div>
  179. </div>
  180. <div class="boxCircRight"></div>
  181. <!-- 足球 -->
  182. <div class="football"></div>
  183. <div class="boxColLine"></div>
  184. </div>
  185. </div>
  186. </div>
  187. </div>
  188. </div>
  189. </div>
  190. </div>
  191. </div>
  192. </div>
  193. </body>
  194. </html>

这次主要的改动如下:

  • 提取公共css代码
  • 使用float属性进行布局
  • 删除重叠部分边框样式(叠加之后颜色会变粗,这里去掉同一侧的边框样式)

不过需要注意的是由于boxColLine不是float元素,应该放置最下方
这样可以利用浮动的特点,防止对布局产生影响
最终效果

06.盒模型补充及田径场实战 - 图10