博客列表 >Chapter6 盒模型与元素位置与大小

Chapter6 盒模型与元素位置与大小

无关
无关原创
2020年06月29日 12:03:05570浏览

一、盒模型常用属性

1、效果图:

2、源码(属性说明一并附在源码中):

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>盒模型常用属性</title>
  7. <style>
  8. .box{
  9. width: 120px;
  10. height: 100px;
  11. }
  12. .box.one{
  13. /* 1.padding:内边距 */
  14. padding:40px;
  15. /* 2.margin:外边距 margin-top,margin-bottom */
  16. margin-bottom: 20px;
  17. /*3.border: 边框:=粗细+虚实+颜色 */
  18. border:10px solid rgb(49, 192, 44);
  19. /* 4.background-color: 背景色; */
  20. background-color: lightcoral;
  21. /* 5.background-clip:规定背景的绘制区域 它有三个值:*/
  22. /* background-clip: content-box;--背景被裁剪到内容框。 */
  23. /* background-clip:padding-box-- 背景被裁剪到内边距框。 */
  24. /* background-clip:border-box --背景被裁剪到边框盒。 */
  25. background-clip: content-box;
  26. }
  27. .box.two{
  28. padding:10px;
  29. margin-top: 40px;
  30. border:2px solid lightcoral;
  31. background-color: lightgreen;
  32. background-clip:padding-box;
  33. }.box.parent{
  34. width:300px;
  35. height: 200px;
  36. border:5px solid yellow;
  37. background-color: lightblue;
  38. /* 6.position:一旦一个元素被添加了position,且值非static,那么她就是定位元素 */
  39. /* position: static;--HTML 元素的默认值,即没有定位,遵循正常的文档流对象。 */
  40. /* position:fixed;--元素的位置相对于浏览器窗口是固定位置。即使窗口是滚动的它也不会移动: */
  41. /* position:relative;--相对定位元素的定位是相对其正常位置。 */
  42. /* position:absolute;--绝对定位的元素的位置相对于最近的已定位父元素,如果元素没有已定位的父元素,那么它的位置相对于<html>: */
  43. /* position: sticky;-- 基于用户的滚动位置来定位。 */
  44. position:relative;
  45. /* 相对定位是相对自己做了偏移,这个元素在文档流中的位置不释放 */
  46. left:30px;
  47. top:40px;
  48. }
  49. .son.no1{
  50. width: 100px;
  51. height: 140px;
  52. background-color:orange;
  53. /* position:absolute;--绝对定位:相对于其定位父级进行定位 */
  54. position:absolute;
  55. /* fixed-固定定位总是相对于body定位,忽略其定位的父级 */
  56. position:fixed;
  57. /* 相同的属性,优先级按顺序执行,后面的覆盖前面的,这3个position,最后一个有效 */
  58. /* position:absolute; */
  59. left: 200px;
  60. top:10px;
  61. }
  62. .son.no2{
  63. width: 100px;
  64. height: 140px;
  65. background-color:purple;
  66. /* position:absolute;--绝对定位:相对于其定位父级进行定位 */
  67. position:absolute;
  68. /* fixed-固定定位总是相对于body定位,忽略其定位的父级 */
  69. /* position:fixed; */
  70. /* 相同的属性,优先级按顺序执行,后面的覆盖前面的,这3个position,最后一个有效 */
  71. /* position:absolute; */
  72. right: 170px;
  73. bottom:10px;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <div class="box one"></div>
  79. <div class="box two"></div>
  80. <hr/>
  81. <div class="box parent">
  82. <div class="son no1"></div>
  83. <div class="son no2"></div>
  84. </div>
  85. </body>
  86. </html>

二、元素大小的重新计算: box-sizing的用法

1、效果图:



2、源码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>元素大小的计算</title>
  7. <style>
  8. /* 样式初始化 */
  9. .box1{
  10. margin: 0;
  11. padding:0;
  12. /*1. box-sizing 属性有3个值: */
  13. /* 1.1 box-sizing:border-box;元素的宽度和高度包括 padding 和 border */
  14. /* 1.2 box-sizing: content-box;指定元素的宽度和高度(最小/最大属性)适用于box的宽度和高度。
  15. 元素的填充和边框布局和绘制指定宽度和高度除外 */
  16. /* 1.3 box-sizing: inherit;指定 box-sizing 属性的值,应该从父元素继承 */
  17. box-sizing: border-box;
  18. /* 元素的宽度和高度包括 padding 和 border */
  19. }
  20. .box11{
  21. width: 100px;
  22. height: 80px;
  23. background-color:red ;
  24. border:4px dashed blue ;
  25. padding:10px;
  26. margin-bottom: 20px;
  27. /* 1.1 box-sizing:border-box;元素的宽度和高度包括了 padding 和 border */
  28. box-sizing: border-box;
  29. /* 2.background-clip:规定背景的绘制区域 它有三个值:*/
  30. /* 2.1 background-clip: content-box;--背景被裁剪到内容框。 */
  31. /* 2.2 background-clip:padding-box-- 背景被裁剪到内边距框。 */
  32. /* 2.3 background-clip:border-box --背景被裁剪到边框盒。 */
  33. background-clip:content-box;
  34. /* width :100=border4*2+padding10*2+contentbox72 ; */
  35. /* height:80=border4*2+padding10*2+contentbox52 ; */
  36. /* 背景颜色大小:72*52; */
  37. }
  38. .box12{
  39. width: 100px;
  40. height: 80px;
  41. background-color:red ;
  42. border:4px dashed blue ;
  43. padding:10px;
  44. margin-bottom: 20px;
  45. box-sizing: border-box;
  46. /* 2.2 background-clip:padding-box-- 背景被裁剪到内边距框。 */
  47. background-clip:padding-box;
  48. /* width :100=border4*2+padding10*2+contentbox72 ; */
  49. /* height:80=border4*2+padding10*2+contentbox52 ; */
  50. /* 背景颜色大小:(100-4*2)*(80-4*2)=92*72; */
  51. }
  52. .box13{
  53. width: 100px;
  54. height: 80px;
  55. background-color:red ;
  56. border:4px dashed blue ;
  57. padding:10px;
  58. margin-bottom: 10px;
  59. box-sizing: border-box;
  60. /* 2.3 background-clip:border-box --背景被裁剪到边框盒。 */
  61. background-clip:border-box;
  62. /* width :100=border4*2+padding10*2+contentbox72 ; */
  63. /* height:80=border4*2+padding10*2+contentbox52 ; */
  64. /* 背景颜色大小:100*80; */
  65. }
  66. .box21{
  67. width: 100px;
  68. height: 80px;
  69. background-color:orange;
  70. border:4px solid blue;
  71. padding: 20px;
  72. margin-bottom: 10px;
  73. box-sizing: content-box;
  74. background-clip:content-box
  75. /* width :=border4*2+padding20*2+contentbox100=148 ; */
  76. /* height:=border4*2+padding20*2+contentbox80=128 ; */
  77. /* 背景颜色大小:100*80; */
  78. }
  79. .box22{
  80. width: 100px;
  81. height: 80px;
  82. background-color: orange;
  83. border:4px dashed blue;
  84. padding: 20px;
  85. margin-bottom: 10px;
  86. box-sizing: content-box;
  87. background-clip:padding-box;
  88. /* width :=border4*2+padding20*2+contentbox100=148 ; */
  89. /* height:=border4*2+padding20*2+contentbox80=128 ; */
  90. /* 背景颜色大小:(100+20*2)*(80+20*2)=140*120; */
  91. }
  92. .box23{
  93. width: 100px;
  94. height: 80px;
  95. background-color:orange;
  96. border:4px dashed blue;
  97. padding: 20px;
  98. margin-bottom: 10px;
  99. box-sizing: content-box;
  100. background-clip:border-box;
  101. /* width :=border4*2+padding20*2+contentbox100=148 ; */
  102. /* height:=border4*2+padding20*2+contentbox80=128 ; */
  103. /* 背景颜色大小:(100+20*2+4*2)*(80+20*2+4*2)=148*128; */
  104. }
  105. .box31{
  106. width: 100px;
  107. height: 80px;
  108. background-color: lightgreen;
  109. border:4px dashed red;
  110. padding: 20px;
  111. margin-bottom: 10px;
  112. box-sizing: inherit;
  113. background-clip: content-box ;
  114. /* width :=border4*2+padding20*2+contentbox100=148 ; */
  115. /* height:=border4*2+padding20*2+contentbox80=128 ; */
  116. /* 背景颜色大小:100*80; */
  117. }
  118. .box32{
  119. width: 100px;
  120. height: 80px;
  121. background-color: lightgreen;
  122. border:4px dashed red;
  123. padding: 20px;
  124. margin-bottom: 10px;
  125. box-sizing: inherit;
  126. background-clip:padding-box;
  127. /* width :=border4*2+padding20*2+contentbox100=148 ; */
  128. /* height:=border4*2+padding20*2+contentbox80=128 ; */
  129. /* 背景颜色大小:(100+20*2)*(80+20*2)=140*120; */
  130. }
  131. .box33{
  132. width: 100px;
  133. height: 80px;
  134. background-color: lightgreen;
  135. border:4px dashed red;
  136. padding: 20px;
  137. margin-bottom: 10px;
  138. box-sizing: inherit;
  139. background-clip:border-box;
  140. /* width :=border4*2+padding20*2+contentbox100=148 ; */
  141. /* height:=border4*2+padding20*2+contentbox80=128 ; */
  142. /* 背景颜色大小:(100+20*2+4*2)*(80+20*2+4*2)=148*128; */
  143. }
  144. </style>
  145. </head>
  146. <body>
  147. <h3> 一、 box-sizing: border-box; 元素的宽度和高度包括 padding 和 border </h3>
  148. <p> box1: width :100=border4*2+padding10*2+contentbox72 ; </p>
  149. <p> box1: height:80=border4*2+padding10*2+contentbox52 ; </p>
  150. <br/>
  151. <div class="box1">
  152. <p>1. box11:背景颜色大小:72*52; </p>
  153. <div class="box11">box11 </div>
  154. <p>2. box12:背景颜色大小:(100-4*2)*(80-4*2)=92*72; </p>
  155. <div class="box12">box12</div>
  156. <p>3. box13: 背景颜色大小:100*80; </p>
  157. <div class="box13">box13</div>
  158. </div>
  159. <hr/>
  160. <h3> 二、 box-sizing: content-box;指定元素的宽度和高度(最小/最大属性)适用于box的宽度和高度。
  161. <br>
  162. 元素的填充和边框布局和绘制指定宽度和高度除外 */ </h3>
  163. <p> box2: width :=border4*2+padding20*2+contentbox100=148 ; </p>
  164. <p> box2: height:=border4*2+padding20*2+contentbox80=128 ; </p>
  165. <br>
  166. <div class="box2">
  167. 1. 背景颜色大小:100*80;
  168. <div class="box21">box21 </div>
  169. 2.背景颜色大小:(100+20*2)*(80+20*2)=140*120;
  170. <div class="box22">box22</div>
  171. 3.背景颜色大小:(100+20*2+4*2)*(80+20*2+4*2)=148*128;
  172. <div class="box23">box23</div>
  173. </div>
  174. <hr/>
  175. <h3>三、 box-sizing: inherit;指定 box-sizing 属性的值,应该从父元素继承</h3>
  176. <p> box3: width :=border4*2+padding20*2+contentbox100=148 ; </p>
  177. <p> box3: height:=border4*2+padding20*2+contentbox80=128 ; </p>
  178. <div class="box3">
  179. 1. 背景颜色大小:100*80;
  180. <div class="box31">box31 </div>
  181. 2. 背景颜色大小:(100+20*2)*(80+20*2)=140*120;
  182. <div class="box32">box32</div>
  183. 3.背景颜色大小:(100+20*2+4*2)*(80+20*2+4*2)=148*128;
  184. <div class="box33">box33</div>
  185. </div>
  186. </body>
  187. </html>

三、 定位元素的水平与垂直

1.1 水平居中:
  1. margin-left:auto;
  2. margin-right:auto;
1.2垂直居中-通过绝对定位来实现:
  1. position:absolute;
  2. top:0;
  3. left: 0;
  4. right:0;
  5. bottom: 0;
  6. margin: auto;

1.效果图

2.源码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>元素的水平与垂直定位</title>
  7. <style>
  8. .container{
  9. margin-top:60px;
  10. margin-left:20px;
  11. width: 300px;
  12. height: 300px;
  13. border:10px dashed orange;
  14. background-color: lightgreen;
  15. /* 转为定位元素 */
  16. position:relative;
  17. }
  18. .box1{
  19. width: 120px;
  20. height: 60px;
  21. border:5px dashed blue;
  22. background-color: yellow;
  23. color:red;
  24. /* 水平居中 */
  25. /* 让浏览器自动计算左右居中margin-left:auto; margin-right:auto; */
  26. margin-left:auto;
  27. margin-right:auto;
  28. }
  29. .box2{
  30. width: 160px;
  31. height: 80px;
  32. border:5px dashed yellow;
  33. background-color: lightsalmon;
  34. color:blue;
  35. /* 通过绝对定位来实现垂直居中 */
  36. position:absolute;
  37. /* 让当前元素绝对定位的上下文充满整个父级容器 */
  38. /* 左上角开始 */
  39. top:0;
  40. left: 0;
  41. /* 右下角结束 */
  42. right:0;
  43. bottom: 0;
  44. margin: auto;
  45. }
  46. form{
  47. width: 300px;
  48. height: 200px;
  49. background-color: lightpink;
  50. position: absolute;
  51. top:0;
  52. left: 0;
  53. right:0;
  54. bottom: 0;
  55. margin: auto;
  56. }
  57. form label{
  58. background-color: yellow;
  59. color:brown;
  60. }
  61. form input{
  62. background-color: lightgoldenrodyellow;
  63. color:red;
  64. }
  65. input:focus {
  66. background-color: rgb(39, 214, 62);
  67. }
  68. .btn{
  69. width: 90px;
  70. height: 40px;
  71. background-color: orangered;
  72. color:black;
  73. font-size: 1.2rem;
  74. margin-top: 10px;
  75. margin-left: 120px;
  76. }
  77. </style>
  78. <body>
  79. <div class="container">
  80. <div class="box1"> box1水平居中</div>
  81. <div class="box2">box2垂直居中</div>
  82. </div>
  83. <hr/>
  84. <form action="">
  85. <p>电话呼叫系统:</p>
  86. <label for="acode">区号:</label>
  87. <input type="area code" name="acode" id="acode"placeholder="3位数字" autofocus required />
  88. <br>
  89. <label for="telephone">电话号码:</label>
  90. <input type="text" name="phone" id="telephone" placeholder="8位数字" required/>
  91. <br>
  92. <button class="btn">拨打</button>
  93. </form>
  94. </body>
  95. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
2020-06-29 16:10:101楼
脑细胞组团阵亡,,,