What I am trying to achieve is a 3 layer effect. The first layer should be fully visible. The second layer should black but 50% transparent, showing through to the first layer's content. The third layer should be on top of the transparent layer, but fully visible (no transparency).

When I try this, the 3rd layer always inherits the opacity of the second layer (also 50% transparent) and for some reason is always behind the 2nd layer and can't be interacted with in the browser (even though it has a higher z-index), regardless of what I do. I have never been able to put anything fully visible or intractable over a transparent element.

Any takers?

Dani AI

Generated

Short expert note: the behavior you saw is normal — applying opacity to an element makes that element and all its descendants render with that transparency and often creates a new stacking context, which can interfere with z-ordering. As discovered, putting the dark layer as a separate sibling and ensuring the dialog is on a higher z-index solves the immediate problem. A cleaner, more future‑proof approach is to give the overlay a semi‑transparent background (so only the background is translucent) rather than using the opacity property.

A minimal pattern that avoids the inheritance problem:

/* overlay: covers viewport but only its background is translucent */
.overlay {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  background: rgba(0,0,0,0.5); /* modern browsers */
  z-index: 1000;
}

/* dialog: sibling above the overlay and fully opaque */
.dialog {
  position: fixed;
  z-index: 1001;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  background: #fff; /* fully opaque */
}

Troubleshooting tips and caveats:

  • opacity applies to children — see opacity on MDN.
  • rgba() only makes the overlay background translucent and leaves sibling/dialog colors untouched — see rgba() on MDN.
  • Be mindful of stacking contexts: transforms, opacity < 1, and some CSS properties create them and can change how z-index behaves — see Understanding z-index on MDN.
  • If the overlay must be click‑through (so background remains interactive) you can use pointer-events, but that also disables click-to-close on the overlay — see pointer-events on MDN.

This pattern keeps the dialog fully visible and interactive while the overlay darkens the page underneath. It also avoids the cross‑child transparency that caused the original issue.

Recommended Answers

All 3 Replies

Finally figured it out. This works in Firefox and Internet Explorer IE.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSS Layers with Transparency</title>
<style type="text/css">
body {
	z-index:1;
	width:100%;
	height:100%;
	margin:0;
}
#darkscreen {
	z-index:2;
	position:absolute;
	top:0;
	left:0;
	width:100%;
	height:100%;
	background-color:#000000;
	opacity:0.5;
	filter:alpha(opacity=50);
}
#widget1 {
	z-index:3;
	position:absolute;
	top:25%;
	left:25%;
	width:200px;
	height:200px;
	margin:auto;
	background-color:#FF6600;
	color:#FFFFFF;
}
#widget2 {
	z-index:1;
	position:absolute;
	top:50%;
	left:50%;
	width:200px;
	height:200px;
	margin:auto;
	background-color:#FF6600;
	color:#FFFFFF;
}
.thickborder {
	border:thick solid;
}
</style>
</head>
<body>

<div id="widget2">
	<b>I am a widget.</b>
	I should be in the background behind the dark screen and appear faded.
	<span style="color:#FFFFFF;border-color:#FFFFFF;" class="thickborder">white</span>
	<span style="color:#000000;border-color:#000000;" class="thickborder">black</span>
</div>

<div id="darkscreen"></div>

<div id="widget1">
	<b>I am a widget.</b>
	I should be clearly visible and in the foreground.
	My white should be white and my black should be black.
	<span style="color:#FFFFFF;border-color:#FFFFFF;" class="thickborder">white</span>
	<span style="color:#000000;border-color:#000000;" class="thickborder">black</span>
</div>

</body>
</html>

what's the widget for?

what's the widget for?

The widget is for popup dialogs, but could be used for other things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.