博主信息
博文 5
粉丝 0
评论 0
访问量 4352
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS常用伪类
浮沉
原创
773人浏览过

CSS 伪类选择器

常用伪类选择器:
1.匹配任意位置的元素:”:nth-of-type(an+b)”
其中:an+b: an起点,b是偏移量, n = (0,1,2,3…);even 索引是偶数的子元素,odd索引是奇数的子元素;
2.反向获取任意位置的元素:”:nth-last-of-type(an+b)”;
3.选择第一个子元素:”:first-of-type”;
4.选择最后一个子元素:”:last-of-type”.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>伪类选择器</title>
  7. <style>
  8. /* 1.匹配任意位置的元素:":nth-of-type(an+b)"
  9. an+b: an起点,b是偏移量, n = (0,1,2,3...)
  10. even 索引是偶数的子元素,odd索引是奇数的子元素*/
  11. ul li:nth-of-type(1n + 2) {
  12. background-color: cyan;
  13. }
  14. ul li:nth-of-type(even) {
  15. background-color: darkred;
  16. }
  17. ul li:nth-of-type(odd) {
  18. background-color: darkturquoise;
  19. }
  20. /* 2.反向获取任意位置的元素:":nth-last-of-type(an+b)" */
  21. ul li:nth-last-of-type(-n + 3) {
  22. background-color: darkgoldenrod;
  23. }
  24. /* 3.选择第一个子元素:":first-of-type" */
  25. ul li:first-of-type {
  26. background-color: violet;
  27. }
  28. /* 4.选择最后一个子元素:":last-of-type" */
  29. ul li:last-of-type {
  30. background-color: wheat;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <ul>
  36. <li>item1</li>
  37. <li>item2</li>
  38. <li>item3</li>
  39. <li>item4</li>
  40. <li>item5</li>
  41. <li>item6</li>
  42. <li>item7</li>
  43. <li>item8</li>
  44. <li>item9</li>
  45. <li>item10</li>
  46. </ul>
  47. </body>
  48. </html>
批改老师:天蓬老师天蓬老师

批改状态:合格

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

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

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