博客列表 >9.23作业-伪类等写法解析

9.23作业-伪类等写法解析

鬼才哥-秋兜
鬼才哥-秋兜原创
2021年09月26日 19:37:03491浏览

所有代码的注释和介绍

  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>9.23作业</title>
  8. <link rel="stylesheet" href="/PHP/ico/iconfont.css" />
  9. <script src="/PHP/ico/iconfont.js"></script>
  10. </head>
  11. <body>
  12. <!-- 选择器-伪类写法要领和实例 -->
  13. <!-- ↓------------------------------>
  14. <!-- 方式一:
  15. 普通的针对样式写法
  16. -->
  17. <h2>实例一</h2>
  18. <ul class="a">
  19. <li>b</li>
  20. <!-- ↓指定该位置样式↓ -->
  21. <li class="c">c</li>
  22. <li>d</li>
  23. </ul>
  24. <style>
  25. .a .c {color: #FF0000;};
  26. </style>
  27. <hr>
  28. <!-- ↓------------------------------>
  29. <!-- 方式二:
  30. 针对指定代码行数做样式写法
  31. (父子级下指定位置都生效)
  32. -->
  33. <h2>实例二</h2>
  34. <ul class="aa">
  35. <li>b</li>
  36. <!-- ↓指定该位置样式↓ -->
  37. <li>c</li>
  38. <li>d</li>
  39. </ul>
  40. <style>
  41. /* :nth-of-t.ype(2) 其中的2代表定位到该位置 */
  42. .aa :nth-of-type(2) {color: #FFFF00;};
  43. </style>
  44. <hr>
  45. <!-- ↓------------------------------>
  46. <!-- 方式三:
  47. 针对指定代码行数做样式写法
  48. (不想让子级的第二个也被选中的,且指定父级用的写法)
  49. -->
  50. <h2>实例三</h2>
  51. <ul class="aaa">
  52. <li>b</li>
  53. <!-- ↓指定该位置样式↓ -->
  54. <li>c</li>
  55. <li>d</li>
  56. <li>
  57. b
  58. <ul>
  59. <li>b1</li>
  60. <!-- ↓指定该位置不享受样式↓ -->
  61. <li>b2</li>
  62. <li>b3</li>
  63. </ul>
  64. </li>
  65. </ul>
  66. <style>
  67. /* 下面这个写法就是会把所有的下级有2个元素的都样式了 */
  68. /* .aaa :nth-of-type(2) {color: #0000FF;}; */
  69. /* 只要在a的类前面加个>即可 忽略了下级了 */
  70. .aaa > :nth-of-type(2) {
  71. color: #0000FF;
  72. /* background-color: #0000FF */
  73. };
  74. </style>
  75. <hr>
  76. <!-- ↓------------------------------>
  77. <!-- 方式四:
  78. : nth-of-type(X)
  79. 1.定位是有个匹配机制,先组合元素再进行分配定位
  80. 2.标签可不限制排列在一起,因为浏览器会自动组合在匹配
  81. 3.想指定元素来定位就要在:nth-of-t.ype(X) :前面加是标签
  82. (以下方为例,我写加个p标签想定位2就要写2个p标签才可以)
  83. -->
  84. <h2>实例四</h2>
  85. <ul class="aaaa">
  86. <li>b</li>
  87. <!-- ↓指定该位置2样式↓ -->
  88. <li>c</li>
  89. <li>d</li>
  90. <li>b</li>
  91. <p>e</p>
  92. <!-- ↓指定该位置2样式↓ -->
  93. <p>r</p>
  94. </li>
  95. </ul>
  96. <style>
  97. /* 只要在a的类前面加个>即可 忽略了下级了 */
  98. .aaaa > p:nth-of-type(2) {color: #00FF00;};
  99. </style>
  100. <hr>
  101. <!-- ↓------------------------------>
  102. <!-- 方式五:
  103. 如果下指定同级、父级或下级的代码有样式不想都父子级都显示就用下面写法
  104. :nth-of-type(行数):not(标签:nth-of-type(行数))
  105. -->
  106. <h2>实例五</h2>
  107. <ul class="a5">
  108. <li>b</li>
  109. <!-- ↓指定排查li的2位置样式↓ -->
  110. <li>c</li>
  111. <li>d</li>
  112. <li>b</li>
  113. <p>p1</p>
  114. <!-- ↓指定显示p的2位置样式↓ -->
  115. <p>p2</p>
  116. <p>p3</p>
  117. </ul>
  118. <style>
  119. .a5 > :nth-of-type(2):not(li:nth-of-type(2)) {
  120. background-color: #0000FF
  121. }
  122. </style>
  123. <hr>
  124. <!-- ↓------------------------------>
  125. <!-- 方式六:
  126. 针对指定位置的写法
  127. -->
  128. <h2>实例六</h2>
  129. <ul class="a6">
  130. <li>b</li>
  131. <li>c</li>
  132. <li>d</li>
  133. <li>e</li>
  134. <li>f</li>
  135. <li>g</li>
  136. </ul>
  137. <style>
  138. /* 抓取第一个来样式
  139. 有专门的选择器写法,也可输入行数
  140. */
  141. /* 方法1: */
  142. /* .a6 > :nth-of-type(1) {
  143. background-color: #0000FF
  144. } */
  145. /* 方法2: */
  146. /* .a6 > :first-of-type {
  147. background-color: #0000FF
  148. } */
  149. /* 抓取最后一个来样式选择器 */
  150. /* .a6 > :last-of-type {
  151. background-color: #0000FF
  152. } */
  153. /* 倒数上去的获取选择器写法 */
  154. .a6 > li:nth-last-of-type(3) {
  155. background-color: #0000FF
  156. }
  157. </style>
  158. <hr>
  159. <h2>伪类选择器的参数</h2>
  160. <ul class="b1">
  161. <li>yanshi</li>
  162. <li>yanshi</li>
  163. <li>yanshi</li>
  164. <li class="bb">yanshi</li>
  165. <li>yanshi</li>
  166. <li>yanshi</li>
  167. </ul>
  168. <style>
  169. /* :nth-of-type(参数)
  170. 公式:
  171. 参数 = an+b a,n.b =[0,1,2,3,4,5,....]
  172. a: 系数,n:计数器/方式,b:偏移量/行数 用于PHP分页参数居多
  173. 元素的有效编号:必须从1开始,n和b都是从0开始
  174. */
  175. /* 以下写法是说明在第三行做样式(0n=单选+3=第二行) */
  176. /* .b1 > :nth-of-type(0n+2) {
  177. background-color: #0000FF
  178. } */
  179. /* 以下写法是全部选中做样式最大权重(n=全选) */
  180. /* .b1 > :nth-of-type(n) {
  181. background-color: #0000FF
  182. } */
  183. /* 以下写法是从第二行起下面都做样式(n=全选+2=第二行)
  184. 原本写法是1n+2,但因为1*所有数字都不变,所以1可以不用写直接用n即可
  185. */
  186. /* .b1 > :nth-of-type(n+2) {
  187. background-color: #0000FF
  188. } */
  189. /* 以下写法是每隔2行就做样式(2n=偶数进行) 以此类推场景*/
  190. /* .b1 > :nth-of-type(2n) {
  191. background-color: #0000FF
  192. } */
  193. /* 偶数简便的写法(even=偶数) */
  194. /* .b1 > :nth-of-type(even) {
  195. background-color: #0000FF
  196. } */
  197. /* 奇数简便的写法(2n=偶数+1=奇数开始)
  198. 简写是:nth-of-type(odd)
  199. */
  200. /* .b1 > :nth-of-type(2n+1) {
  201. background-color: #0000FF
  202. } */
  203. /* 以下写法是抓取前三做样式(-n)
  204. 算法:
  205. -1*0+3=3
  206. -1*1+3=2
  207. -1*2+3=1
  208. -1*3+3=0(非法索引,匹配就结束了)
  209. */
  210. /* .b1 > :nth-of-type(-n+3) {
  211. background-color: #0000FF
  212. } */
  213. /* 以下写法是抓取后三做样式,换选择器(-n)
  214. 算法:
  215. -1*0+3=3
  216. -1*1+3=2
  217. -1*2+3=1
  218. -1*3+3=0(非法索引,匹配就结束了)
  219. */
  220. .b1 > :nth-last-of-type(-n+3) {
  221. background-color: #0000FF
  222. }
  223. /* 总结:
  224. 1.获取指定的某一个:(b)
  225. 2.获取前几个:(n-b)
  226. 3.获取指定位置后的全部元素:(n+b)
  227. 4.获取全部偶数(2n或even)或全部奇数(2n+1或odd)元素 */
  228. /* 以下写法是针对根元素来做样式 */
  229. /* 写法1:*/
  230. /* html {background-color: #0000FF} */
  231. /* 写法2:*/
  232. /* :root {background-color: #0000FF} */
  233. /* 或者用class来定位写法也可以 第三行class如下: */
  234. /* 写法1: */
  235. /* .b1 .bb ~ * {样式代码} */
  236. /* 写法2: */
  237. /* .b1 .bb ~ li {样式代码} */
  238. /* 以上写法要增加class才可触发 */
  239. </style>
  240. <hr>
  241. <h2>表单的激活/禁用样式控制</h2>
  242. 激活:<input type="text">
  243. <br>
  244. 禁用:<input type="text" disabled>
  245. <style>
  246. /* 以下写法是针对表单的激活/禁用 的框做样式 */
  247. /* 激活状态 */
  248. input:enabled {background-color: #0000FF}
  249. /* 禁用状态 */
  250. input:disabled {background-color: #8b062e}
  251. </style>
  252. <!-- 字体图标 -->
  253. <hr>
  254. <h2>字体图标</h2>
  255. <h4>CSS方式引用</h4>
  256. <span class="iconfont icon-jingdong">JDlogo</span>
  257. <style>
  258. .iconfont.icon-jingdong {
  259. color: rgb(255, 0, 0);
  260. font-size: 500%;
  261. }
  262. </style>
  263. <br>
  264. <h4>JS方式引用</h4>
  265. <svg class="icon" aria-hidden="true">
  266. <use xlink:href="#icon-jingdongicon-"></use>
  267. </svg>
  268. </body>
  269. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议