There’s a CSS3 pseudo-class out there called :only-child
that I didn’t think I would ever need to use. The Mozilla Developer Network says it, “represents any element which is the only child of its parent. This is the same as :first-child:last-child
or :nth-child(1):nth-last-child(1)
, but with a lower specificity.”
When working recently on an overhaul of Kingston.com, however, I did find a need to use this pseudo-class. In the Company pages there are various tables of information. When the table uses just one row in the THEAD
that row gets a powder blue background color. But when there are two rows in the THEAD
the first row gets a lighter shade of background color while the second row gets the same color that a row will get if it’s the only child in a THEAD
. The only way to get the only header row in one instance and the second header row in another instance to match, without resorting to class names or deprecated HTML, was to use the :only-child
pseudo-class, like so:
- THEAD TR:nth-child(even),
- THEAD TR:only-child
- {
- background-color: #CCE5FF;
- }
And here’s the CodePen I created to showcase the effect: