Tuesday 24 September 2013

Complex Selectors - Part6

:nth-of-type(n) & :nth-last-of-type(n)

The :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes are very similar to that of the :nth-child(n) and :nth-last-child(n) pseudo-classes, however instead of counting every element within a parent the:nth-of-type(n) and :nth-last-of-type(n) pseudo-classes only count elements of their own type. For example, when counting paragraphs within an article, the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes will skip any headings, divisions, or miscellaneous elements that are not paragraphs, while the :nth-child(n)and :nth-last-child(n) would count every element, no matter it’s type, only selecting the ones that match the element within the stated selector. Additionally, all of the same expression possibilities used within the :nth-child(n)and :nth-last-child(n) pseudo-classes are also available within the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes.
Using the :nth-of-type(n) pseudo-class within the p:nth-of-type(3n) selector we are able to identify every third paragraph within a parent, regardless of other sibling elements within the parent. Here the paragraphs on lines 5 and 9 are selected, thus marked bold.
CSS
  1. STRUCTURAL & POSITION PSEUDO-CLASSESp:nth-of-type(3n) {...}
HTML
  1. STRUCTURAL & POSITION PSEUDO-CLASSES<article>
  2. <h1>...</h1>
  3. <p>...</p>
  4. <p>...</p>
  5. <p>...</p>
  6. <h2>...</h2>
  7. <p>...</p>
  8. <p>...</p>
  9. <p>...</p>
  10. </article>
As with the :nth-child(n) and :nth-last-child(n) pseudo-classes, the primary difference between the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes is that the :nth-of-type(n) pseudo-class counts elements from the beginning of the document tree and the :nth-last-of-type(n) pseudo-class counts elements from the end of the document tree.
Using the :nth-last-of-type(n) pseudo-class we can write the p:nth-last-of-type(2n+1) selector which identifies every second paragraph from the end of a parent element starting with the last paragraph. Here the paragraphs on lines 4, 7, and 9 are selected, thus marked bold.
CSS
  1. STRUCTURAL & POSITION PSEUDO-CLASSESp:nth-last-of-type(2n+1) {...}
HTML
  1. STRUCTURAL & POSITION PSEUDO-CLASSES<article>
  2. <h1>...</h1>
  3. <p>...</p>
  4. <p>...</p>
  5. <p>...</p>
  6. <h2>...</h2>
  7. <p>...</p>
  8. <p>...</p>
  9. <p>...</p>
  10. </article>

Target Pseudo-class

The :target pseudo-class is used to style elements when an element’s ID attribute value matches that of the URI fragment identifier. The fragment identifier within a URI can be recognized by the hash character, #, and what directly follows it. The URL http://example.com/index.html#hello includes the fragment identifier of hello. When this identifier matches the ID attribute value of an element on the page, <section id="hello"> for example, that element may be identified and stylized using the :target pseudo-class. Fragment identifiers are most commonly seen when using on page links, or linking to another part of the same page.
Looking at the code below, if a user would visit a page with the URI fragment identifier of #hello the section with that same ID attribute value would be stylized accordingly using the :target pseudo-class. If the URI fragment identifier changes, and matches the ID attribute value of another section, that new section may be stylized using the same selector and pseudo-class from before.
CSS
  1. section:target {...}
HTML
  1. <section id="hello">...</section>

Empty Pseudo-class

The :empty pseudo-class allows elements that do not contain children or text nodes to be selected. Comments, processing instructions, and empty text nodes are not considered children and are not treated as such.
Using the div:empty pseudo-class will identify divisions without any children or text nodes. Below the divisions on lines 2 and 3 are selected, and marked bold, as they are completely empty. Even though the second division contains a comment it is not considered to be a child, thus leaving the division empty. The first division contains text, the third division contains one blank text space, and the last division contains a strong child element, thus they are all ruled out and are not selected.
CSS
  1. div:empty {...}
HTML
  1. <div>Hello</div>
  2. <div><!-- Coming soon. --></div>
  3. <div></div>
  4. <div> </div>
  5. <div><strong></strong></div>

Negation Pseudo-class

