I'm expanding on my comments above because the answers haven't really addressed the crux of your question and your counter argument in the comments. What you want to do is impossible with a parent and a child, and would instead require at least siblings within the parent varying in opacity, with no change to the parent's opacity.
Any element with zero opacity acts like a cloak of invisibility that hides anything it contains, including text nodes and other contained elements. If you try this with a <div>
, you'll have the same result as you did with a <span>
: the child element can not be seen, even though the browser recognizes that its opacity is at 1
, because it is effectively shielded by the cloak of its parent element.
In your counter example in the comments above, you ask why it seems to work when you wrap a heading (<h1>
) element with a paragraph (<p>
) element, in a form like this:
<p style="opacity: 0;">
<h1 style="opacity: 1;">blue</h1>
</p>
This is because as the browser renders these elements, it corrects for the mistake of wrapping one flow element with another. If you check the elements as rendered in the DOM, you'll see that it looks as follows:
<p style="opacity: 0;"></p>
<h1 style="opacity: 1;">blue</h1>
<p></p>
This is because elements like p
and h1
are only permitted to contain phrasing content, like spans, text runs, etc. When you introduce flow content like a heading, the browser assumes that you forgot to close the paragraph before the heading, and then when it eventually reaches the closing paragraph tag after the heading, it hyper-corrects that to an empty paragraph.
You can also verify this relationship between elements by applying classes and trying to select the child with reference to the parent, say something like this:
<p class="parent">
<h1 class="child">blue</h1>
</p>
.parent > .child {
color: blue;
}
Your "child" element will not render as a child of the "parent" element, so the style won't be applied.
So to reiterate, a child can not override the opacity of its parent, even when it has full opacity itself because the parent shields it from view, and the counter example does not work, but only appears to work because the code as written is technically broken and corrected by the browser, rendering what you intended to be a child instead as a sibling.
something
, it gets h1 tag - the inner tag. Does that not happen to span? – difoxy2 Mar 03 '18 at 00:25