opacity css element showing through

Solutions on MaxInterview for opacity css element showing through by the best coders in the world

showing results for - "opacity css element showing through"
Hafsa
15 May 2019
1Opacity Affects Stacking Order
2Consider the following HTML snippet:
3
4<div class="wrapper tweak">
5    <div class="overlay"> 
6        <p>The Overlay Panel</p>
7    </div>
8    <div class="content">
9        <p>The main Content panel with a motif: 
10           <b>&hearts;</b> <b class="foggy">&spades;</b></p>
11    </div>
12</div>
13and the following CSS:
14
15.wrapper {
16    position: relative;
17    outline: 1px solid blue;
18}
19.overlay {
20    position: absolute;
21    top: 0;
22    left: 0;
23    background-color: white;
24    border: 1px dotted blue;
25    width: 400px;
26}
27
28.content p b {
29    background-color: black;
30    color: white;
31    padding: 0 10px;
32    opacity: 1.0; /* default value */
33}
34.content p b.foggy {
35    opacity: 0.4;
36}
37
38.tweak .overlay {
39    z-index: 1;
40}
41Basically, 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.
42
43The content has two bold elements with two different opacity values.
44
45See the basic line in the demo fiddle is at: http://jsfiddle.net/audetwebdesign/DxQZv/
46
47When 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.
48
49However, if you explicitly set z-index: 1 to the .overlay (add the .tweak class), then .overlay gets placed higher in the stacking order.
50
51There are other ways of adjusting the z-index value to get the same effect, but this may be the simplest.
52
53Reference:
54
55See 9.9.1 Specifying the stack level: the 'z-index' property in the CSS2 spec
56and 3.2. Transparency: the ‘opacity’ property in the CSS3 Color Module spec.
57
58Technically, 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.
59
60However, 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.
61
62There is also an earlier question that deals with the same issue:
63
64What has bigger priority: opacity or z-index in browsers?
65
66which I found after I posted my answer.
queries leading to this page
how to put a solid over opacity cssmake a non transparent divopacity dosent work with body tag thmlhow to make background non transparent in cssopacity 1 on opacity 0transparent div without text dispayingincss tricks 2c transparent div background colorshow stuff opacity cssdiv not transparentcss propoerty transparentmake all content of div transparentcss set opacityhow to remove opacity in html element inside opacitycss solid emenet in front of opacitycss non transparent backgroundopacity attribute css doesnt work on text in a divopacity 1 css inside a div with half opacitycontent over opacity divdiv transparency csshow to make not transperant uaing cssbackground non transparent cssnon transparent background cssposition making element transaprentreduce transparency of divhow to make a div non transparentsolid not transparent csscss make element not transparentcss transparent background but opaque elementdiv without opacity on div opacityhow to make an element transparent cssnot transparent csshow to make the inside of a box transparent in cssheader inherits opacity from imagecss making only div opacitygive everything opacity except text css jshow to apply opacity for selected card remaining opacity 0 5 jscss property opacityopacity css propertyavigation transparancy non in cssopaque color on over all elements cssopacity element becomestransparent section remove opacity on contenthow to separate div and text opacitychanging opacity screen blink instead desaperinner bax 100 25 opacity cssforce no see through of div cssis opacity full transparentdiv layer non transparnttransparent property in csssopacity cssremove opacity inside divwhenever we used card componets inside any div who have opacity 0 03 2c how we can change the card component opacitycss make opaiquecss see through parts of divcss make element transparent without losing border 22transparent borders 22make image inside div not opacity cssopacity 1 to 0 not workingcss solid background on transparent elementopacity css element showing throughhow to make a div which is solid on top of a div which is translucentmake parent transparent but not childcss adding opacity to divcss opacity on divmake elements of div have different opacityi can see through an element in css to the nextcss opacity solidopacity 1 0how to set non trasparent background csshow to make an element the transparent to its grandparenthow to have two different opacity in same div csselement above a transparent background is also transparentdiv set opacitytransparent tag cssmake just div transparentangular get first not transparent element on clickcss set text on top of opacityno transperent csshow to stop css opacity from going transparentwhen div gets low opacity text needs to stay high opacityhow to make the insides of a html boxes transparentopacity doesnt work on images inside divsopacity of html element without tag60 opacity htmlopacity css element showing through