博主信息
博文 8
粉丝 0
评论 0
访问量 5315
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
选择器权重与伪类选择器
平凡之路
原创
873人浏览过

一 选择器权重

代码

  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. }

示例


批改老师:PHPzPHPz

批改状态:合格

老师批语:代码已经展示出来了,就没有必要再截屏了,直接展示效果图即可
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学