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

选择器权重~伪类

shooter
shooter原创
2022年03月22日 13:53:05544浏览

权重参考值

  1. /* 选择器的权重
  2. id(千) > class(百) > tag(个)
  3. */
  4. /* tag 1个 权重 (0,0,1) */
  5. body {
  6. color: red;
  7. }
  8. /* tag 2个 权重 (0,0,2)*/
  9. body ul {
  10. background-color: aqua;
  11. }
  12. /* class 1个 权重 (0,1,0) */
  13. .list {
  14. background-color: chartreuse;
  15. }
  16. /* id 1个 权重 (1,0,0) */
  17. #b {
  18. background-color: coral;
  19. }

伪类

  1. /* :first-of-type 匹配分组的第一个元素 */
  2. .list > li:first-of-type {
  3. background-color: aqua;
  4. }
  5. /* :last-of-type 匹配分组的最后一个元素 */
  6. .list > :last-of-type {
  7. background-color: darkorange;
  8. }
  9. /* :nth-last-of-type()
  10. 反向匹配第三个元素
  11. a=0
  12. n=n
  13. b=3
  14. a*n+b=3
  15. */
  16. .list > :nth-last-of-type(3) {
  17. background-color: deeppink;
  18. }
  19. /* 获取偶数索引元素
  20. a=2
  21. n=n
  22. b=0
  23. a*n+0=(
  24. 2*0+0=0
  25. 2*1+0=2
  26. 2*2+0=4
  27. 2*3+0=6
  28. ....
  29. 以此类推
  30. )
  31. */
  32. .list > :nth-last-of-type(2n) {
  33. background-color: chartreuse;
  34. }
  35. /* 获取奇数索引元素
  36. a=2
  37. n=n
  38. b=1
  39. a*n+1=(
  40. 2*0+1=1
  41. 2*1+1=3
  42. 2*2+1=5
  43. 2*3+1=7
  44. ....
  45. 以此类推 */
  46. .list > :nth-last-of-type(2n + 1) {
  47. background-color: brown;
  48. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议