博客列表 >css选择器的简述和使用

css选择器的简述和使用

景云
景云原创
2020年12月21日 18:26:25698浏览

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>CSS基础学习</title>
  7. <!--
  8. css的引入方式二:内嵌式
  9. 通过style标签引入的css规则,仅适用于当前html页面
  10. -->
  11. <style>
  12. /* 标签选择器 */
  13. p{
  14. color:aqua;
  15. }
  16. #p{
  17. color: bisque;
  18. }
  19. /* 上下文选择器:1、空格:所有层级;2:>:父子级;3:+:相邻的兄弟;4:~:相邻后面所有的兄弟 */
  20. /* 后代选择器:所有层级 */
  21. ul li{
  22. background-color: red;
  23. }
  24. /* 子代选择器:仅父子层级 */
  25. body>ul>li{
  26. background-color: blue;
  27. }
  28. /* 同级相邻选择器:仅选中与之相邻的第一个兄弟元素 */
  29. /* .item1+*{ */
  30. .item1+li{
  31. background-color: green;
  32. }
  33. /* 同级所有选择器:选中与之相邻的后面所以兄弟元素 */
  34. .item2 ~li{
  35. background-color: black;
  36. }
  37. /* 伪类选择器 */
  38. /* 1.匹配任意位置的元素:`:nth-of-type(an+b)`
  39. an:起点;b:偏移量;a b=0,1,2,3... ;
  40. 0n+b可只写b,因为0乘任何数都是0,例如 ol li:nth-of-type(2)代表第二个元素
  41. 1n代表全部元素,1可以省略,因为1乘以任何数都是本身,1n+b代表从第三个元素开始的全部元素,
  42. 2n+3代表从第三个元素开始偏移,之后每再加2的元素都会被选中;就是3,5,7...
  43. 3n+2跟上面一样类推。
  44. */
  45. /* ol li:nth-of-type(2n+3){
  46. background-color: red;
  47. } */
  48. /* 2.反向获取任意位置元素`:nth-last-of-type()`
  49. 规律如正相匹配一致
  50. */
  51. /* ol li:nth-last-of-type(-1n+3){
  52. background-color: burlywood;
  53. } */
  54. /* 3.选择所有索引为奇数的元素
  55. odd也可以
  56. ol li:nth-of-type(2n+1)也可以。
  57. */
  58. /* ol li:nth-of-type(2n-1){
  59. background-color: chocolate;
  60. } */
  61. /* 4.选择所有索引为偶数的元素
  62. 也可用2n+1*/
  63. ol li:nth-of-type(even){
  64. background-color: cornsilk;
  65. }
  66. </style>
  67. <!--
  68. css的引入方式三:外链式
  69. 使用link标签引入外部公共样式表
  70. -->
  71. <link rel="stylesheet" href="style.css">
  72. </head>
  73. <body>
  74. <!-- 要求:1. 实例演示css规则的三种引入到html文档中的方式; 2. 实例演示标签选择器,class选择器,id选择器,上下文选择器, 结构伪类选择器,重点是结构伪类选择器 -->
  75. <!--
  76. css的引入方式一:行内式
  77. 规则仅对当前行有效
  78. -->
  79. <h3>1.CSS的引入方式</h3>
  80. <p style="color:red;">css引入方式一</p>
  81. <p>css引入方式二</p>
  82. <p class="p">css引入方式三+class选择器</p>
  83. <p id="p">id选择器</p>
  84. <h3>2.上下文选择器</h3>
  85. <ul>
  86. <li class="item1">item1</li>
  87. <li class="item2">item2</li>
  88. <li>item3</li>
  89. <li>item4
  90. <ul>
  91. <li>item4-1</li>
  92. <li>item4-2</li>
  93. <li>item4-3</li>
  94. </ul>
  95. </li>
  96. <li>item5</li>
  97. </ul>
  98. <h3>3.伪类选择器:结构伪类相关;难点重点</h3>
  99. <ol>
  100. <li>itemo-1</li>
  101. <li>itemo-2</li>
  102. <li>itemo-3</li>
  103. <li>itemo-4</li>
  104. <li>itemo-5</li>
  105. <li>itemo-6</li>
  106. <li>itemo-7</li>
  107. <li>itemo-8</li>
  108. </ol>
  109. <ol>
  110. <li>ol2-li</li>
  111. </ol>
  112. </body>
  113. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议