博主信息
博文 14
粉丝 0
评论 0
访问量 10576
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS权重计算与伪类
Bruce.lau
原创
637人浏览过

CSS权重计算与伪类

一、CSS权重计算

  1. <div>
  2. <ul class = "list">
  3. <li id = "item1">内容1</li>
  4. <li>内容2</li>
  5. <li>内容3</li>
  6. <li>内容4</li>
  7. <li>内容5</li>
  8. </ul>
  9. </div>

权重计算过程

1

以 div.list.li { color:red;}为例

id class tag total
0 1 2 (0,1,2)

2

以 #item { color:red;}为例

id class tag total
1 0 0 (1,0,0)

3

以 list #item { color:red;}为例

id class tag total
1 1 0 (1,1,0)

调试模式 !important

  1. li{ font-size:20px;} !important

使用此模式,忽略其他权重,优先级第一。

二、伪类及参数

1.nth-of-type(n),表示选中第几个元素,例如

  1. <div>
  2. <ul class = "list">
  3. <li>内容1</li>
  4. <li>内容2</li>
  5. <li>内容3</li>
  6. <li>内容4</li>
  7. <li>内容5</li>
  8. </ul>
  9. </div>
  10. <style>
  11. .list> :nth-of-type(3) /*选中第3个/
  12. .list> :nth-first-of-type() /*选中第1个/
  13. .list> :nth-last-of-type() /*选中最后1个/
  14. </style>

2.nth-of-type(an+b),计算公式

1.参数an+b,其中a表示系数,b表示偏移量,n取值自整数0,1,2,3,4……;
2.an的值必须大于0,即为有效;an表示索引,以当前第几个元素为起点
3.b的值可以为0
4.a可以为复数
5.取奇偶,(odd)为奇数,(even)为偶数;

选择一组

  1. <div>
  2. <ul class = "list">
  3. <li>内容1</li>
  4. <li>内容2</li>
  5. <li>内容3</li>
  6. <li>内容4</li>
  7. <li>内容5</li>
  8. </ul>
  9. </div>
  10. list> :nth-of-type(1n+b){color:blue;}

选中全部:计算过程

第一次:nth-of-type(1n+b)=nth-of-type(1x0+0)=nth-of-type(0),参数=0无效;
第二次:nth-of-type(1n+b)=nth-of-type(1x1+0)=nth-of-type(1),表示以第一个元素开始,不用偏移,即选中第1个;
第三次:nth-of-type(1n+b)=nth-of-type(1x2+0)=nth-of-type(2),表示以第一个元素开始,不用偏移,即选中第2个;
第四次:以此类推,直到结束为止。

选第3个及以后:计算过程

第一次:nth-of-type(1n+3)=nth-of-type(1x0+2)=nth-of-type(0),参数an=0无效;
第二次:nth-of-type(1n+2)=nth-of-type(1x1+2)=nth-of-type(3),表示以第一个为起点,往后偏移2个单位,即选中第3个;
第三次:nth-of-type(1n+2)=nth-of-type(1x2+2)=nth-of-type(4),表示以第二个为起点,往后偏移2个单位,即选中第4个;
第四次:以此类推,直到结束为止。

选前3个:计算过程

第一次:nth-of-type(-1n+3)=nth-of-type(-1x0+3)=nth-of-type(3),选中第3个;
第二次:nth-of-type(-1n+3)=nth-of-type(-1x1+3)=nth-of-type(2),选中第2个;
第三次:nth-of-type(-1n+3)=nth-of-type(-1x2+3)=nth-of-type(1),选中第1个;
第四次:nth-of-type(-1n+3)=nth-of-type(-1x3+3)=nth-of-type(0),参数无效;

选倒数3个:计算过程

第一次:nth-last-of-type(-1n+3)=nth-of-type(-1x0+3)=nth-last-of-type(3),选中倒数第3个;
第二次:nth-last-of-type(-1n+3)=nth-of-type(-1x1+3)=nth-last-of-type(2),选中倒数第2个;
第三次:nth-last-of-type(-1n+3)=nth-of-type(-1x2+3)=nth-last-of-type(1),选中倒数第1个;
第四次:nth-last-of-type(-1n+3)=nth-last-of-type(-1x3+3)=nth-of-type(0),参数无效;

选奇数:nth-of-type(odd)或者nth-of-type(2n-1)

选偶数:nth-of-type(even)或者nth-of-type(2n)

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