How to Select Elements by Inline Style When the Browser Rewrites the style Attribute
Some elements on your page are hidden in the HTML, and some are hidden later by script:
<div class="panel" style="display:none">…</div>
<div class="panel" id="settings">…</div>
document.getElementById('settings').style.display = 'none';
Both panels are hidden, but a selector for hidden panels only finds the first one:
document.querySelectorAll('[style*="display:none"]'); // NodeList [div.panel]
document.getElementById('settings').getAttribute('style');
// "display: none;"
Short answer: attribute selectors compare the attribute's text, and
when script changes an inline style, the browser writes that text itself: a space
after the colon, a semicolon at the end, lowercase keywords, colors as
rgb(). Markup that script never touched keeps the author's spelling. So
no single substring matches both. Select every element with a style
attribute and check the parsed value instead:
const hidden = [...document.querySelectorAll('[style]')]
.filter((el) => el.style.display === 'none');
Why the style attribute changes
When the HTML parser reads style="display:none", it stores that string as
the attribute's value and parses it into declarations, which you see as
element.style. As long as nothing changes, the attribute keeps your
exact text.
When script changes element.style (setting a property,
setProperty(), removeProperty() or
cssText), the CSSOM spec has the browser
update the style attribute
by serializing all of the element's declarations. Your original text is thrown away and
replaced with the browser's standard spelling of every property on the element, not
only the one you changed.
Selectors never see element.style. [style*="…"] only looks
at the attribute string, so it depends on who wrote that string last. People run into
this when they look for elements hidden by script; see this
Elixir Forum thread
and this question on Habr.
What the browser rewrites
I set each value with element.style.cssText and read
getAttribute('style') in Chromium 159 and Firefox 159. Except for the
font names, both browsers wrote the same thing:
You wrote The attribute becomes
display:none display: none;
display:none!important display: none !important;
color:RED color: red;
color:#f00 color: rgb(255, 0, 0);
color:#ff000080 color: rgba(255, 0, 0, 0.5);
color:hsl(0 100% 50%) color: rgb(255, 0, 0);
margin:0 auto margin: 0px auto;
opacity:.5 opacity: 0.5;
transform:translateX(10PX) transform: translateX(10px);
background-image:url(x.png) background-image: url("x.png");
font-family:Arial,'Helvetica Neue' Chromium: font-family: Arial, "Helvetica Neue";
Firefox: font-family: Arial, Helvetica Neue;
So the problem isn't only display. A selector like
[style*="color: #f00"] or [style*="margin: 0;"] stops
matching as soon as script touches the element's styles.
setAttribute('style', …) is the exception: it stores the string you pass,
like the parser does. el.setAttribute('style', 'display:none') leaves
display:none in the attribute.
Gotcha: changing another property rewrites display too
The rewrite covers every declaration on the element. If an element was hidden in the
HTML and some other script later sets a margin, its display text changes
as well:
<div id="b" style="color:RED;display:none">…</div>
document.querySelectorAll('[style*="display:none"]'); // NodeList [div#b]
b.style.margin = '0';
b.getAttribute('style');
// "color: red; display: none; margin: 0px;"
document.querySelectorAll('[style*="display:none"]'); // NodeList []
The element is still hidden, but it's no longer selected. This makes the bug hard to reproduce: it depends on whether an unrelated script, such as an animation or a layout helper, has touched the element.
Gotcha: untouched markup keeps its spelling
Switching to the browser's spelling doesn't fix it either.
[style*="display: none"] matches everything script has touched, but not
elements whose markup says display:none, display : none or
DISPLAY:NONE. Those keep the author's text until script changes their
styles. Meanwhile element.style.display is "none" for all of
them, because the declarations were parsed either way.
Gotcha: removing the last property leaves style=""
When script removes the last inline property, with
el.style.display = '' or el.style.removeProperty('display'),
the attribute isn't removed. It becomes an empty string:
el.setAttribute('style', 'display:none');
el.style.display = '';
el.getAttribute('style'); // ""
el.matches('[style]'); // true
So [style] doesn't mean "has inline styles", and
:not([style]) doesn't mean "has none". Check
el.style.length instead, which is 0 here.
Gotcha: Chromium and Firefox write font names differently
The rewritten text isn't always the same across browsers. In my tests Chromium put
double quotes around a font name containing a space and Firefox left the quotes out
(see the table above). A selector such as
[style*='"Helvetica Neue"'] that you checked in one browser can fail in
the other. This is one more reason to compare parsed values instead of strings.
The fix: filter on element.style
Use the selector only to narrow the search to elements that have inline styles, and let the browser's parsed declarations answer the question:
function withInlineStyle(property, value, root = document) {
return [...root.querySelectorAll('[style]')].filter((el) => {
return el.style.getPropertyValue(property) === value;
});
}
withInlineStyle('display', 'none'); // both panels from the top of this article
el.style reads the same no matter how the attribute is spelled, so this
finds display:none, display: none; and
DISPLAY:NONE alike. The value you compare against must use the browser's
spelling, though: compare colors with 'rgb(255, 0, 0)', not
'#f00', because that's what getPropertyValue() returns.
Better still, when your own code hides elements, have it mark them in a way that's
easy to select: a class, a data- attribute, or the
hidden attribute (el.hidden = true). Then
querySelectorAll('[hidden]') works no matter how the styles are written.
If you can only use a selector
Some tools only take a selector string: CSS, a test runner, a scraping config. Cover
both spellings, and add the i flag for uppercase markup:
[style*="display:none" i], [style*="display: none" i]
That matched all five hidden elements in my test page, but it's still matching text:
it misses unusual spacing like display :none, and it would also match
display: none inside a custom property value. Use it where you control
the markup, and the element.style filter everywhere else.
If you really mean "hidden"
[style*="display:none"] only finds elements hidden by their own inline
style. It misses elements hidden by a stylesheet, by the hidden
attribute, or by a hidden ancestor. If the question is "can the user see this?", ask
the browser:
const hidden = [...document.querySelectorAll('.panel')]
.filter((el) => !el.checkVisibility());
checkVisibility() returns false when the element or any
ancestor has display: none, whatever the reason. It's supported in all
current major browsers. For older ones, check
getComputedStyle(el).display on the element and its ancestors.
Try it in your browser
Box A is hidden by its markup. The button runs the selectors, hides box B with
style.display, sets a margin on box A, and runs them again. Before the
script, only [style*="display:none"] finds box A. After it, only
[style*="display: none"] does. The element.style filter
finds every hidden box both times.
Results will appear here.
Summary
-
Attribute selectors compare the
styleattribute as text. Changing any inline style from script makes the browser rewrite the whole attribute in its own spelling, such asdisplay: none;,color: red;andrgb(255, 0, 0). -
Markup that script never touched keeps the author's spelling, so neither
[style*="display:none"]nor[style*="display: none"]finds every element hidden with an inline style. -
Changing an unrelated property also rewrites
display. Removing the last property leavesstyle="", which still matches[style]. - Chromium and Firefox don't always rewrite the same way: font names with spaces are quoted in Chromium and not in Firefox.
-
Select
[style]and filter onel.style.display === 'none', mark elements yourself with a class orhidden, or usecheckVisibility()if you mean "not visible".