博主信息
博文 7
粉丝 0
评论 0
访问量 5126
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
实例演示:选择器权重,常用伪类选择器
原创
679人浏览过

选择器权重

代码

  1. <body>
  2. <ul>
  3. <li class="item1">权重(0,1,2)大于(0,1,1),所以背景色显示蓝色,而非橙色</li>
  4. <li class="item1">权重2</li>
  5. <li class="item2" id="weight">权重(1,0,1)大于(0,1,1),所以背景色显示蓝色,而非粉红色</li>
  6. <li class="item2">权重4</li>
  7. <li class="item3">权重5</li>
  8. <li class="item3">权重6</li>
  9. </ul>
  10. <style>
  11. /* id: 千位 , class: 百位 , tag: 个位 */
  12. /* id: 1 , class: 0 , tag: 1 */
  13. /* 权重(1,0,1) */
  14. #weight {background-color: blue;}
  15. /* id: 0 , class: 1, tag: 2 */
  16. /* 权重(0,1,2) */
  17. ul li.item1 {background-color: cyan;}
  18. /* id: 0 , class: 1, tag: 1 */
  19. /* 权重(0,1,1) */
  20. li.item1 {background-color: orange;}
  21. li.item2 {background-color: violet;}
  22. /* id: 0 , class: 0, tag: 1 */
  23. /* 权重(0,0,1) */
  24. li {background-color: green;}
  25. </style>
  26. </body>

效果

常用伪类选择器

代码

  1. <body>
  2. <ul>
  3. <li>item1</li>
  4. <li>item2</li>
  5. <li>item3</li>
  6. <li>item4</li>
  7. <li>item5</li>
  8. <li>item6</li>
  9. <li>item7</li>
  10. <li>item8</li>
  11. <li>item9</li>
  12. <li>item10</li>
  13. <li>item11</li>
  14. <li>item12</li>
  15. <li>item13</li>
  16. <li>item14</li>
  17. </ul>
  18. <style>
  19. /* 匹配第1个 */
  20. li:first-of-type{ color:orange }
  21. /* :nth-of-type(an+b) */
  22. /* 匹配第4个 */
  23. /* a=0,n=[0,1,...], b = 4 */
  24. li:nth-of-type(4){ color:pink }
  25. /* a=-1 */
  26. /* 取前3个 */
  27. li:nth-of-type(-n+3) { background-color: yellow; }
  28. /* 取后3个 */
  29. li:nth-last-of-type(-n+3) { background-color:lightseagreen; }
  30. /* 匹配第9个及之后 */
  31. /* a=1,n=[0,1,...], b = 9 */
  32. li:nth-of-type(n+9){ color:red }
  33. /* 匹配最后1个 */
  34. li:last-of-type{ color: blue }
  35. </style>
  36. </body>

效果

批改老师: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+教程免费学