一、css选择器的优先级解释

排序规则:!important > 行内样式>ID选择器 > 类选择器 > 标签 > 通配符 > 继承 > 浏览器默认属性

  1. <html lang="zh-CN">
  2. <head>
  3. <meta charset="UTF-8" />
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <!-- 内部样式,仅作用于当前的html文档 -->
  8. <style>
  9. h1,
  10. h2,
  11. h3,
  12. h4,
  13. {
  14. font-size: 2em;
  15. }
  16. /* 语法 : 选择器 {
  17. 声明,有二部分
  18. 属性: 值,多个声明之间用分号隔开
  19. } */
  20. /* 选择器 + 声明块 = 样式规则 */
  21. h1 {
  22. color: #990091;
  23. /* !important 级别最高的,不建议用,适用于调试 */
  24. color: #ff0 !important;
  25. }
  26. html body h3.active {
  27. color: #900;
  28. }
  29. html body h4#first {
  30. color: #22e932;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <!-- 行内样式,仅适用于当前元素,优先级要高于style标签设置的内部样式 -->
  36. <h1 style="color: #3c6">这个是h1!important样式</h1>
  37. <h2 style="color: #e90d94">这个是h2标签样式</h2>
  38. <h3 class="active">类样式</h3>
  39. <h4 class="active" id="first">这个是id签样式</h4>
  40. </body>
  41. </html>

引用样式方法

  1. 1、引用样式方法一
  2. @import url(style.css);
  3. 2、引用样式方法二
  4. <link rel="stylesheet" href="style.css" />
  5. 3、属性选择器
  6. - li[class="on"] = li.on
  7. - li[id="foo"] = li#foo
  8. 4、-后代选择器 ul li
  9. -只选中子元素,忽略孙元素body > ul > li
  10. -同级相邻 .start + li
  11. -同级所有兄弟 .start ~ li

结构伪类

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <style>
  9. /* 结构伪类: 选择子元素, 要有一个父元素做为查询起点 */
  10. /* .list > :nth-child(3) {
  11. background-color: violet;
  12. } */
  13. /* 匹配任何位置的元素
  14. n = (0,1,2,3,4....) */
  15. /* .list > li:nth-child(0n + 3) {
  16. background-color: violet;
  17. } */
  18. /* 分组伪类结构选择器,推荐使用 */
  19. .list > li:nth-of-type(3) {
  20. background-color: #ff4800;
  21. }
  22. /* 选择中第一个p */
  23. .list > p:nth-of-type(1) {
  24. background-color: #0dec0d;
  25. }
  26. .list > li:nth-of-type(1) {
  27. background-color: #0db1f1;
  28. }
  29. /* 最后一个li */
  30. .list > li:nth-of-type(7) {
  31. background-color: #800035;
  32. }
  33. /* 最后一个p */
  34. .list > p:nth-of-type(3) {
  35. background-color: #630cee;
  36. }
  37. .list p:last-of-type {
  38. background-color: blue;
  39. }
  40. .list p:first-of-type {
  41. background-color: red;
  42. }
  43. /* 选择倒数第三个li */
  44. .list > li:nth-last-of-type(3) {
  45. background-color: yellow;
  46. }
  47. ul li:only-of-type {
  48. background-color: yellow;
  49. }
  50. /* 选择任何一个: :nth-of-type(n)
  51. 选择第一个: :first-of-type
  52. 选择最后一个: :last-of-type
  53. 选择倒数某一个: :nth-last-of-type()
  54. 唯一子元素的元素: :only-of-type */
  55. </style>
  56. </head>
  57. <body>
  58. <ul class="list">
  59. <li>参数1</li>
  60. <li>参数</li>
  61. <li>参数</li>
  62. <li>参数4</li>
  63. <li>参数5</li>
  64. <li>参数6</li>
  65. <p>参数7</p>
  66. <p>参数8</p>
  67. <li>参数9</li>
  68. <p>参数0</p>
  69. </ul>
  70. <ul>
  71. <li>参数</li>
  72. </ul>
  73. </body>
  74. </html>

更多相关文章

  1. Kafka参数broker.id详解
  2. 数十亿用户的Facebook如何进行贝叶斯系统调优?
  3. js第一课 引入方式 参数 函数
  4. 为什么使用Tailwind Css框架?
  5. js引入方式、变量与常量的声明与使用方式、函数与高阶函数,实例演
  6. 分位数回归, Oaxaca分解, Quaids模型, 非参数估计程序
  7. 0331作业
  8. js基础:变量与常量的声明以及函数知识
  9. javascript:引入方式/变量与常量声明/函数与高阶函数/归并参数/箭

随机推荐

  1. 知识储备:Android系统架构
  2. android 1.5 NDK发布
  3. qpython3:安卓上运行Python
  4. Android(安卓)View Attributes
  5. Android启动模式之singleTask解析
  6. TextView使用完全讲解
  7. Android 异步Http框架简介和实现原理
  8. Android中设置控件可见与不可见
  9. 饭后Android 第一餐-NavigationView+Tool
  10. 【月入41万】Mono For Android中使用百度