Make a Parent Div Webkit-Filter Not Affect Children

Make a parent div webkit-filter not affect children

This is not a problem of properties inheritance, as you can think.

The way filters work makes that imposible to fix changing attributes in the CSS: The element affected by the filter is rendered, all the children are rendered, and then the result (as an image) has the filter applied.

So the only alternatives left are:

1) Change the HTML, as Lowkase suggested

2) In your case, seems that all you want to make gray is the background image. In this case, you can leave the HTML as is, display the image in a pseudo element, and apply the filter to this pseudo element.

CSS

.cell{
opacity:0.7;
width:420px;
height:420px;
transition: opacity .3s ease-in-out;
-moz-transition: opacity .3s ease-in-out;
-webkit-transition: opacity .3s ease-in-out;
}

.A1 {
position: relative;
}
.A1:before {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
background-image:url('http://i.imgur.com/NNKxZ5R.jpg');
filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
filter: gray; /* IE6-9 */
-webkit-filter: blur(15px); /* Google Chrome, Safari 6+ & Opera 15+ */
z-index: -1;
}

#text {
color:#ffffff;
text-align:center;
font:18px sans serif;
text-decoration:none;
}
.cell:hover {
opacity:1.0;
}

.A1:hover:before {
filter: none;
-webkit-filter: grayscale(0);
transition: opacity .3s ease-in-out;
-moz-transition: opacity .3s ease-in-out;
-webkit-transition: opacity .3s ease-in-out;
}

fiddle

I have also changed your filter to blur to make it more clear the the text is not affected by the filter. Since you had also some opacity set, the text still looked grayish just because you were seeing the gray under it.

Added example using brightness filter (for webkit)

demo 2

Setting filter brightness of parent without affecting child element

Changing the opacity of a parent element will always change the opacity of the child element. Sadly there is no way around that. However you can change the HTML structure and absolute position the elements on top of one another, and then only target the elements you want to fade.