The negation pseudo-class, :not(x), is a pseudo-class that takes an argument which is filtered out from the selection to be made. The p:not(.intro) selector uses the negation pseudo-class to identify every paragraph element without the class of intro. The paragraph element is identified at the beginning of the selector followed by the :not(x)pseudo-class. Inside of the parentheses falls the negation selector, the class of .intro in this case.
Below both the div:not(.awesome) and :not(div) selectors use the :not(x) pseudo-class. Thediv:not(.awesome) selector identifies any division without the class of awesome, while the :not(div) selector identifies any element that isn’t a division. As a result the division on line 1 is selected as well as the two sections on lines 3 and 4, thus they are marked bold. The only element not selected is the division with the class of awesome, as it falls outside of the two negation pseudo-classes.
CSS
  1. div:not(.awesome) {...}
  2. :not(div) {...}
HTML
  1. <div>...</div>
  2. <div class="awesome">...</div>
  3. <section>...</section>
  4. <section class="awesome">...</section>

Pseudo-classes Example

HTML
  1. <table>
  2. <thead>
  3. <tr>
  4. <th>Number</th>
  5. <th>Player</th>
  6. <th>Position</th>
  7. <th>Height</th>
  8. <th>Weight</th>
  9. <th>Birthday</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <tr>
  14. <td>8</td>
  15. <td>Marco Belinelli</td>
  16. <td>G</td>
  17. <td>6-5</td>
  18. <td>195</td>
  19. <td>03/25/1986</td>
  20. </tr>
  21. <tr>
  22. <td>5</td>
  23. <td>Carlos Boozer</td>
  24. <td>F</td>
  25. <td>6-9</td>
  26. <td>266</td>
  27. <td>11/20/1981</td>
  28. </tr>
  29. <tr>
  30. <td>21</td>
  31. <td>Jimmy Butler</td>
  32. <td>G-F</td>
  33. <td>6-7</td>
  34. <td>220</td>
  35. <td>09/14/1989</td>
  36. </tr>
  37. ...
  38. </tbody>
  39. </table>
CSS
  1. table {
  2. border-spacing: 0;
  3. width: 100%;
  4. }
  5. th {
  6. background: #404853;
  7. background: linear-gradient(#687587, #404853);
  8. border-left: 1px solid rgba(0, 0, 0, 0.2);
  9. border-right: 1px solid rgba(255, 255, 255, 0.1);
  10. color: #fff;
  11. padding: 8px;
  12. text-align: left;
  13. text-transform: uppercase;
  14. }
  15. th:first-child {
  16. border-top-left-radius: 4px;
  17. border-left: 0;
  18. }
  19. th:last-child {
  20. border-top-right-radius: 4px;
  21. border-right: 0;
  22. }
  23. td {
  24. border-right: 1px solid #c6c9cc;
  25. border-bottom: 1px solid #c6c9cc;
  26. padding: 8px;
  27. }
  28. td:first-child {
  29. border-left: 1px solid #c6c9cc;
  30. }
  31. tr:first-child td {
  32. border-top: 0;
  33. }
  34. tr:nth-child(even) td {
  35. background: #e8eae9;
  36. }
  37. tr:last-child td:first-child {
  38. border-bottom-left-radius: 4px;
  39. }
  40. tr:last-child td:last-child {
  41. border-bottom-right-radius: 4px;
  42. }

Demo

NUMBERPLAYERPOSITIONHEIGHTWEIGHTBIRTHDAY
8Marco BelinelliG6-519503/25/1986
5Carlos BoozerF6-926611/20/1981
21Jimmy ButlerG-F6-722009/14/1989
9Luol DengF6-922004/16/1985
22Taj GibsonF6-922506/24/1985
32Richard HamiltonG6-719302/14/1978
12Kirk HinrichG6-419001/02/1981
48Nazr MohammedC6-1025009/05/1977
13Joakim NoahC6-1123202/25/1985
77Vladimir RadmanovicF6-1023511/19/1980
2Nate RobinsonG5-918005/31/1984
1Derrick RoseG6-319010/04/1988
25Marquis TeagueG6-219002/28/1993