16.7. Find elements by computed style
Topics
This question is in
16. DOM: finding elementsSelectors find elements by what the HTML says: tags, classes, attributes. Sometimes the question is about what the browser actually shows: "which elements are display: none right now?", "which text is red?", "what still uses the old brand color?". The answer is in the computed style, the final value of each property after every style sheet, inline style, default style and inherited value has been applied. Audit scripts, test tools and design-token migrations search the page this way.
Write findByStyle(root, property, value) in by-style.js. It returns every element inside root whose computed value for property is the value value computes to.
Signature
function findByStyle(root, property, value) // returns Element[]root: an element in the page.rootitself is not included.property: a CSS property name as written in CSS, such as"color","font-weight"or"margin-top".value: a CSS value in any form the browser accepts for that property:"red","#f00","bold","1rem".- Returns the matching descendants of
root, in document order.
Requirements
- The result is an array of every matching descendant of
root, at any depth, in document order, and neverrootitself. - Styles count wherever they come from: style sheets, the style attribute, and the browser's own defaults (a
<b>is bold). - Values are compared the way the browser computes them, not as text:
"red"matches#f00andrgb(255 0 0);"bold"matches700;"16px"matches1remand12pt;".5"matches an opacity of0.5. The page's own style sheets never change whatvaluemeans: with a rule likediv { color: blue !important }in the page,"red"still means red. - Inherited values count: a
<p>inside an orange section has an orangecolor. Properties that are not inherited, likebackground-color, are not passed to children. - Each element's own value counts, even inside a hidden parent: a
<p>inside adisplay: nonebox still hasdisplay: block. - SVG elements are searched too.
- A value or property the browser does not accept matches nothing.
- The page is left as it was, and a few thousand elements are searched quickly.
Examples
// <style>.hidden { display: none }</style>
// <main id="main">
// <p id="a" class="hidden">One</p>
// <p id="b" style="color: #f00">Two <b id="c">three</b></p>
// </main>
const main = document.getElementById("main");
findByStyle(main, "display", "none"); // [p#a]
findByStyle(main, "color", "red"); // [p#b, b#c]: the <b> inherits the color
findByStyle(main, "font-weight", "bold"); // [b#c]: from the browser's default stylesfindByStyle(main, "color", "not-a-color"); // []
findByStyle(main, "colour", "red"); // []Environment
- Plain JavaScript in a browser page; the tests call the global
findByStylefromby-style.js. rootis always in the page. Values that depend on where an element sits, such as percentages andemlengths, are out of scope, and so are custom properties (--name).- Any DOM and CSSOM API is allowed:
getComputedStyle,element.style,children,attachShadow.
Hints
- The browser can tell you what
"red"computes to. Where could you ask it, without changing any element the caller owns? - If the browser rejects a value, what does
element.style.getPropertyValuereturn for it?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it