博客列表 >结构伪类选择器:不分组(不区分元素类型)

结构伪类选择器:不分组(不区分元素类型)

霏梦
霏梦原创
2020年06月16日 08:48:07532浏览

- 作者:霏梦

  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>结构伪类选择器:不分组(不区分元素类型)</title>
  7. <style>
  8. body {
  9. width: 600px;
  10. text-align: center;
  11. }
  12. /* 匹配第一个子元素 */
  13. .h3-title > *:first-child {
  14. font-size: 30px;
  15. color: blueviolet;
  16. }
  17. .h3-title > .wsp:first-child {
  18. font-size: 30px;
  19. color: limegreen;
  20. }
  21. /* 最后一个元素 */
  22. .h3-title > :last-child {
  23. font-size: 40px;
  24. color: magenta;
  25. }
  26. /* 选第3个 */
  27. .h3-title > :nth-child(3) {
  28. border: 2px solid coral;
  29. background-color: cornflowerblue;
  30. }
  31. /* 只选择偶数单元格 2n or even */
  32. .h3-title > :nth-child(2n) {
  33. background-color: crimson;
  34. }
  35. /* 获取奇数单元格 odd or 2n-1 */
  36. .h3-title > :nth-child(2n-1) {
  37. font-size: 50px;
  38. }
  39. /* 从指定位置开始,选择剩下的所有元素 */
  40. .h3-title > .wsp:nth-child(n + 4) {
  41. color: goldenrod;
  42. }
  43. /* 只取前3 */
  44. .h3-title > .wsp:nth-child(-n + 3) {
  45. color: green;
  46. }
  47. /* 只取最后3个 */
  48. .h3-title > .wsp:nth-last-child(-n + 3) {
  49. background-color: greenyellow;
  50. }
  51. /* 取第8行 用倒数*/
  52. .h3-title > .wsp:nth-last-child(2) {
  53. background-color: rebeccapurple;
  54. font-size: 80px;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <div class="h3-title">
  60. <div class="wsp">我是第1行</div>
  61. <div class="wsp">我是第2行</div>
  62. <div class="wsp">我是第3行</div>
  63. <div class="wsp">我是第4行</div>
  64. <div class="wsp">我是第5行</div>
  65. <div class="wsp">我是第6行</div>
  66. <div class="wsp">我是第7行</div>
  67. <div class="wsp">我是第8行</div>
  68. <div class="wsp">我是第9行</div>
  69. </div>
  70. </body>
  71. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议