博客列表 >1020作业class的权重和伪类

1020作业class的权重和伪类

A 管志岗-电商策划
A 管志岗-电商策划原创
2022年10月21日 18:24:42376浏览

作业1:class的权重的计算方式

  1. /* 选择器权重 */
  2. /* ? css用3个正整数,来表示选择器的权重 */
  3. /* ? 权重规则
  4. 1. 实体标记: id, class, tag
  5. 2. 排列顺序: id, class, tag
  6. 2. 计数方式: 选择器中的实体标记数量 */
  7. /* 1、权重的表示方法,鼠标移动到css标签上面显示的 */
  8. /* ? * (0,1,1)?: id=>0, class => 0, tag =>1 */
  9. /* ? (id,class,tag): */
  10. h1 {
  11. color: red;
  12. }
  13. /* ? (0,1,1) = id =>0, class =1, tag = 1*/
  14. /* id = null, class = .title, tag = h1 */
  15. h1.title {
  16. color: red;
  17. }
  18. /* ?权重多少? (1,1,1) */
  19. /* ? id = #active = 1, class = .title = 1, tag = h1 = 1 */
  20. h1#active.title {
  21. color: red;
  22. }
  23. /* ? 权重多少?(1,2,3)
  24. id = #active = 1, class = .top.title = 2, tag = header, h1, div = 3
  25. */
  26. header.top h1.title div#active {
  27. color: red;
  28. }
  29. /* 2. 优先级权重 */
  30. /* ? (0,0,1) 和 (0,0,2)哪个大? */
  31. /* 根据css层叠样式规则,后面的样式会覆盖前面的样式 */
  32. /* 举例: */
  33. /* 同样是样式,被后面覆盖 */
  34. .china {
  35. color: #f40;
  36. }
  37. .china {
  38. color: aqua;
  39. }
  40. /* 该方案导致自定义样式规则,依赖书写位置/书写顺序 */
  41. /* .china {
  42. color: #f40;
  43. } , 只要将权重大于(0,1,0),就可以了*/
  44. /* 将0,0,1 => 0,2,0 行吗? */
  45. .use.user {
  46. color: #f40;
  47. }
  48. .use {
  49. color: #1b5081;
  50. }
  51. /* 1. 摆脱了对选择器位置的限制 */
  52. /* 2. 不需要修改html源代码 */
  53. /* ! 3. 权重注意事项:尽量不要在'id'属性,权重太高
  54. 为什么?
  55. 1. 权重过高,位于权重顶端
  56. 2. id 会导致选择器失去弹性/弹性不足,不利于调试或复用
  57. */
  58. /* 4. 权重经典应用场景: class 动态调整是样式 */
  59. /* ? 举例,公司有个项目是依赖UI库:bootstrap.css */
  60. /* 不能直接修改框架里面的class名字,后期框架升级,或者其他地方有应用这个class的地方就会收到影响 */
  61. /* ? 只能修改html代码,最小代价 */
  62. /* ! 修改"html.class ",为它添加自定义类,就是用链
  63. 同一个class有很多名字*/
  64. /* 举例 */
  65. .col-md-6.yellow {
  66. height: 2em;
  67. border: rgb(16, 196, 10) solid thin;
  68. color: #f40;
  69. }

伪类:

  1. /* ? 结构伪类:用来匹配子元素 给一个查询起点*/
  2. /* ? 起点: ul.list */
  3. /* ? 匹配第一个,class */
  4. .list .li.two {
  5. background-color: aqua;
  6. }
  7. /* 用伪类来获取第一个 */
  8. .list .li:nth-of-type(1) {
  9. background-color: #3610e0;
  10. }
  11. /* 用伪类来获取第二个 */
  12. .list .li:nth-of-type(3) {
  13. background-color: #3610e0;
  14. }
  15. /* 用伪类来获取第三个 */
  16. .list .li:nth-of-type(4) {
  17. background-color: #3610e0;
  18. }
  19. /* 用伪类来获取最后一个 */
  20. .list .li:nth-of-type(8) {
  21. background-color: #3610e0;
  22. }
  23. /* ? 快速获取第一个和最后一个 有语法糖*/
  24. .list-2 li:first-of-type {
  25. background-color: aquamarine;
  26. }
  27. .list-2 li:last-of-type {
  28. background-color: aquamarine;
  29. }
  30. /* 问题: 只获取前三个,怎么办?
  31. * !: nth-of-type(an + b);
  32. * 1. a: 系数,[0,1,2,3...]
  33. * 2. n: 参数,[0,1,2,3...]
  34. * 3. b: 偏移量, 从0开始
  35. ? 规则: 计算出来的索引,必须是有效的(从1开始)
  36. */
  37. /* ! 匹配元素的第二个场景: 1. 匹配一个, 2. 匹配一组 */
  38. /* ? 匹配第1个 */
  39. .list-3 li:nth-of-type(0n + 1) {
  40. /*
  41. 1. 0n + 1 = 1
  42. 2. 0*0 + 3 = 3,(n取值为0)
  43. 3. 0*3 + 3 = 3
  44. ...
  45. */
  46. background-color: #3610e0;
  47. }
  48. /* 直接写2也可以 */
  49. .list-3 li:nth-of-type(2) {
  50. background-color: #1dc217;
  51. }
  52. /* * ? a = 1 :匹配一组 (正向)*/
  53. /* 全选 1n, 0可以不放 */
  54. .list-3 li:nth-of-type(1n + 0) {
  55. background-color: #1dc217;
  56. }
  57. /* .list-3 li:nth-of-type(1n) {
  58. background-color: #1dc217;
  59. } */
  60. /* 乘任何数不变 试下a = 2*/
  61. .list-4 li:nth-of-type(2n + 2) {
  62. background-color: #1dc217;
  63. }
  64. /*
  65. 计算过程
  66. * 1: 1 * 0 + 2 = 2
  67. * 2: 1 * 1 + 2 = 3
  68. * 3: 1 * 2 + 2 = 4
  69. * 4: 1 * 3 + 2 = 5
  70. ...
  71. */
  72. /* 如果匹配前三个 */
  73. .list-5 li:nth-of-type(-3n + 8) {
  74. background-color: #1dc217;
  75. }

效果图:

权重效果图:
效果图
伪类效果图:
效果图

作业2:实例演示结构伪类,通过位置关系匹配子元素

备注:

  1. 鼠标移动到class文件里面的标签就会有提示权重
  2. (0,1,2) id = 0, class = 1, tag = 2;
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议