Shorten verbose CSS that repeats combinations of elements and pseudo-classes

You can combine the selectors using :is() and comma , .tr1 th:is(:nth-child(1),:nth-child(5)), :is(.tr2,.tr4) td:is(:nth-child(2),:nth-child(4)), .tr3 td:nth-child(3) {background-color:green} .tr1 th:is(:nth-child(1),:nth-child(5)):hover, :is(.tr2,.tr4) td:is(:nth-child(2),:nth-child(4)):hover, .tr3 td:nth-child(3):hover {background-color:hotpink} .tr1 th:is(:nth-child(1),:nth-child(5))::selection, :is(.tr2,.tr4) td:is(:nth-child(2),:nth-child(4))::selection, .tr3 td:nth-child(3)::selection {color:hotpink}

“querySelectorAll()” with multiple conditions in JavaScript

Is it possible to make a search by querySelectorAll using multiple unrelated conditions? Yes, because querySelectorAll accepts full CSS selectors, and CSS has the concept of selector groups, which lets you specify more than one unrelated selector. For instance: var list = document.querySelectorAll(“form, p, legend”); …will return a list containing any element that is a … Read more

How to do a wildcard element name match with “querySelector()” or “querySelectorAll()” in JavaScript?

[id^=’someId’] will match all ids starting with someId. [id$=’someId’] will match all ids ending with someId. [id*=’someId’] will match all ids containing someId. If you’re looking for the name attribute just substitute id with name. If you’re talking about the tag name of the element I don’t believe there is a way using querySelector

selector for nth nested elements

In CSS the selector string is largely describing the nesting structure, and there does not currently exist any generational skipping selectors such that you might theoretically do something like .item:nth-grandchild(4) to replace your fifth example. If reducing verbosity of your css is of high importance to you (lets say you have up 10 or even … Read more

CSS selector based on width?

Is there a CSS rule that would match elements based on their width? No. If not, would it be interesting to propose that in the standard? Maybe, but not the Selectors standard, because Selectors is not intended to be tightly coupled with the styling aspect of CSS. There were proposals to standardize something called “element … Read more

CSS nth-match doesn’t work

Where’s the problem? The problem is that you’re treating ideas and proposals in early-stage drafts as actual features, by using them with the expectation that they’d work at all, let alone across browsers. Review the status of the WD: This is a draft document and may be updated, replaced or obsoleted by other documents at … Read more

select second child

Make sure your <li> actually has two <a> elements as its first two children, and not some other elements around them since those may be invalidating the :first-child and adjacent sibling selectors. If you just want to select the second <a> element regardless of what other types of elements there are in your <li>, use … Read more