I need a CSS filter to apply to all elements in a container, except for specific ones. Quick example to explain the situation:
<div class="container">
<img class="one" src="blah" />
<img class="two" src="blah" />
<img class="three" src="blah" />
</div>
Then I am applying filters as so:
.container {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
}
So the container has the greyscale filter applied to it, and all img in it are turned to grey. However, I want one of the img to not turn to grey:
.two {
-webkit-filter: grayscale(0);
filter: grayscale(0);
}
However, this is not working. The filter of the container seems to be overriding the filter of the contained element. Any ideas how I can get around this? Is there an easy way, or do I have to write up some jQuery to look at all the elements that aren't ".two" and apply the filter to them, rather than the container?
Update: I neglected to mention an important caveat: The container has to be greyscale, due to it having a background-image property that is to also be turned grey. This little snippet is part of more containers that are all going greyscale as well, I'm really just trying to figure out if there's a way to have an overriding exemption to the rule on the parent, since the parent has to have the rule as well.
F » filter. The CSS filter property provides access to effects like blur or color shifting on an element’s rendering before the element is displayed. Filters are commonly used to adjust the rendering of an image, a background, or a border.
You can use this id as a CSS Selector as well: #contents{ color: white; background-color: black; } The above code snippet will select the HTML element which has an idvalue of contents. As a reminder, every idin a document mustbe unique.1
Create Filterable DIV Elements 1 Step 1) Add HTML:#N#Example#N#<!-- Control buttons -->#N#<div id="myBtnContainer">#N#<button class="btn active"... 2 Step 2) Add CSS:#N#Example#N#.container {#N#overflow: hidden;#N#}#N#.filterDiv {#N#float: left;#N#background-color: 3 2196F3;#N#color:... 4 Step 3) Add JavaScript: More ...
A computed value of other than "none" results in the creation of a stacking context the same way that CSS opacity does. The filter property has no effect on the geometry of the target element’s box model, even though filters can cause painting outside of an element’s border box.
According to CSS specifity rules -
Either put the .two
after the .container
in the css,
Or make the .two
more specific, i.e. img.two
UPDATE
The .container
rule is on the div
itself - not on the images. So the container goes grayscale regardless of what you tell the images to do. Try changing that into .container img
, and then try incorporating the answers you received.
use > to specify an image that is a child of .container the use not: to specify that you don't want the second image grey
.container > img:not(.two) {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
}
<div class="container">
<img class="one" src="http://lorempixel.com/400/200" />
<img class="two" src="http://lorempixel.com/400/200" />
<img class="three" src="http://lorempixel.com/400/200" />
</div>
jsfiddle
.container > img:not(.two) {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With