0


前端:CSS选择器(级联/层叠样式单)--用作装饰

1.选择器:给谁加样式

三种样式如果对于不同的方面,效果叠加,如果是相同的方面,行内样式的优先级最高,外部样式和内部样式的优先级相等,谁在上面先用谁

2.css的语法规则:

  1. selector{
  2. property:value;
  3. property:value;
  4. }
  1. /* 基本选择器*/
  2. /*1.元素选择器 根据元素名称做选择*/
  3. div{
  4. background: red;
  5. color: yellow;
  6. }
  7. /*特例: * 选择所有元素 */
  8. *{
  9. background: red;
  10. color: yellow;
  11. }
  12. /*2.属性选择器 []属性*/
  13. div[id]{
  14. background: red; /*有id属性*/
  15. color: yellow;
  16. }
  17. div[id=a]{
  18. background: red;/*有id属性,并且id属性值等于a*/
  19. color: yellow;
  20. }
  21. div[id$=a]{
  22. background: red;/*有id属性,并且id属性值以a结尾的div元素*/
  23. color: yellow;
  24. }
  25. div[id^=a]{
  26. background: red;/*有id属性,并且id属性值以a开头的div元素*/
  27. color: yellow;
  28. }
  29. div[id*=a]{
  30. background: red;/*有id属性,并且id属性值包含a的div元素*/
  31. color: yellow;
  32. }
  33. div[mm*=a]{
  34. background: red;/*有mm属性,并且id属性值包含a的div元素 属性值可以自己创造*/
  35. color: yellow;
  36. }
  37. /*3.id选择器*/
  38. #ss{
  39. background: red;
  40. color: yellow;
  41. }
  42. /*id最好唯一*/
  43. /*4.class选择器*/
  44. .ss{
  45. background: red;
  46. color: yellow;
  47. }
  48. /*特例,结合选择器*/
  49. p.ss{
  50. background: red;
  51. color: yellow; /*不要用空格,直接拼接*/
  52. }
  53. /*5.包含选择器 selector1 selector2* 空格只时强调包含关系,时候带就行*/
  54. div p{
  55. background: red;
  56. color: yellow; /*在第一个选择器所包含的元素里面,找第二个选择器满足的元素*/
  57. }
  58. /*6.子选择器 selector1>selector2 强调父子关系*/
  59. div>p{
  60. background: red;
  61. color: yellow; /*在第一个选择器所包含的元素里面,找第二个选择器满足的元素*/
  62. }
  63. /*7.兄弟选择器 selector1~selector2 同级往下找弟弟*/
  64. .php~.java{
  65. background: red;
  66. color: yellow;
  67. }
  68. .php~*{
  69. background: red;
  70. color: yellow;
  71. }
  72. /*8.选择器组合 selector1,selector2,selector3 之间是或的关系*/
  73. p,
  74. span,
  75. div{
  76. background: red;
  77. color: yellow;
  78. }
  79. /* 伪元素选择器*/
  80. /*1.首字母 只有块级元素(竖着布局的元素)可以使用*/
  81. div::first-letter{
  82. font-size: 40px;
  83. color: purple;
  84. }
  85. /*2.首行 只有块级元素(竖着布局的元素)可以使用*/
  86. div::first-line{
  87. font-size: 40px;
  88. color: purple;
  89. }
  90. /*像一串英文字母aaa会看做一行,浏览器认为中文可以随意换行,英文单词或者连字符可以换行,如果是连续的字母没有意义,不会换行,可以用下面的进行换行*/
  91. div{
  92. word-break: break-all;
  93. }
  94. /*单词裂开*/
  95. /*3.特效追加 在前面追加*/
  96. div::before{
  97. content: "111";
  98. font-size: 40px;
  99. color: purple;
  100. }
  101. /*4.在元素后面追加 after*/
  102. div::after{
  103. content: "111";
  104. font-size: 40px;
  105. color: purple;
  106. }
  107. /*!!!!需要通过content开辟空间,进行追加*/
  108. /* 伪类选择器*/
  109. /*1.结构性伪类选择器*/
  110. /*括号里 n可以是数字,如果是数字 n从1开始
  111. 可以是单词 even偶数个 odd奇数个
  112. 可以是表达式 2n+1 3n+2 n从0开始
  113. 找第一个 first-child
  114. 找最后一个 last-child
  115. 倒数 nth-last-child()
  116. 正数 nth-child()
  117. 只认数字,如果类型恰好符合,则找到*/
  118. ul li:nth-child(2){
  119. font-size: 40px;
  120. color: purple;
  121. }
  122. ul li:nth-child(2){
  123. font-size: 40px;
  124. color: purple;
  125. }
  126. ul li:nth-child(odd){
  127. font-size: 40px;
  128. color: purple;
  129. }
  130. /*找同类型的 nth-of-type 既认数字,也认类型
  131. 括号里 n可以是数字,如果是数字 n从1开始
  132. 可以是单词 even偶数个 odd奇数个
  133. 可以是表达式 2n+1 3n+2 n从0开始
  134. 找第一个 first-of-type
  135. 找最后一个 last-of-type
  136. 倒数 nth-last-of-type()
  137. 正数 nth-of-type()*/
  138. ul li:nth-of-type(1){
  139. font-size: 40px;
  140. color: purple;
  141. }
  142. /*2.UI状态伪类选择器*/
  143. /*hover鼠标悬停状态*/
  144. /*focus鼠标焦点状态*/
  145. /*checked鼠标选中状态*/
  146. ul li:hover{
  147. font-size: 40px;
  148. color: purple;
  149. }
  150. /*3.其他伪类选择器*/
  151. /*not() 排除 过滤掉某些元素 括号中可以写任何一个选择器*/
  152. ul li:not(.java){
  153. font-size: 40px;
  154. color: purple;
  155. }
  156. ul li:not(.java):not(.php){
  157. font-size: 40px;
  158. color: purple;
  159. }
  160. /*CSS选择器优先级*/
  161. /*1.选择器写得越长越准确,优先级越高*/
  162. /*2.优先级 id选择器>class选择器>元素选择器*/
  163. /*3.同级别选择器下,CSS按照代码顺序执行 后面代码会把前面的覆盖掉*/
  164. /*4.终极规则:以上规则适用于绝大部分场景,特殊场景不适用请自行调试*/

