css_nth_child.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>CSS Selector :nth-child</title>
  5. <style type="text/css">
  6. p {
  7. margin: 2px;
  8. }
  9. div {
  10. margin-bottom: 1em;
  11. border: 1px solid #ddd;
  12. }
  13. div div {
  14. margin: 0;
  15. }
  16. .nth-child-1 p:nth-child(1) strong,
  17. .nth-child-3 p:nth-child(3) strong,
  18. .nth-child-odd p:nth-child(odd) strong,
  19. .nth-child-even p:nth-child(even) strong,
  20. .nth-child-n p:nth-child(n) strong,
  21. .nth-child-3n p:nth-child(3n) strong,
  22. .nth-child-n2 p:nth-child(n+2) strong, /* won't work because of the split("+") */
  23. .nth-child-2n1 p:nth-child(2n+1) strong, /* won't work because of the split("+") */
  24. .nth-child-3nm2 p:nth-child(3n-2) strong {
  25. background-color: #99ff99;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h3>nth-child(1)</h3>
  31. <div class="nth-child-1">
  32. <p><strong>1</strong></p>
  33. <p><strong>2</strong></p>
  34. <p><strong>3</strong></p>
  35. <p><strong>4</strong></p>
  36. <p><strong>5</strong></p>
  37. </div>
  38. <h3>nth-child(3)</h3>
  39. <div class="nth-child-3">
  40. <p><strong>1</strong></p>
  41. <p><strong>2</strong></p>
  42. <p><strong>3</strong></p>
  43. <p><strong>4</strong></p>
  44. <p><strong>5</strong></p>
  45. </div>
  46. <h3>nth-child(odd)</h3>
  47. <div class="nth-child-odd">
  48. <p><strong>1</strong></p>
  49. <p><strong>2</strong></p>
  50. <p><strong>3</strong></p>
  51. <p><strong>4</strong></p>
  52. <p><strong>5</strong></p>
  53. </div>
  54. <h3>nth-child(even)</h3>
  55. <div class="nth-child-even">
  56. <p><strong>1</strong></p>
  57. <p><strong>2</strong></p>
  58. <p><strong>3</strong></p>
  59. <p><strong>4</strong></p>
  60. <p><strong>5</strong></p>
  61. </div>
  62. <h3>nth-child(n)</h3>
  63. <div class="nth-child-n">
  64. <p><strong>1</strong></p>
  65. <p><strong>2</strong></p>
  66. <p><strong>3</strong></p>
  67. <p><strong>4</strong></p>
  68. <p><strong>5</strong></p>
  69. </div>
  70. <h3>nth-child(3n)</h3>
  71. <div class="nth-child-3n">
  72. <p><strong>1</strong></p>
  73. <p><strong>2</strong></p>
  74. <p><strong>3</strong></p>
  75. <p><strong>4</strong></p>
  76. <p><strong>5</strong></p>
  77. </div>
  78. <h3>nth-child(n+2)</h3>
  79. <div class="nth-child-n2">
  80. <p><strong>1</strong></p>
  81. <p><strong>2</strong></p>
  82. <p><strong>3</strong></p>
  83. <p><strong>4</strong></p>
  84. <p><strong>5</strong></p>
  85. </div>
  86. <h3>nth-child(2n+1)</h3>
  87. <div class="nth-child-2n1">
  88. <p><strong>1</strong></p>
  89. <p><strong>2</strong></p>
  90. <p><strong>3</strong></p>
  91. <p><strong>4</strong></p>
  92. <p><strong>5</strong></p>
  93. </div>
  94. <h3>nth-child(3n-2)</h3>
  95. <div class="nth-child-3nm2">
  96. <p><strong>1</strong></p>
  97. <p><strong>2</strong></p>
  98. <p><strong>3</strong></p>
  99. <p><strong>4</strong></p>
  100. <p><strong>5</strong></p>
  101. </div>
  102. </body>
  103. </html>