Skip to curriculum
Jump to the editor

16.7. Find elements by computed style

Medium
Topics

This question is in

16. DOM: finding elements
20–30 min

Selectors 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

JAVASCRIPT
function findByStyle(root, property, value) // returns Element[]
  • root: an element in the page. root itself 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

  1. The result is an array of every matching descendant of root, at any depth, in document order, and never root itself.
  2. Styles count wherever they come from: style sheets, the style attribute, and the browser's own defaults (a <b> is bold).
  3. Values are compared the way the browser computes them, not as text: "red" matches #f00 and rgb(255 0 0); "bold" matches 700; "16px" matches 1rem and 12pt; ".5" matches an opacity of 0.5. The page's own style sheets never change what value means: with a rule like div { color: blue !important } in the page, "red" still means red.
  4. Inherited values count: a <p> inside an orange section has an orange color. Properties that are not inherited, like background-color, are not passed to children.
  5. Each element's own value counts, even inside a hidden parent: a <p> inside a display: none box still has display: block.
  6. SVG elements are searched too.
  7. A value or property the browser does not accept matches nothing.
  8. The page is left as it was, and a few thousand elements are searched quickly.

Examples

JAVASCRIPT
// <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 styles
JAVASCRIPT
findByStyle(main, "color", "not-a-color"); // []
findByStyle(main, "colour", "red");        // []

Environment

  • Plain JavaScript in a browser page; the tests call the global findByStyle from by-style.js.
  • root is always in the page. Values that depend on where an element sits, such as percentages and em lengths, are out of scope, and so are custom properties (--name).
  • Any DOM and CSSOM API is allowed: getComputedStyle, element.style, children, attachShadow.

Hints

  1. The browser can tell you what "red" computes to. Where could you ask it, without changing any element the caller owns?
  2. If the browser rejects a value, what does element.style.getPropertyValue return 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
Back to the question
Loading the editor…