Reputation: 1713
Good day
I am trying to do something that should be simple, but not having much luck. I want to select all elements with the following CSS property and value:
overflow-y: scroll
Here is what I have tried based on two separate and conflicting W3Schools articles:
*[overflow-y=scroll] {
background-color: red;
}
*[overflow-y="scroll"] {
background-color: red;
}
Articles in question being these:
https://www.w3schools.com/css/css_attribute_selectors.asp
https://www.w3schools.com/cssref/sel_attribute_value.asp
Does anyone have any idea what I am doing wrong here, please? I'd greatly appreciate any advice
Upvotes: 0
Views: 431
Reputation: 499
The problem is that in the articles you linked, CSS searches through HTML attributes, not CSS properties. So you can use square brackets to search in HTML attributes like this:
#input[type="number"] {
background-color: red;
}
<input type="number" placeholder="Enter your age">
But unfortunately you can't access CSS properties in the same way.
Assuming that you set overflow-y
with CSS, you can use same selectors to set the background-color
. For example:
.scrollCont {
overflow-y: scroll;
background-color: red;
}
You can loop through elements and check if the overflow-y
has value of scroll
.
[...document.querySelectorAll("*")].forEach(elem => {
if (getComputedStyle(elem).getPropertyValue("overflow-y") == "scroll")
elem.style.backgroundColor = "red";
});
Upvotes: 1