3.CSS选择器详细介绍

基本选择器

1.元素选择器 根据元素名称做选择
  1. <div>hello</div>
  2. <div>hello</div>
  3. <div>hello</div>
  4. <div>hello</div>
  5. <p>hello</p>
  6. div{
  7. background: aqua;
  8. color: black;
  9. }

特例: * 选择所有元素 body html也是元素

  1. *{
  2. background: aqua;
  3. color: black;
  4. }

2.属性选择器 []属性
  1. <div id="e">hello</div>
  2. <div id="ab">hello</div>
  3. <div id="ba">hello</div>
  4. <div id="daad">hello</div>
  5. <div mm="baab">hello</div>
  6. <p>hello</p>
  7. div[id]{
  8. color: rgb(55, 0, 255);
  9. }
  10. div[id=e]{
  11. background: red;
  12. }
  13. div[id$=a]{
  14. background: greenyellow;
  15. }
  16. div[id^=a]{
  17. background: rgb(249, 49, 219);
  18. }
  19. div[id*=a]{
  20. background: rgb(19, 178, 246);
  21. color: yellow;
  22. }
  23. div[mm*=a]{
  24. background: rgb(166, 28, 212);
  25. color: yellow;
  26. }

3.id选择器
  1. #e{
  2. background-color: aqua;
  3. }

id最好唯一

4.class选择器
  1. <div class="a">hello</div>
  2. <div>hello</div>
  3. <div id="ss" class="a">hello</div>
  4. <div>hello</div>
  5. <p>hello</p>
  6. </body>
  7. .a{
  8. background-color: blueviolet;
  9. }