$(document).ready(function(){  function setBrightness (bright) {  $('.icon').css('filter', `brightness(${bright})`);}
window.setInterval(function () { let ms = new Date().getTime(); let bright = Math.cos(2.0 * 3.14 * ms / 3000.0) + 1.0; setBrightness(bright); }, 50);});
#bar {  background: #222;  font-size: 0; }
.icon { background-image: url(https://cdn.sstatic.net/img/share-sprite-new.svg?v=78be252218f3); width: 36px; height: 34px; display: inline-block; position: absolute; top:0; left: 0; }
.icon-wrap { width: 36px; height: 34px; text-align: center; position: relative; display: table-cell; /* normally middle but set to top to not obscure background for this example: */ }
.icon1 .icon { background-position: -141px -54px; }
.icon2 .icon { background-position: -220px -54px; }
.indicator { border-radius: 2px; color: white; font-family: sans-serif; font-size: 8pt; width: 18px; height: 14px; position: absolute; top:0; left:50%; margin-left: -9px; }
.icon1 .indicator { background: #a00; }
.icon2 .indicator { background: #0f0; }
<div id="bar">  <div class="icon1 icon-wrap">    <span class="icon"></span>    <span class="indicator">3</span>  </div>  <div class="icon2 icon-wrap">    <span class="icon"></span>    <span class="indicator">10</span>  </div></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

How to blur(css) div without blur child element

How to disable blur on child element?

.enableBlur>* {  filter: blur(1.2px);}
.disableBlur { filter: blur(0);}
<div class="enableBlur">  <hr>  qqqqq<br>  <span>qqqqq</span><br>  <hr  class="disableBlur">  <div>aaaaa</div>  <div>bbbbb</div>  <div class="disableBlur">DDDDD</div>  <hr>  <img src="https://lh5.googleusercontent.com/-n8FG4f09-ug/AAAAAAAAAAI/AAAAAAAACmA/ldtxmWX1SyY/photo.jpg?sz=48">  <img class="disableBlur" src="https://lh5.googleusercontent.com/-n8FG4f09-ug/AAAAAAAAAAI/AAAAAAAACmA/ldtxmWX1SyY/photo.jpg?sz=48"></div>

Parent element backdrop-filter does not apply for its child

You should avoid applying backdrop-filter to a parent element of the sub menu. And idea is to consider a pseudo element where you can have the filter:

nav {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 50px;
background: hsla(0, 0%, 100%, 80%);
border-bottom: 1px solid hsla(0, 0%, 0%, 10%);
z-index: 1;
}
nav:before {
content:"";
position:absolute;
z-index:-1;
-webkit-backdrop-filter: blur(2px);
backdrop-filter: blur(2px);
top:0;
left:0;
right:0;
bottom:0;
}
nav ul {
display: flex;
align-items: center;
margin: 0;
padding: 0;
list-style: none;
}

nav li {
position: relative;
margin: 0 20px;
height: 50px;
line-height: 50px;
}

nav li ul {
position: absolute;
top: 50px;
left: 0;
flex-direction: column;
align-items: stretch;
width: 200px;
background: hsla(0, 0%, 100%, 80%);
opacity: 0;
-webkit-backdrop-filter: blur(2px);
backdrop-filter: blur(2px);
pointer-events: none;
}

nav li:hover ul {
opacity: 1;
pointer-events: all;
}

nav li li {
margin: 0;
height: 30px;
line-height: 30px;
}
<nav>
<ul>
<li>Nav 1</li>
<li>Nav 2</li>
<li>Nav 3
<ul>
<li>Nav 3-1</li>
<li>Nav 3-2</li>
<li>Nav 3-3</li>
</ul>
</li>
</ul>
</nav>

<main>

<h2>What is Lorem Ipsum?</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<h2>Why do we use it?</h2>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>

<h2>Where does it come from?</h2>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
1.10.32.</p>
<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions
from the 1914 translation by H. Rackham.</p>

<h2>Where can I get some?</h2>
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum,
you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary
of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>

</main>

Remove CSS filter on child elements

You can't do it that way. Childs are affected by their parent style.

That's how Cascading Style Sheets works.

I suggest you to use a pseudo-element to make it work like you want, so that only the pseudo-element would be affected.

See comments in the snippet:

.main {  position: relative;   /* Added */  width: 300px;  height: 300px;  list-style: none;}
.main::before { /* Added */ content: ''; position: absolute; top: 0; bottom: 0; left: 0; right: 0; background-color: blue; transition: 0.5s;}
.button { position: absolute; top: 35%; height: 30px; width: 120px; display: none; z-index: 99; background-color: black; color: white;}
.icon { width: 30px; position: absolute; z-index: 99; display: none; width: 100px;}
.main:hover>.button { display: block; /* filter: none; Commented */}
.main:hover>.icon { display: block; /* filter: none; Commented */}
.main:hover::before { /* Modified */ filter: brightness(50%);}
<li class="main">  <img src="https://help.seesaw.me/hc/en-us/article_attachments/204081043/bear.png" class="icon" />  <a class="button" href="#">Button</a></li>

Filter property on div affecting children

This not possible.

In the Filter Effects Module Level 1 you can read:

A computed value of other than none results in the creation of a
stacking context [CSS21] the same way that CSS opacity does. All the
elements descendants are rendered together as a group with the filter
effect applied to the group as a whole. [source: w3.org]

This means that all children are affected by by the filter property the same way opacity works.

Workaround :

If you can't change your makup, you can apply the filter only to the elements you need it on. In your example, you can replace the black background with a pseudo element and apply the drop shadow to that pseudo element. This will prevents applying the filter to the parent and affecting all the children

Example:

.greenBorder {  position:relative;  width: 50px;  height: 50px;  border-radius: 10px;}.greenBorder:before{  content:'';  position:absolute;  top:0; left:0;  width:100%; height:100%;  background: black;  border-radius:inherit;  -webkit-filter: drop-shadow(5px 5px 5px green);  -moz-filter: drop-shadow(5px 5px 5px green);  -ms-filter: drop-shadow(5px 5px 5px green);  -o-filter: drop-shadow(5px 5px 5px green);  filter: drop-shadow(5px 5px 5px green);}
.withShadow { position: absolute; width: 50px; height: 10px; left: 30px; top: 25px; background: red; border-radius: 5px; -webkit-filter: drop-shadow(5px 5px 5px green); -moz-filter: drop-shadow(5px 5px 5px green); -ms-filter: drop-shadow(5px 5px 5px green); -o-filter: drop-shadow(5px 5px 5px green); filter: drop-shadow(5px 5px 5px green);}
.withoutShadow { position: absolute; width: 10px; height: 50px; left: 30px; top: 25px; background: blue; border-radius: 5px;}
<div class="greenBorder">  <div class="withoutShadow"></div>  <div class="withShadow"></div></div>

Apply CSS effect to parent but not children

Insert an additional div and just position it absolute:

<div id="wrapper">
<div id="test" style="
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-image: url(banner.jpg);
background-attachment: fixed;
background-position: top;
background-repeat: no-repeat;
background-size: cover;
filter: grayscale(100%);
z-index: -2;
"></div>
<header id="top-wrapper" class="inline-content">
<div id="logo" class="inline-content">
<img src="logo.svg" alt="Logo">
<h1>Tuffi portfolio</h1>
</div>
<nav>
<ul id="menu" class="inline-content">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Project</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>

<section id="banner" class="inline-content">
<h2>Hello World!</h2>
<h1>I am Tuffi</h1>
<h2>and I am a Web Developer</h2>
<h3>based in London</h3>
</section>
</div>

Obviously, remove everything from #wrapper except the height of 100%.

Apply grayscale on div except one of it's child element

When a filter is applied it is not possible to ignore to the child elements.
The alternate solution is.

.gray{  position:absolute;  top:0;  left:0;  right:0;  bottom:0;  background-color:purple;  filter: grayscale(100%);  z-index:-1;}.exclude{  background-color:yellow;  width:inherit;  height:30px;  top:10px;  position:absolute;  }
<div id="parent_div" style="position:relative;height:100px;width:100px;">  <div class="gray" ></div>  <div class="exclude"></div></div>


Related Topics



Leave a reply



Submit