{"id":336866,"date":"2021-03-23T08:06:11","date_gmt":"2021-03-23T15:06:11","guid":{"rendered":"https:\/\/css-tricks.com\/?page_id=336866"},"modified":"2021-07-14T12:13:18","modified_gmt":"2021-07-14T19:13:18","slug":"where","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/w\/where\/","title":{"rendered":":where"},"content":{"rendered":"\n
The For example:<\/p>\n\n\n\n Matches as if you wrote that selector like this:<\/p>\n\n\n\n …except the specificity of the first example above is (0, 0, 0, 1) for the single element selector ( It’s a tough call deciding if you want the specificity of Imagine a classic layout:<\/p>\n\n\n\n If I want to select the links in the header and footer, I could…<\/p>\n\n\n\n That selector is (0, 0, 1, 0), meaning if I were to write:<\/p>\n\n\n\n That selector is less specific at (0, 0, 0, 2), and will not override the other. But if I wrote:<\/p>\n\n\n\n That is only (0, 0, 0, 1), so my override will<\/em> work.<\/p>\n\n\n\n Perhaps a less-contrived example is selecting a header by ID without having to resort to ID-level specificity. <\/p>\n\n\n\n There I’ve selected a header with a specific ID. IDs have very high specificity\u2009\u2014\u2009(0, 1, 0, 0)\u2009\u2014\u2009which is hard to override, but here I’ve managed to select just that header<\/em>, but not use any super high specificity to do it. <\/p>\n\n\n Each selector within the comma-separated list within :where()<\/code> pseudo selector in CSS is functionally identical to the
:is()<\/code> psuedo selector<\/a> in that it takes a comma-separated list of selectors to match against, except that where
:is()<\/code> takes the most specific<\/em> among them as the specificity of that whole part,
the specificity of :where()<\/code> is always zero (0)<\/strong>.<\/p>\n\n\n\n\n\n\n\n
main :where(h1, h2, h3) {\n color: orange;\n}<\/code><\/pre>\n\n\n\n
main h1, main h2, main h3 {\n color: orange;\n}<\/code><\/pre>\n\n\n\n
main<\/code>) and doesn’t include the specificity of anything in the
:where()<\/code> part. Whereas, in the second example, the specificity is (0, 0, 0, 2) because there are two element selectors.<\/p>\n\n\n
Deciding on specificity handling<\/h3>\n\n\n
:is()<\/code> or the zero-specificity of
:where()<\/code>, although I might argue that keeping specificity low in general leads to fewer headaches than working with elements that have a higher specificity. <\/p>\n\n\n\n
<header class=\"header\"><\/header>\n<main class=\"main\"><\/header>\n<aside class=\"aside\"><\/aside>\n<footer class=\"footer\"><\/footer><\/code><\/pre>\n\n\n\n
:is(.header, .footer) a {\n color: red;\n}<\/code><\/pre>\n\n\n\n
header a {\n color: orange;\n}<\/code><\/pre>\n\n\n\n
:where(.header, .footer) a {\n color: red;\n}<\/code><\/pre>\n\n\n\n
h3:where(#specific-header) {\n outline: 1px solid yellow;\n}<\/code><\/pre>\n\n\n\n
Forgiving selectors<\/h3>\n\n\n
:where(x, y, z)<\/code> is forgiving in that it is ignored if it is invalid. That is important because it doesn’t wipe out the entire selector like invalid selectors normally do<\/a>. For more information, see how it works with the
:is()<\/code> selector<\/a> since it is identical. <\/p>\n\n\n\n
:where(h1, totally:fake) {\n \/* will still select h1 *\/\n}<\/code><\/pre>\n\n\n
Browser support<\/h3>\n\n\n
IE<\/th> Edge<\/th> Firefox<\/th> Chrome<\/th> Safari<\/th> Opera<\/th><\/tr><\/thead> No<\/td> 88<\/td> 78<\/td> 88<\/td> 14<\/td> 74<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n Android Chrome<\/th> Android Firefox<\/th> Android Browser<\/th> iOS Safari<\/th> Opera Mobile<\/th><\/tr><\/thead> All<\/td> All<\/td> 91<\/td> 14.4<\/td> No<\/td><\/tr><\/tbody><\/table> More information<\/h3>\n\n\n \t\t\n