特例,结合选择器

  1. div.a{
  2. background-color: aquamarine;
  3. }
  4. #ss.a{
  5. color: red;
  6. }

5.包含选择器 selector1 selector2* 空格只是强调包含关系
  1. <div>
  2. <p>
  3. hello
  4. </p>
  5. </div>
  6. <p>hello</p>
  7. <p class="java">hello</p>
  8. <p class="java ">hello</p>
  9. <p class="php">hello</p>
  10. <p class="java">hello</p>
  11. <p class="java">hello</p>
  12. div p{
  13. background-color: aqua;
  14. }

6.子选择器 selector1>selector2 强调父子关系
  1. div>p{
  2. color: red;
  3. }
  4. <div>
  5. <p>
  6. hello
  7. </p>
  8. </div>
  9. <div>
  10. <h1>
  11. <p>
  12. hello
  13. </p>
  14. </h1>
  15. </div>
  16. <p>hello</p>
  17. <p class="java">hello</p>
  18. <p class="java ">hello</p>
  19. <p class="php">hello</p>
  20. <p class="java">hello</p>
  21. <p class="java">hello</p>

7.兄弟选择器 selector1~selector2 同级往下找弟弟
  1. <p>hello</p>
  2. <p class="java">hello</p>
  3. <p class="java ">hello</p>
  4. <p class="php">hello</p>
  5. <p class="java">hello</p>
  6. <p class="nn">hello</p>
  7. .php~.java{
  8. background-color: blueviolet;
  9. }
  10. .php~*{
  11. background-color: chartreuse;
  12. }/*全选*/

8.选择器组合 selector1,selector2,selector3 选择器之间是或 的关系,只需要满足一个就可以使用该选择器给的样式
  1. <div>hello</div>
  2. <span>hello</span>
  3. <p>hello</p>
  4. <p>hello</p>
  5. div,
  6. p{
  7. background-color: aqua;
  8. }
  9. div,
  10. span,
  11. p{
  12. color: red;
  13. }

伪元素选择器

1.首字母 只有块级元素(竖着布局的元素)可以使用
  1. div::first-letter{
  2. font-size: 40px;
  3. color: purple;
  4. }

2.首行 只有块级元素(竖着布局的元素)可以使用
  1. <div>
  2. <p>hello</p>
  3. <p>hello</p>
  4. </div>
  5. div::first-line{
  6. font-size: 40px;
  7. color: purple;
  8. }

像一串英文字母aaa会看做一行,浏览器认为中文可以随意换行,英文单词或者连字符可以换行,如果是连续的字母没有意义,不会换行,可以用下面的进行换行

  1. div{
  2. word-break: break-all;
  3. }
  4. /*单词裂开*/
  5. <div>
  6. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  7. </div>
3.特效追加 在前面追加
  1. div::before{
  2. content: "hello";
  3. background-color: aqua;
  4. color: purple;
  5. }
  6. <div>
  7. world
  8. </div>

4.在元素后面追加 after
  1. div::after{
  2. content: "hello";
  3. background-color: aqua;
  4. color: purple;
  5. }

!!!需要通过content开辟空间,进行追加

伪类选择器

1.结构性伪类选择器

括号里 n可以是数字,如果是数字 n从1开始,可以是单词 even偶数个 odd奇数个 可以是表达式 2n+1 3n+2 n从0开始

找第一个 first-child

找最后一个 last-child

倒数 nth-last-child()

正数 nth-child()

