opacity css element showing through

Opacity Affects Stacking Order
Consider the following HTML snippet:

<div class="wrapper tweak">
    <div class="overlay"> 
        <p>The Overlay Panel</p>
    </div>
    <div class="content">
        <p>The main Content panel with a motif: 
           <b>&hearts;</b> <b class="foggy">&spades;</b></p>
    </div>
</div>
and the following CSS:

.wrapper {
    position: relative;
    outline: 1px solid blue;
}
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    background-color: white;
    border: 1px dotted blue;
    width: 400px;
}

.content p b {
    background-color: black;
    color: white;
    padding: 0 10px;
    opacity: 1.0; /* default value */
}
.content p b.foggy {
    opacity: 0.4;
}

.tweak .overlay {
    z-index: 1;
}
Basically, I have a wrapper with two child div's, .overlay and .content. Note that the overlay is ahead of the content in the code (DOM). If it were after, you would not have seen the problem in the first place.

The content has two bold elements with two different opacity values.

See the basic line in the demo fiddle is at: http://jsfiddle.net/audetwebdesign/DxQZv/

When you enable the overlay, the .foggy element with opacity of 0.4 appears in front of the overlay. The stacking order for this arrangement is, from back to front: the in-flow content with opacity of 1.0 (default), the absolutely positioned overlay, then the element with opacity 0.4.

However, if you explicitly set z-index: 1 to the .overlay (add the .tweak class), then .overlay gets placed higher in the stacking order.

There are other ways of adjusting the z-index value to get the same effect, but this may be the simplest.

Reference:

See 9.9.1 Specifying the stack level: the 'z-index' property in the CSS2 spec
and 3.2. Transparency: the ‘opacity’ property in the CSS3 Color Module spec.

Technically, adding an opacity value (other than 1.0) to an element creates a new stacking context with z-index of 0, which is also what happens with a positioned element.

However, when two elements have the same z-index, they are painted in the order they appear in the DOM (tree order). In this case, the overlay is painted first followed by the bold element with opacity of 0.4, which is why it appears in front of the overlay.

There is also an earlier question that deals with the same issue:

What has bigger priority: opacity or z-index in browsers?

which I found after I posted my answer.

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
opacity css background non transparent css css make element not transparent opacity 1 css inside a div with half opacity header inherits opacity from image opacity element becomes make all content of div transparent how to separate div and text opacity how to stop css opacity from going transparent when div gets low opacity text needs to stay high opacity css propoerty transparent element above a transparent background is also transparent css make element transparent without losing border -&quot;transparent borders&quot; no transperent css make image inside div not opacity css css adding opacity to div how to make the inside of a box transparent in css opacity 1 to 0 not working changing opacity screen blink instead desaper opacity 1 0 how to make background non transparent in css position making element transaprent how to make a div which is solid on top of a div which is translucent not transparent css css transparent background but opaque element div layer non-transparnt i can see through an element in css to the next give everything opacity except text css js css set text on top of opacity div set opacity remove opacity inside div make parent transparent but not child avigation Transparancy non in css angular get first not transparent element on click css opacity on div 60 opacity html opaque color on over all elements css non transparent background css css see through parts of div css make opaique how to have two different opacity in same div css transparent div without text dispayingin transparent section remove opacity on content make a non transparent div how to make a div non transparent whenever we used card componets inside any div who have opacity 0.03, how we can change the card component opacity how to make an element the transparent to its grandparent transparent tag css opacity dosent work with body tag thml is opacity full transparent opacity attribute css doesnt work on text in a div css making only div opacity make just div transparent opacity doesnt work on images inside divs opacity css property css set opacity div not transparent make elements of div have different opacity reduce transparency of div content over opacity div opacity 1 on opacity 0 css tricks, transparent div background color how to apply opacity for selected card remaining opacity 0.5 js opacity of html element without tag css solid emenet in front of opacity css opacity solid css non transparent background show stuff opacity css div transparency css transparent property in csss inner bax 100% opacity css how to remove opacity in HTML element inside opacity how to make an element transparent css how to make not transperant uaing css solid not transparent css how to put a solid over opacity css css solid background on transparent element css property opacity force no see through of div css how to set. non trasparent background css how to make the insides of a html boxes transparent opacity css element showing through
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source