Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply CSS Filter to all but specific sub-elements

Tags:

html

css

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.

like image 743
SpaceMouse Avatar asked Jul 05 '15 14:07

SpaceMouse


People also ask

What is the use of filter in CSS?

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.

How to select the contents of an HTML element using CSS?

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

How to create filterable div elements?

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 ...

What is the difference between CSS opacity and filter?

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.


2 Answers

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.

like image 194
JNF Avatar answered Sep 27 '22 23:09

JNF


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%);
 }
like image 20
billy Avatar answered Sep 27 '22 21:09

billy