博主信息
博文 6
粉丝 0
评论 0
访问量 5465
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
选择器权重、伪类选择器、结构伪类选择器
似水流年
原创
1235人浏览过

实例演示选择器权重 并写出详细计算过程

选择器

  1. tag标签选择器
    1. <h1>Hello world</h1>
    2. <style>
    3. /* 标签选择器 */
    4. h1{color:red;}
    5. </style>
  2. clss选择器
    1. <h1 class="cls">Hello world</h1>
    2. <style>
    3. /* class选择器 */
    4. h1.cls{color:blue;}
    5. </style>
  3. id选择器
    1. <h1 id="id">Hello world</h1>
    2. <style>
    3. /* id选择器 */
    4. h1#id{color:green;}
    5. </style>
  4. 上下文选择器/层级选择器:子元素选择器、后代选择器、相邻兄弟选择器
    1. <ul class="list">
    2. <li class="item">item-1</li>
    3. <li class="item second">item-2</li>
    4. <li class="item">item-3</li>
    5. <ul class="inner">
    6. <li>item3-1</li>
    7. <li>item3-2</li>
    8. <li>item3-3</li>
    9. </ul>
    10. <li class="item">item-4</li>
    11. <li class="item">item-5</li>
    12. <li class="item">item-6</li>
    13. <li class="item">item-7</li>
    14. <li class="item">item-8</li>
    15. </ul>
    16. <style>
    17. /* 子元素选择器 > */
    18. .list>li{border:1px solid black;}
    19. /* 后代元素选择器 空格 */
    20. .list li{border:1px solid red;}
    21. /* 相邻兄弟选择器 */
    22. .item.second{background-color: aqua ;}
    23. .item.second +*{background-color: greenyellow;}
    24. /* 相邻所有兄弟选择器 */
    25. .item.second ~*{background-color: goldenrod;}
    26. </style>
  5. 选择器的权重
    1. <h1 class="a" id="b">Hello world</h1>
    2. <style>
    3. /* 权重:(id,class,tag) */
    4. /* 权重值(1个id,0个clss,0个tag,值为1,0,0 )*/
    5. #b{color: red;}
    6. /* 权重值(0个id,1个clss,1个tag,值为0,1,1 )*/
    7. h1.a{color: aqua;}
    8. /* 权重值(0个id,0个clss,1个tag,值为0,0,1 )*/
    9. h1{color: blue;}
    10. /* !important 调试样式,能覆盖所有的优先级,忽略任何权重 */
    11. h1{color: blueviolet !important;}
    12. </style>

使用bootstrap、elementUI、layUI等可以通过增加class属性来增加权重;

  1. <div class="col-md-3 a"></div>
  2. <!-- 在原有clss里添加a -->
  3. <style>
  4. div.col-md-3{height: 40px;background-color: aqua;}
  5. /* 通过增加class选择器,权重变化为(0,1,1)-->(0,2,1) */
  6. div.col-md-3.a{border: 1px solid;}
  7. </style>

文档样式权重大于外部引用样式

  1. <style>/*文档样式*/</style>
  2. <!-- 外部引用样式 -->
  3. <link rel="stylesheet" herf="/static/style.css">

实例演示常用伪类选择器 并写出参数计算过程

伪类选择器:

  1. <ul class="title">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li>item3</li>
  5. <li>item4</li>
  6. <li>item5</li>
  7. <li>item6</li>
  8. <li>item7</li>
  9. <li>item8</li>
  10. <li>item9</li>
  11. <li>item10</li>
  12. <li>item11</li>
  13. <li>item12</li>
  14. <li>item13</li>
  15. <li>item14</li>
  16. </ul>
  17. <style>
  18. /* 选择第一个 */
  19. .title>li:first-of-type{background-color: blueviolet;}
  20. /* 选择最后一个 */
  21. .title>li:last-of-type{background-color: chocolate;}
  22. /* 正数第六个 */
  23. .title>li:nth-of-type(6){background-color: burlywood;}
  24. /* 倒数第二个 */
  25. .title>li:nth-last-of-type(2){background-color: burlywood;}
  26. </style>

结构伪类选择器的参数计算

  • 结构伪类选择器计算公式

    :nth-of-type(an+b)
    1、a:系数
    2、n:参数 1,2,3,4,……
    3、b:偏移量
    前3个:nth-of-type(-n+3)
    倒数3个:nth-last-of-type(-n+3)
    奇数:odd
    偶数:even

  1. <ul class="title">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li>item3</li>
  5. <li>item4</li>
  6. <li>item5</li>
  7. <li>item6</li>
  8. <li>item7</li>
  9. <li>item8</li>
  10. <li>item9</li>
  11. </ul>
  12. <style>
  13. /* 选择所有的,计算为1n+0,简写为n */
  14. .title>li:nth-of-type(n){color: brown;}
  15. /* 选择第三个,计算为0n+3 */
  16. /* .title>li:nth-of-type(3){color: red;} */
  17. /* 选择第三个往后的 nth-of-type(n+3) */
  18. .title>li:nth-of-type(n+3){
  19. background-color: darkcyan;
  20. }
  21. /* 选择前三个:nth-of-type(-n+3) */
  22. .title>li:nth-of-type(-n+3){
  23. background-color: indigo;
  24. }
  25. /* 选择最后的3个:nth-last-of-type(-n+3) */
  26. .title>li:nth-last-of-type(-n+3){
  27. background-color: blueviolet;
  28. }
  29. /* 选择偶数个 */
  30. .title>li:nth-of-type(2n){background-color: aqua;}
  31. /* .title>li:nth-of-type(even){background-color: aqua;} */
  32. /* 选择奇数: */
  33. .title>li:nth-of-type(2n+1){background-color: blueviolet;}
  34. /* .title>li:nth-of-type(odd){background-color: aqua;} */
  35. </style>

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