只认数字,如果类型恰好符合,则找到

  1. ul li:nth-child(1){
  2. color: blue;
  3. }
  4. ul li:nth-child(2){
  5. color: red;
  6. }/*执行1次*/
  7. ul li:nth-child(2n+1){
  8. font-size: 30px;
  9. }
  10. ul li:nth-child(odd){
  11. background-color:aqua ;
  12. }
  13. ul li:first-child{
  14. background-color:pink ;
  15. }
  16. ul li:last-child{
  17. background-color:rgb(126, 197, 245) ;
  18. }/*执行一次*/
  19. ul li:nth-last-child(even){
  20. background-color: blueviolet;
  21. }/*执行一次*/
  22. <ul>
  23. <li>aaa</li>
  24. <p>aaa</p>
  25. <li>aaa</li>
  26. <li>aaa</li>
  27. <li>aaa</li>
  28. <li>aaa</li>
  29. </ul>

找同类型的 nth-of-type 既认数字,也认类型

括号里 n可以是数字,如果是数字 n从1开始

  1. 可以是单词 even偶数个 odd奇数个
  2. 可以是表达式 2n+1 3n+2 n0开始

找第一个 first-of-type

找最后一个 last-of-type

倒数 nth-last-of-type()

正数 nth-of-type()

  1. ul li:nth-of-type(1){
  2. color: blue;
  3. }
  4. ul li:nth-of-type(2){
  5. color: red;
  6. }/*执行1次*/
  7. ul li:nth-of-type(3n){
  8. font-size: 30px;
  9. }
  10. ul li:nth-of-type(odd){
  11. background-color:aqua ;
  12. }
  13. ul li:first-of-type{
  14. background-color:pink ;
  15. }
  16. ul li:last-of-type{
  17. background-color:rgb(126, 197, 245) ;
  18. }/*执行一次*/
  19. ul li:nth-last-child(3){
  20. background-color: blueviolet;
  21. }/*执行一次*/
  22. <ul>
  23. <li>aaa</li>
  24. <p>aaa</p>
  25. <li>aaa</li>
  26. <li>aaa</li>
  27. <li>aaa</li>
  28. <li>aaa</li>
  29. </ul>

2.UI状态伪类选择器

hover鼠标悬停状态

focus鼠标焦点状态

checked鼠标选中状态

  1. /* focus(焦段状态) hover(悬停状态) */
  2. /* 鼠标放上去之前 */
  3. ul li{
  4. background: red;
  5. }
  6. ul li:hover{
  7. background: blue;
  8. }
  9. /* 原本的原色 */
  10. input{
  11. background: blue;
  12. }
  13. /* 开始输入数据 */
  14. input:focus{
  15. background: red;
  16. }
  17. /* 鼠标悬停 */
  18. input:hover{
  19. background: yellow;
  20. }
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>FLOAT</title>
  5. <style>
  6. ul li{
  7. background-color: red;
  8. }
  9. ul li:hover{
  10. background-color: blue;
  11. }
  12. input{
  13. background-color: blueviolet;
  14. }
  15. input:focus{
  16. background-color: greenyellow;
  17. }
  18. input:hover{
  19. background-color: yellow;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <ul>
  25. <li>aaa</li>
  26. <li>aaa</li>
  27. <li>aaa</li>
  28. <li>aaa</li>
  29. </ul>
  30. <input type="text">
  31. </body>
  32. </html>

悬停状态

聚焦状态

3.其他伪类选择器

not() 排除 过滤掉某些元素 括号中可以写任何一个选择器*/

  1. <ul>
  2. <li>qcby</li>
  3. <li class="java">qcby</li>
  4. <li>qcby</li>
  5. <li class="java">qcby</li>
  6. <li>qcby</li>
  7. <li class="java">qcby</li>
  8. <li>qcby</li>
  9. <li class="php">qcby</li>
  10. </ul>
  11. /* 排除class是.java的 */
  12. li:not(.java){
  13. background: red;
  14. }
  15. li:not(.java):not(.php){
  16. background: red;
  17. }

欢迎大家,点击关注,加评论呦

标签: 前端 css

本文转载自: https://blog.csdn.net/weixin_63708006/article/details/141066927
版权归原作者 YY编程小白 所有, 如有侵权,请联系我们删除。

“前端:CSS选择器(级联/层叠样式单)--用作装饰”的评论:

还没有评论