博客列表 >盒模型计算方式及伪类选择器灵活使用与初识媒体查询

盒模型计算方式及伪类选择器灵活使用与初识媒体查询

超超多喝水
超超多喝水原创
2021年09月25日 12:10:12508浏览

盒模型计算方式及伪类选择器灵活使用与初识媒体查询

盒模型计算方式

盒模型五大常用属性:

  • width
  • height
  • border
  • margin
  • padding

默认情况下,给一个盒模型设置了样式后,盒模型默认状态下的计算方式应该是 w3c 标准盒子计算方式,即: 实际宽度=width+height+border+padding,如:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>box-sizing</title>
  8. <style>
  9. .box {
  10. width: 200px;
  11. height: 200px;
  12. padding: 10px;
  13. border: 10px dashed;
  14. background-color: hotpink;
  15. background-clip: content-box;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="box"></div>
  21. </body>
  22. </html>

该 div 的盒子的实际宽高为 width 或 height 的 200+左右或上下 padding 的 10×2+左右或上下的 border10×2,最终值就为(200+10×2+10×2)=240。

标准盒子

但是按照一般习惯,也为了方便布局,应该是 width 的实际宽度就是 width 的宽度,height 的实际高度就是 height 的高度,此时可以使用box-sizing: border-box;来限制 width 的内容区根据 border 跟 padding 自适应,如:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>box-sizing</title>
  8. <style>
  9. .box {
  10. width: 200px;
  11. height: 200px;
  12. padding: 10px;
  13. border: 10px dashed;
  14. background-color: hotpink;
  15. background-clip: content-box;
  16. box-sizing: border-box;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="box"></div>
  22. </body>
  23. </html>

此时实际宽高就变成了 200,内容区变成了 width 或 height 的 200-左右或上下 padding 的 10×2-左右或上下的 border10×2,最终内容区的值就为(200-10×2-10×2)=160。

border-box

伪类选择器灵活使用

常用伪类选择器 注释
:nth-of-type() 分组匹配,在匹配之前将元素根据类型进行分组后再匹配,常用于选择同组单个元素或者从上往下选择
:nth-last-of-type() 分组匹配,在匹配之前将元素根据类型进行分组后再匹配,常用于选择同组单个元素或者从下往上选择
:first-of-type 常用于选择第一个元素
:last-of-type 常用于选择最后一个元素
:not() 在一个集合中,去掉某一些元素

分组匹配:nth-of-type():nth-last-of-type()的参数都为 an+b,其中 a 是系数,n 为从 0 开始自增的计数器,b 为最小值为 1 的偏移量。ps:需要注意元素的有效编号是从 1 开始的,如:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. .l {
  10. position: relative;
  11. }
  12. .l1 {
  13. width: 300px;
  14. position: absolute;
  15. left: 0;
  16. top: 0;
  17. border: 6px solid gray;
  18. }
  19. .l2 {
  20. width: 300px;
  21. top: 0;
  22. left: 350px;
  23. position: absolute;
  24. border: 6px solid gray;
  25. }
  26. .l3 {
  27. width: 300px;
  28. top: 0;
  29. left: 700px;
  30. position: absolute;
  31. border: 6px solid gray;
  32. }
  33. /* :nth-of-type()选中list类下li同类的正数第5个子元素 */
  34. .list > :nth-of-type(5) {
  35. background-color: hotpink;
  36. }
  37. /* :nth-last-of-type()选中list类下li同类的倒数第5个元素 */
  38. .list > :nth-last-of-type(5) {
  39. background-color: red;
  40. }
  41. /* :first-of-type选中list类中第一个元素,由于li跟p是两个不同的元素,因此两个分组会分开取值,这里用:not()排除掉p元素 */
  42. .list > :first-of-type:not(p:first-of-type) {
  43. background-color: green;
  44. }
  45. /* :last-of-type选中list类中最后一个元素 */
  46. .list > :last-of-type:not(p:last-of-type) {
  47. background-color: lightblue;
  48. }
  49. /* 从list2类第9个元素开始从上往下每3个选中1个选中所有元素 */
  50. .list2 > :nth-of-type(3n + 9) {
  51. background-color: yellow;
  52. }
  53. /* 从list2类倒数第9个元素开始从下往上每4个选中1个选中所有元素 */
  54. .list2 > :nth-last-of-type(4n + 9) {
  55. background-color: slateblue;
  56. }
  57. /* 从list2类中的list7类中选取偶数元素,这里需要注意两个:nth-of-type()之间要用空格隔开 */
  58. .list2 .list7 > :nth-of-type(even) {
  59. background-color: tomato;
  60. }
  61. /* 从list2类中的list8类中选取偶数元素,这里需要注意两个:nth-of-type()之间要用空格隔开 */
  62. .list2 .list8 > :nth-of-type(odd) {
  63. background-color: lightblue;
  64. }
  65. /* 从list3中选择前6个元素 */
  66. .list3 > :nth-of-type(-n + 6) {
  67. background-color: lightseagreen;
  68. }
  69. /* 从list3中选择后6个元素 */
  70. .list3 > :nth-last-of-type(-n + 6) {
  71. background-color: magenta;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="l">
  77. <div class="l1">
  78. <ul class="list">
  79. <li>item1</li>
  80. <li>item2</li>
  81. <p>------</p>
  82. <li>item3</li>
  83. <p>------</p>
  84. <li>item4</li>
  85. <p>------</p>
  86. <li>item5</li>
  87. <p>------</p>
  88. <li>item6</li>
  89. </ul>
  90. </div>
  91. <div class="l2">
  92. <ul class="list2">
  93. <li>item1</li>
  94. <li>item2</li>
  95. <li>item3</li>
  96. <li>item4</li>
  97. <li>item5</li>
  98. <li>item6</li>
  99. <li>
  100. item7
  101. <ul class="list7">
  102. <li>item7-1</li>
  103. <li>item7-2</li>
  104. <li>item7-3</li>
  105. <li>item7-4</li>
  106. <li>item7-5</li>
  107. <li>item7-6</li>
  108. </ul>
  109. </li>
  110. <li>
  111. item8
  112. <ul class="list8">
  113. <li>item8-1</li>
  114. <li>item8-2</li>
  115. <li>item8-3</li>
  116. <li>item8-4</li>
  117. <li>item8-5</li>
  118. <li>item8-6</li>
  119. </ul>
  120. </li>
  121. <li>item9</li>
  122. <li>item10</li>
  123. <li>item11</li>
  124. <li>item12</li>
  125. <li>item13</li>
  126. <li>item14</li>
  127. </ul>
  128. </div>
  129. <div class="l3">
  130. <ul class="list3">
  131. <li>item1</li>
  132. <li>item2</li>
  133. <li>item3</li>
  134. <li>item4</li>
  135. <li>item5</li>
  136. <li>item6</li>
  137. <li>item7</li>
  138. <li>item8</li>
  139. <li>item9</li>
  140. <li>item10</li>
  141. <li>item11</li>
  142. <li>item12</li>
  143. <li>item13</li>
  144. <li>item14</li>
  145. <li>item15</li>
  146. <li>item16</li>
  147. <li>item17</li>
  148. <li>item18</li>
  149. </ul>
  150. </div>
  151. </div>
  152. </body>
  153. </html>

以上代码中

  • :nth-of-type(5)等同于:nth-of-type(0n+5)
  • :nth-last-of-type(5)等同于:nth-last-of-type(0n+5)
  • :nth-of-type(even)等同于:nth-of-type(2n+0)
  • :nth-of-type(odd)等同于:nth-of-type(2n+1)
  • :nth-of-type(-n + 6):nth-last-of-type(-n + 6)n 前面加-号可以实现反选操作,以选中前几位或后几位
  • :nth-of-type(an + b)需要注意,b 前面不建议使用-号,虽然会默认忽略报错不影响显示,但并不符合规范

初识媒体查询

使用媒体查询可以针对不同的屏幕设置不同的样式,达到适配不同型号屏幕的效果

1.从小到大进行媒体查询(移动优先)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. html {
  11. font-size: 10px;
  12. }
  13. body {
  14. font-size: 1.6rem;
  15. }
  16. p {
  17. border: 0.1rem solid red;
  18. width: 9rem;
  19. background-color: hotpink;
  20. }
  21. /* 开始媒体查询 */
  22. /* 以移动优先,从小到大进行屏幕的媒体查询 */
  23. /* 设置最小480手机宽度的字体大小 */
  24. @media (min-width: 480px) {
  25. html {
  26. font-size: 12px;
  27. }
  28. }
  29. /* 设置第二个级别640手机宽度的字体大小 */
  30. @media (min-width: 640px) {
  31. html {
  32. font-size: 24px;
  33. }
  34. }
  35. /* 设置第三个级别720手机宽度的字体大小 */
  36. @media (min-width: 720px) {
  37. html {
  38. font-size: 48px;
  39. }
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <p>hello world</p>
  45. </body>
  46. </html>

由于浏览器对最小字号有默认限制,小于 480 的效果我们与 不小于 480 但是小于 640 的效果进行对比

尺寸为 411 时的效果
411
尺寸为 481 时的效果
481

可以看到,由于浏览器默认字号的限制,字号大小并没有变化,但是我们设置的宽度是跟随配置的最小 font-size 进行了变化的

尺寸为 642 时的效果
642
尺寸为 721 时的效果
721

可以看到 480 以后都进行了正常的变化

2.从大到小进行媒体查询(PC 优先)

在从小到大进行媒体查询的时候,我们发现在小于设置的最小尺寸 480 的时候,font-size 为 10px 会自动生效,同样的在从大到小进行媒体查询的时候,如果大于设置的最大尺寸的时候,font-size 为 10px 也会自动生效,这时候就需要我们设置最大尺寸的同时,以最大尺寸的尺寸设置一个最小尺寸,来避免这个情况发生

下面为了更好的理解先不加以最大尺寸的尺寸设置的最小尺寸来展示

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. html {
  11. font-size: 10px;
  12. }
  13. body {
  14. font-size: 1.6rem;
  15. }
  16. p {
  17. border: 0.1rem solid red;
  18. width: 9rem;
  19. background-color: hotpink;
  20. }
  21. /* 开始媒体查询 */
  22. /* 以PC优先,从大到小进行屏幕的媒体查询 */
  23. /* 设置最大720手机宽度的字体大小 */
  24. @media (max-width: 720px) {
  25. html {
  26. font-size: 48px;
  27. }
  28. }
  29. /* 设置第二个级别640手机宽度的字体大小 */
  30. @media (max-width: 640px) {
  31. html {
  32. font-size: 24px;
  33. }
  34. }
  35. /* 设置第三个级别480手机宽度的字体大小 */
  36. @media (max-width: 480px) {
  37. html {
  38. font-size: 12px;
  39. }
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <p>hello world</p>
  45. </body>
  46. </html>

尺寸为 435 时的效果
435
尺寸为 483 时的效果
483
尺寸为 641 时的效果
641
尺寸为 725 时的效果
725

通过以上发现尺寸在 720 以下时均能正常显示,但是当尺寸大于 720 以后字号反而恢复了最初默认的 font-size 为 10px 的大小

这个时候我们就需要在最后面加一个最小 720 时的尺寸,来进行限制,我们将以下代码加入到 css 末尾

  1. /* 设置当大于720时的统一字体大小 */
  2. @media (min-width: 720px) {
  3. html {
  4. font-size: 48px;
  5. }
  6. }

此时尺寸为 725 时恢复正常,即屏幕尺寸大于 720 也正常显示设置字号的最大值

尺寸为 725 时的效果
725-2

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议