博主信息
博文 26
粉丝 0
评论 0
访问量 18743
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
1020作业:权重原理与计算方式+结构伪类
高空中的云
原创
891人浏览过

权重的原理与计算方式

权重规则

遵循(id,class,tag)计数和顺序,例如:
(0,0,1) 代表 0个id,0个class,1个tag
h1{color:red;} 的选择器权重就是 (0,0,1),只有一个tag``h1
h1.title{color:red}的权重就是(0,1,1),有一个class,一个tag
h1#active.title{color:red}的权重就是(1,1,1),有一个id,一个class,一个tag
html body .header h1 .title div #active{color:red}的权重就是(1,2,4)

几个要点

同权重,后来者居上

例如,

  1. html h1{
  2. color:red;
  3. }
  4. body h1{
  5. color:blue;
  6. }

html h1body h1 都是 (0,0,2),但是h1最终呈现的颜色是blue

id > class > tag

例如,

  1. <html>
  2. <body>
  3. <h1 id="titleId" class="titleClass">
  4. 这是一个标题
  5. </h1>
  6. </body>
  7. </html>
  8. <style>
  9. #titleId{
  10. color:red;
  11. }
  12. .titleClass{
  13. color:yellow;
  14. }
  15. html body h1{
  16. color:blue;
  17. }
  18. </style>

虽然html body h1是(0,0,3),但h1的显示颜色仍然为red.如果移除掉titleId,则显示为yellow.

修改UI库样式

最小代价是,为html标签元素添加自定义类,为该自定义类编写样式.通过提高权重或者后来者居上引入来达到样式覆盖的目的

页面整洁和健壮

html文件中尽量不直接写style,所有样式均写在css文件中.慎用!important

实例演示结构伪类

实例

结果如图

css部分代码

  1. .list > li.first {
  2. border: 5px solid red;
  3. }
  4. .list > li:nth-of-type(n){
  5. font-weight: thin;
  6. }
  7. .list > li:nth-of-type(1){
  8. background-color: gray;
  9. }
  10. /* 只有n=0,1时候,才有意义 */
  11. .list > li:nth-of-type(-n+2){
  12. font-size: 1.2rem;
  13. }
  14. .list > li:nth-of-type(2n+1){
  15. color: purple;
  16. }
  17. .list > li:nth-of-type(3n){
  18. font-size: 2.5rem;
  19. }
  20. .list > li:nth-last-of-type(2){
  21. font-style: italic;
  22. border-bottom: 2px dashed blue;
  23. }
  24. .list > li:nth-last-of-type(1){
  25. background-color: coral;
  26. font-size:1rem;
  27. color:bisque;
  28. }

其他

:nth-child():nth-of-type 的不同之处在于,如果同一个父节点下,子标签类型不完全一样,[tag/class]:nth-child(n)渲染的是当前父节点下,第n个元素的样式;而 [tag/class]:nth-of-type则渲染当前父节点下,第n个同名tag/class元素标签的样式,如:

  1. <div class="divClass">
  2. <p class="paClass">
  3. 这是第一个paragraph
  4. </p>
  5. <a class="paClass">
  6. 这是两个paragraph中的a
  7. </a>
  8. <p class="paClass">
  9. 这是第二个paragraph
  10. </p>
  11. </div>
  12. <style>
  13. .divClass > .paClass:nth-child(1){
  14. font-size:.5rem;
  15. }
  16. .divClass > .paClass:nth-child(2){
  17. font-size:1.5rem;
  18. }
  19. .divClass > .paClass:nth-child(3){
  20. font-size:3rem;
  21. }
  22. .divClass > .paClass:nth-of-type(1){
  23. color: red;
  24. }
  25. .divClass > .paClass:nth-of-type(2){
  26. color: yellow;
  27. }
  28. .divClass > .paClass:nth-of-type(3){
  29. color: blue;
  30. }
  31. </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+教程免费学