I do not want to inherit the child opacity from the parent in CSS

前端 未结 14 924
猫巷女王i
猫巷女王i 2020-11-21 06:31

I do not want to inherit the child opacity from the parent in CSS.

I have one div which is the parent, and I have another div inside the first div which is the child

相关标签:
14条回答
  • 2020-11-21 06:40

    Opacity of child element is inherited from the parent element.

    But we can use the css position property to accomplish our achievement.

    The text container div can be put outside of the parent div but with absolute positioning projecting the desired effect.

    Ideal Requirement------------------>>>>>>>>>>>>

    HTML

                <div class="container">       
                  <div class="bar">
                      <div class="text">The text opacity is inherited   from the parent div    </div>
                  </div>
                </div>
    

    CSS

                   .container{
                    position:relative;
                                       }
               .bar{
                   opacity:0.2;
                   background-color:#000;
                   z-index:3;
                   position:absolute;
                   top:0;
                   left:0;
                  }
    
                  .text{
                    color:#fff;
    
                   }
    

    Output:--

    Inherited Opacity of Text(the text color is #000; but not visisble.)

    the Text is not visible because inheriting opacity from parent div.

    Solution ------------------->>>>>>

    HTML

           <div class="container">  
             <div class="text">Opacity is not inherited from parent div "bar"</div>
             <div class="bar"></div>
           </div>
    

    CSS

                   .container{
                    position:relative;
                                       }
               .bar{
                   opacity:0.2;
                   background-color:#000;
                   z-index:3;
                   position:absolute;
                   top:0;
                   left:0;
                  }
    
                  .text{
                    color:#fff;
                    z-index:3;
                    position:absolute;
                   top:0;
                   left:0;  
                   }
    

    Output :

    Not Inherited

    the Text is visible with same color as of background because the div is not in the transparent div

    0 讨论(0)
  • 2020-11-21 06:40

    Answers above seems to complicated for me, so I wrote this:

    #kb-mask-overlay { 
        background-color: rgba(0,0,0,0.8);
        width: 100%;
        height: 100%; 
        z-index: 10000;
        top: 0; 
        left: 0; 
        position: fixed;
        content: "";
    }
    
    #kb-mask-overlay > .pop-up {
        width: 800px;
        height: 150px;
        background-color: dimgray;
        margin-top: 30px; 
        margin-left: 30px;
    }
    
    span {
      color: white;
    }
    <div id="kb-mask-overlay">
      <div class="pop-up">
        <span>Content of no opacity children</span>
      </div>
    </div>
    <div>
     <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vitae arcu nec velit pharetra consequat a quis sem. Vestibulum rutrum, ligula nec aliquam suscipit, sem justo accumsan mauris, id iaculis mauris arcu a eros. Donec sem urna, posuere id felis eget, pharetra rhoncus felis. Mauris tellus metus, rhoncus et laoreet sed, dictum nec orci. Mauris sagittis et nisl vitae aliquet. Sed vestibulum at orci ut tempor. Ut tristique vel erat sed efficitur. Vivamus vestibulum velit condimentum tristique lacinia. Sed dignissim iaculis mattis. Sed eu ligula felis. Mauris diam augue, rhoncus sed interdum nec, euismod eget urna.
    
    Morbi sem arcu, sollicitudin ut euismod ac, iaculis id dolor. Praesent ultricies eu massa eget varius. Nunc sit amet egestas arcu. Quisque at turpis lobortis nibh semper imperdiet vitae a neque. Proin maximus laoreet luctus. Nulla vel nulla ut elit blandit consequat. Nullam tempus purus vitae luctus fringilla. Nullam sodales vel justo vitae eleifend. Suspendisse et tortor nulla. Ut pharetra, sapien non porttitor pharetra, libero augue dictum purus, dignissim vehicula ligula nulla sed purus. Cras nec dapibus dolor. Donec nulla arcu, pretium ac ipsum vel, accumsan egestas urna. Vestibulum at bibendum tortor, a consequat eros. Nunc interdum at erat nec ultrices. Sed a augue sit amet lacus sodales eleifend ut id metus. Quisque vel luctus arcu. 
     </p>
    </div>

    kb-mask-overlay it's your (opacity) parent, pop-up it's your (no opacity) children. Everything below it's rest of your site.

    0 讨论(0)
  • 2020-11-21 06:44

    The question didn't defined if the background is a color or an image but since @Blowski have already answered for coloured backgrounds, there's a hack for images below:

    background: linear-gradient(rgba(0,0,0,.6), rgba(0,0,0,.6)), url('image.jpg');
    

    This way you can manipulate the color of your opacity and even add nice gradient effects.

    .wrapper {
      width: 630px;
      height: 420px;
      display: table;
      background: linear-gradient(
        rgba(0,0,0,.8), 
        rgba(0,0,0,.8)), 
        url('http://cdn.moviestillsdb.com/sm/35bc3c6a2b791425de6caf8b9391026e/rambo-iii.jpg');
     }
    
    h1 {
      display: table-cell;
      vertical-align: middle;
      text-align: center;
      color: #fff;
      }
    <div class="wrapper">
    <h1>Question 5770341</h1>
    </div>

    0 讨论(0)
  • 2020-11-21 06:50

    Opacity is not actually inherited in CSS. It's a post-rendering group transform. In other words, if a <div> has opacity set you render the div and all its kids into a temporary buffer, and then composite that whole buffer into the page with the given opacity setting.

    What exactly you want to do here depends on the exact rendering you're looking for, which is not clear from the question.

    0 讨论(0)
  • 2020-11-21 06:50

    On mac you can use Preview editor to apply opacity to a white rectangle laid over your .png image before you put it in your .css.

    1) Image
    Logo

    2) Create a rectangle around the image
    Rectanle around logo

    3) Change background color to white
    rectangle turned white

    4) Adjust rectangle opacity
    opaque image

    0 讨论(0)
  • 2020-11-21 06:51

    As others have mentioned in this and other similar threads, the best way to avoid this problem is to use RGBA/HSLA or else use a transparent PNG.

    But, if you want a ridiculous solution, similar to the one linked in another answer in this thread (which is also my website), here's a brand new script I wrote that fixes this problem automatically, called thatsNotYoChild.js:

    http://www.impressivewebs.com/fixing-parent-child-opacity/

    Basically it uses JavaScript to remove all children from the parent div, then reposition the child elements back to where they should be without actually being children of that element anymore.

    To me, this should be a last resort, but I thought it would be fun to write something that did this, if anyone wants to do this.

    0 讨论(0)
提交回复
热议问题