Home >Web Front-end >CSS Tutorial >How to Achieve Selective Opacity in a Container Without Affecting Child Elements?
Achieving Selective Opacity in HTML/CSS
When creating visual elements on a webpage, controlling opacity is vital. However, achieving opacity without affecting child elements can be challenging. This question explores a technique to apply opacity to a container without compromising the visibility of its content.
The desired effect is a popup box positioned over the webpage's content, with the background content partially visible by reducing its opacity. The questioner attempted to use opacity on the container, but the opacity also affected the child element, resulting in both having reduced opacity.
To overcome this issue, you can employ a combination of opacity and background color. By setting the container's background color to an rgba value with an opacity component (e.g., rgba(56,255,255,0.1)), you can reduce its opacity while preserving the child element's visibility. This allows you to create the intended effect of a popup box with a partially opaque background.
<code class="css">#container { border: solid gold 1px; width: 400px; height: 200px; background:rgba(56,255,255,0.1); } #box { border: solid silver 1px; margin: 10px; width: 300px; height: 100px; background:rgba(205,206,255,0.1); } <div id="container"> containter text <div id="box"> box text </div> </div> </code>
The above is the detailed content of How to Achieve Selective Opacity in a Container Without Affecting Child Elements?. For more information, please follow other related articles on the PHP Chinese website!