博客列表 >选择器权重+伪类选择器及参数

选择器权重+伪类选择器及参数

愿情的博客
愿情的博客原创
2022年03月22日 18:57:24441浏览

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. <link rel="stylesheet" href="css/selector-weight.css" />
  9. <!-- <style>
  10. body {
  11. background-color: red;
  12. }
  13. </style> -->
  14. <!-- 文档样式 > 外部样式 -->
  15. </head>
  16. <body>
  17. <h1 class="title" id="active">Hello</h1>
  18. <div class="col-md-3 vip"></div>
  19. </body>
  20. </html>
  1. /* id: 千位
  2. class: 百位
  3. tag: 个位 */
  4. /* id:0
  5. class: 0
  6. tag: 2
  7. 权重 : 0,0,2 */
  8. body h1 {
  9. color: violet;
  10. }
  11. /* 调试样式 */
  12. body {
  13. background-color: red !important;
  14. }
  15. /* 0,1,2 */
  16. /* body h1.title {
  17. color: red;
  18. } */
  19. /* 0,1,0 > 0,0,2 */
  20. h1.title {
  21. color: red;
  22. }
  23. /* 1,0,0 > 0,1,1 */
  24. /* 1,1,2 */
  25. body h1#active.title {
  26. color: seagreen;
  27. }
  28. /* (0,0,1) */
  29. /* !important: 最高指示,忽略任何权重 */
  30. /* !important: 调试样式 */
  31. h1 {
  32. color: chartreuse !important;
  33. }
  34. /* (1, 1, 1) */
  35. h1#active.title {
  36. color: blue;
  37. }
  38. /* 1,1,1 --> 1,1,2 */
  39. /* 权重让选择器摆脱了对书写顺序的依赖 */
  40. /* 0,0,2 > 0,0,1 */
  41. h1 {
  42. color: darkorange;
  43. }
  44. /* (0,1,1) */
  45. div.col-md-3 {
  46. border: 1px solid #000;
  47. }
  48. body {
  49. background-color: yellow;
  50. }
  51. /* (0,2,1) > (0,,1,1) */
  52. div.col-md-3.vip {
  53. border: 1px solid #000;
  54. /* .... */
  55. }
  56. /* 原因就是不需要修改框架源码,只需要通过动态调整优先级就可以改变默认样式 */

2,伪类选择器

  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. @import url('css/selector-fake.css');
  10. </style>
  11. </head>
  12. <body>
  13. <!-- 伪: 假 -->
  14. <!-- 类: class -->
  15. <!-- 结构伪类 -->
  16. <ul class="list">
  17. <li>item1</li>
  18. <li>item2</li>
  19. <li>item3</li>
  20. <li>item4</li>
  21. <li>item5</li>
  22. <li>item6</li>
  23. <li>item7</li>
  24. <li>item8</li>
  25. <li>item9</li>
  26. <li>item10</li>
  27. <li>item11</li>
  28. <li>item12</li>
  29. <li>item13</li>
  30. <li>item14</li>
  31. <li>item15</li>
  32. <li>item16</li>
  33. <li>item17</li>
  34. <li>item18</li>
  35. </ul>
  36. </body>
  37. </html>
  1. /* 分组结构伪类, 用于选择:子元素 */
  2. /* 所以应该给它一个查询起点 */
  3. /* .list>.first {
  4. background-color: violet;
  5. } */
  6. /* .list>li:nth-of-type(1) {
  7. background-color: violet;
  8. } */
  9. /* nth-of-type(1) ===> first-of-type */
  10. .list>li:first-of-type {
  11. background-color: green;
  12. }
  13. /* .list>li:nth-of-type(11) {
  14. background-color: violet;
  15. } */
  16. .list>li:last-of-type {
  17. background-color: red;
  18. }
  19. /* 倒数第4个 */
  20. /* .list>li:nth-of-type(10) {
  21. background-color: violet;
  22. } */
  23. .list>li:nth-last-of-type(4) {
  24. background-color: violet;
  25. }
  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. /* @import url('css/selector-fake-parma.css'); */
  10. @import 'css/selector-fake-parma.css';
  11. </style>
  12. </head>
  13. <body>
  14. <ul class="list">
  15. <li>item1</li>
  16. <li>item2</li>
  17. <li>item3</li>
  18. <li>item4</li>
  19. <li>item5</li>
  20. <li>item6</li>
  21. <li>item7</li>
  22. <li>item8</li>
  23. </ul>
  24. </body>
  25. </html>
  1. /*
  2. :nth-of-type(an+b)
  3. 1. a: 系数, [0,1,2,...]
  4. 2. n: [0,1,2,3,....]
  5. 3. b: 偏移量, 从0开始
  6. 注: 计算出来的索引,必须是有效, 从1开始
  7. */
  8. /* 选择元素只有二种情况:
  9. 1. 选择一个
  10. 2. 选择一组 */
  11. /* 1. 匹配一个, a = 0 */
  12. /* :nth-of-type(an+b) */
  13. /* 匹配第3个 */
  14. /* a=0,n=[0,1,...], b = 3 */
  15. /* .list> :nth-of-type(0n + 3) {
  16. background-color: lightgreen;
  17. } */
  18. /* .list> :nth-of-type(3) {
  19. background-color: lightgreen;
  20. } */
  21. /* 2. 匹配一组 */
  22. /* 2.1 a = 1 */
  23. /* .list> :nth-of-type(1n) {
  24. background-color: lightgreen;
  25. } */
  26. /* n = 0,1,2...
  27. 1*0 =0
  28. 1*1 = 1
  29. 1*2 = 2
  30. 1*3 = 3
  31. [1,2,3,....] */
  32. /* .list> :nth-of-type(n) {
  33. background-color: lightgreen;
  34. } */
  35. /* .list>* {
  36. background-color: lightgreen;
  37. } */
  38. /* 实际开发中, 使用 n+b(偏移量)来快速选择一组元素 */
  39. /* 匹配第3个元素后面的所有兄弟元素 */
  40. /* .second~* {
  41. background-color: violet;
  42. } */
  43. /* .list> :nth-of-type(n + 3) {
  44. background-color: lightgreen;
  45. } */
  46. /* n = 0,1,2,...
  47. 0+3=3
  48. 1+3=4
  49. 2+3=5
  50. .... */
  51. /* a=-1 */
  52. /* 取前3个 */
  53. /* .list> :nth-of-type(-n + 3) {
  54. background-color: lightgreen;
  55. } */
  56. /* -1*0 + 3 = 3
  57. -1*1 + 3 = 3-1 = 2
  58. -1*2 + 3 = 3-2 = 1
  59. -1*3+3 = 3-3 = 0
  60. [3,2,1] */
  61. /* 最后三个,只需要换一下选择器, 参数不变 */
  62. /* .list> :nth-last-of-type(-n + 3) {
  63. background-color: lightgreen;
  64. } */
  65. /* .list> :nth-of-type(2n - 1) {
  66. background-color: lightgreen;
  67. } */
  68. /*
  69. .list> :nth-of-type(odd) {
  70. background-color: lightgreen;
  71. } */
  72. .list> :nth-of-type(even) {
  73. background-color: lightgreen;
  74. }
  75. /* odd: 奇数, even: 偶数 */
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议