How would I use CSS to style all elements that are not images? I tried using
body:not(img) {
filter: grayscale(1);
}
but this does not work.
How would I use CSS to style all elements that are not images? I tried using
body:not(img) {
filter: grayscale(1);
}
but this does not work.
2
Answers
Doing
:not
on the body will target elements directly on the body.What you’re after is something like
This targets all elements ‘*’ where the element is not the img tag.
Although you may need to tweak this, as doing this might also interfere with your images depending on things overlapping (such as parent divs of the images).
A greyscale filter on any element will apply to all its contents. So your selector not only needs to exclude images, it also needs to exclude elements which have an image descendant. This is the selector you need:
Here is a small snippet to demonstrate this working. Images, whether wrapped or not, remain in colour, while the rest of the document becomes greyscale. Tested in Chrome, Safari and Firefox on Mac.