博客列表 >选择器权重与伪类选择器

选择器权重与伪类选择器

平凡之路
平凡之路原创
2022年03月23日 22:17:43656浏览

一 选择器权重

代码

  1. /* 选择器的权重
  2. 1 权重大小:mpottant>id>class>标签 */
  3. /* 标签选择器
  4. (1) 标签选择器权重为个位 1个标签个位为1,每加一个标签个位权重+1 */
  5. /* (2)多个标签选择器中间用“空格链接” */
  6. 实例
  7. /* 这个选择器权重(0.0.1) */
  8. h1 {
  9. color: aqua;
  10. }
  11. /* 这个选择器权重(0.0.2) */
  12. body h1 {
  13. color: blue;
  14. }
  15. /* 这个选择器权重(0.0.3) */
  16. body h1 p {
  17. color: rgb(0, 255, 64);
  18. }
  19. /* CLASS选择器 (1)class选择器权重为十位 1个class为10,每加一个class十位权重 + 10 (2)多个class选择器中间用“空格链接” 标签和class相连用 . */
  20. div.aaaa {
  21. color: blueviolet;
  22. }
  23. .b a {
  24. color: rgb(226, 43, 43);
  25. }
  26. .xx {
  27. color: aqua;
  28. }
  29. .ss.xxx {
  30. color: rgb(15, 131, 64);
  31. }
  32. /*id选择器 (1)id选择器权重为百位 1个id为100,每加一个id百位权重 + 100 */
  33. #dd {
  34. color: rgb(255, 0, 0);
  35. }

示例


伪类选择器

代码

  1. /* 伪类选择器 */
  2. /* 结构伪类 */
  3. /* > 伪类,仍是`class`级, 结构伪类用于获取一组元素,所以和上下文选择器一样,需要设置查询起点(父级),否则从根递归 */
  4. /* 1 nth-of-type 匹配唯一的子元素*/
  5. .list > li:nth-of-type(2n + 2) {
  6. color: aqua;
  7. background-color: blue;
  8. }
  9. /* 偶数算法
  10. (2*0+2=2)
  11. (2*1+2=4)
  12. (2*2+2=6)
  13. (2*3+2=8) */
  14. .list1 > li:nth-of-type(2n + 1) {
  15. color: rgb(38, 0, 255);
  16. background-color: rgb(255, 0, 149);
  17. }
  18. /*奇数算法
  19. (2*0+1=1)
  20. (2*1+1=3)
  21. (2*2+1=5)
  22. (2*3+1=7) */
  23. .list2 > li:nth-of-type(n + 5) {
  24. color: rgb(0, 26, 2);
  25. background-color: rgb(255, 0, 0);
  26. }
  27. /* 从第五个开始计算
  28. (0+0+5=5)
  29. (0+1+5=6)
  30. (0+2+5=7)
  31. (0+3+5=7) */
  32. .list3 > li:nth-of-type(-n + 5) {
  33. color: rgb(0, 26, 2);
  34. background-color: rgb(255, 0, 0);
  35. }
  36. /* 从第五个开始计算倒序 */
  37. /* 从第五个开始计算
  38. (0+0+5=5)
  39. (0-1+5=4)
  40. (0-2+5=3)
  41. (0-3+5=2)
  42. (0-4+5=1)
  43. (0-5+5=0)
  44. */
  45. /* first-of-type 匹配分组第一个子元素*/
  46. .list4 > li:first-of-type {
  47. color: rgb(0, 255, 21);
  48. background-color: rgb(140, 0, 255);
  49. }
  50. /* last-of-type 匹配分组最后一个子元素*/
  51. .list5 > li:last-of-type {
  52. color: rgb(0, 255, 21);
  53. background-color: rgb(140, 0, 255);
  54. }
  55. /* first-of-type 匹配分组倒数第三名*/
  56. .list6 > li:nth-last-of-type(-n + 3) {
  57. color: rgb(0, 255, 21);
  58. background-color: rgb(140, 0, 255);
  59. }

示例


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