博主信息
博文 28
粉丝 0
评论 1
访问量 18444
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
选择器权重和伪类选择器
centos
原创
821人浏览过

选择器权重和伪类选择器

选择器有id class tag 三种

id代表千位
class代表百位
tag代表个位
其中!improtant是最高级别,忽略权重
下面演示权重递归
为了方便演示,代码顺序采用倒序,以免同位选择器因为读写顺序产生歧义

  • html部分
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>权重和伪类选择器</title>
    8. <link rel="stylesheet" tyep="text/css" href="0321.css" />
    9. </head>
    10. <body>
    11. <ul class="item" id="title">
    12. <li>列表1</li>
    13. <li>列表2</li>
    14. <li>列表3</li>
    15. <li>列表4</li>
    16. <li>列表5</li>
    17. <li>列表6</li>
    18. <li>列表7</li>
    19. <li>列表8</li>
    20. <li>列表9</li>
    21. <li>列表10</li>
    22. <li>列表11</li>
    23. <li>列表12</li>
    24. <li>列表13</li>
    25. </ul>
    26. </body>
    27. </html>
  • css部分
  1. /* 添加一个id */
  2. #title li {
  3. background-color: brown;
  4. }
  5. /* 添加class */
  6. .item li {
  7. background-color: blueviolet;
  8. }
  9. /* 两个标签 */
  10. ul li {
  11. background-color: bisque;
  12. }
  13. /* 一个标签 */
  14. li {
  15. border: 1px solid;
  16. background-color: aquamarine;
  17. }




伪类选择器

伪类选择器以’:’开头,常用的有first-child last-child nth-of-type(n+b) nth-first-of-type last-of-type 奇数odd 偶数even 等组合
以下是演示css

  1. li {
  2. border: 1px solid;
  3. }
  4. /* 选择第一个 */
  5. ul > li:first-child {
  6. background-color: aquamarine;
  7. }
  8. /* 选择最后一个 */
  9. ul > li:last-child {
  10. background-color: brown;
  11. }
  12. /* 选择第三个 */
  13. ul > li:nth-of-type(3) {
  14. background-color: bisque;
  15. }
  16. /* 选择倒数第三个 */
  17. ul > li:nth-last-of-type(3) {
  18. background-color: bisque;
  19. }
  20. /* 前三个 */
  21. ul > li:nth-of-type(-n + 3) {
  22. background-color: red;
  23. }
  24. /* 最后三个 */
  25. ul > li:nth-last-of-type(-n + 3) {
  26. background-color: blue;
  27. }
  28. /* 奇数行 */
  29. ul > li:nth-last-of-type(odd) {
  30. background-color: blueviolet;
  31. }
  32. /* 偶数行 */
  33. ul > li:nth-last-of-type(even) {
  34. background-color: gray;
  35. }
批改老师: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+教程免费学