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