I am trying to revise for my exam on HTML, JavaScript and CSS3; and I am a little confused at CSS selectors and which take priority.
I have the following CSS:
table { border: 1px solid black; }
tr:nth-child(odd) { background-color: red; }
tr td:nth-of-type(even) { background-color: blue; }
td { background-color: green; }
I thought that whatever comes last takes priority, so in my opinion all cells in the table would be green.
However the even cells are still blue, as per the nth-of-type selector. Even when I put this to the top and remove the green td line, the blue is still shown in the middle with only the odd cells showing in red.
Can someone explain why the nth-of-type seems to take precedent over everything else?
Here is the example:
table { border: 1px solid black; }
tr:nth-child(odd) { background-color: red; }
tr td:nth-of-type(even) { background-color: blue; }
td { background-color: green; }
<table style="width: 100%;">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
https://jsfiddle.net/e6157xwz/
Use just * {margin:0; box-sizing: border-box;} as your Quick Reset. (I usually like the default Paddings so no need to use padding: 0; but you can add that one too).
The nth-of-type is very similar to the nth-child pseudo-class. The main difference is that it specifically considers the type of the element getting selected before checking any other logic. Let's use our example from above but apply nth-of-type instead.
The :nth-of-type(n) selector matches every element that is the nth child, of the same type (tag name), of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.
As a special case for increasing specificity, you can duplicate weights from the CLASS or ID columns. Duplicating id, class, pseudo-class or attribute selectors within a compound selector will increase specificity when overriding very specific selectors over which you have no control.
What's going on here is selector specificity.
The following list of selector types is by increasing specificity:
- Universal selectors (e.g.
*
)- Type selectors (e.g.
h1
)
Class selectors (e.g..example
)
Attribute selectors (e.g.[type="radio"]
)- Pseudo-classes (e.g.,
:hover
)- ID selectors (e.g.
#example
)- Inline style (e.g.
style="font-weight:bold"
)
In your example, the pseudo selector :nth-of-type(even/odd)
is category 3 which takes precedence since the concurring selectors are only type selectors
(category 2.1).
Deviating from MDN information available behind that link, I have corrected that type, class and attribute selectors actually have the same specificity.
The rule with the blue is more specific and that is why it takes precedence.
Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With