If a child element shares the same background color as its parent, the opacity will cause the background

I am trying to set up a modal where the parent element has a slightly transparent and dark background to highlight the modal object. I want the modal itself to have a black background, but for some reason, it disappears when I do that. I'm curious why this is happening and what steps I need to take to ensure the modal retains its black background.

What I expect

https://i.sstatic.net/GBXPd.png

Working example with a color other than black. working fine

<div id="area" class="area">
  <div class="cover"></div>
</div>

.area {
  position: relative;
  flex: 1 0 auto;
  margin: 10px 15px;
  padding: 10px;
}

.area .cover {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: black;
  opacity:0.8;
}

.note {
  width: 50%;
  background: green;
  margin: 0px auto;
  color: white;
  display: flex;
  justify-content: center;
  margin-top:10%;
  border: 1px;
  border-radius: 8px;
  height: 200px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  padding:10px;
  box-sizing: border-box;
  font-size: 2rem;
}
<div id="area" class="area">
  <h1>lorem ipsum</h1>
  <div class="cover">
    <div class="note">xx</div>
  </div>
</div>

Not working when the background-color is black

<div id="area" class="area">
  <div class="cover"></div>
</div>

.area {
  position: relative;
  flex: 1 0 auto;
  margin: 10px 15px;
  padding: 10px;
}

.area .cover {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: black;
  opacity:0.8;
}

.note {
  width: 50%;
  background: black; /* not working with black*/
  margin: 0px auto;
  color: white;
  display: flex;
  justify-content: center;
  margin-top:10%;
  border: 1px;
  border-radius: 8px;
  height: 200px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  padding:10px;
  box-sizing: border-box;
  font-size: 2rem;
}
<div id="area" class="area">
  <h1>lorem ipsum</h1>
  <div class="cover">
    <div class="note">xx</div>
  </div>
</div>

Answer №1

The reason for this issue is that the opacity property affects the entire modal, including the .note section.

A better approach would be to apply a semi-transparent background to the modal using

background-color:rgba(0,0,0,0.8);
. This will only affect the background color and not the entire element.

Answer №2

Employing the opacity property on an element reduces the opacity of both the element itself and its child elements. To achieve a semi-transparent background, use the rgba() function with an opacity value inserted into the background-color property.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps should I follow to utilize my spotify api token effectively?

Currently, I am working on developing a random playlist generator using the Spotify API. However, when I retrieve information from their server, I receive a 401 error code. I have managed to obtain an access token by following a tutorial. My main concern ...

Setting up the html div to contain the three.js container

Within my article container, there are three aside tags. The third aside with id='menuPuzzleType' contains a Three.js script that creates and displays two cube meshes in a Canvas renderer. <article id="popup"> <aside id='close ...

Is it possible to automatically direct an iframe video to another video once it finishes

I'm having some trouble adding a video to my website. I want the video to redirect to another page on my site once it finishes playing. I've searched through various forums, but none of the solutions seem to work for my specific situation. Below ...

Two headings using the same tags appear distinctively rendered

Recently, we added an iframe to our company's website that I have been helping manage. This iframe is responsible for handling requests sent to our organization. Interestingly, the headings on our website are styled using the following CSS: color: # ...

Switch div and expand/shrink the rest

Apologies for what might seem like a trivial question, but after working for a few hours now, I'm finding it difficult to wrap my head around this. My initial instinct is to handle this by manipulating the entire div structure with JavaScript, but I c ...

The perplexing problem of scrolling in Dojox/mobile/scrollableview

I've noticed some scroll issues with the dojox/mobile/ScrollableView (version 1.10+) in my cordova application, specifically on Android phones. This problem seems to be affecting other widget-based architectures and even some store apps as well. I wou ...

ChangeToWebCode <td> Class

My PowerShell functions are designed to collect system statistics, convert them into HTML fragments, and then merge everything into a single HTML file at the end. While some functions are working perfectly fine, I'm encountering an issue with specifi ...

Creating stylish rounded corners for the table header with a specific background color using CSS

I am a beginner in HTML/CSS and I am trying to create a table with rounded top corners and a different background color for the header line. I have been able to achieve each individually with the help of online resources but I am struggling to combine the ...

"Enhance Your Website with Various Hover Effects Using CSS across Multiple Elements

Has anyone experienced trouble with making the hover effect work in my onHover class and applying a display effect to span.box? Any ideas on how to fix this? .onHover { display: block; width: 200px; height: 20px; background: green; } .onHover:ho ...

The form-control class in HTML prohibits the hiding of textboxes

The CSS class form-control is causing issues with hiding a textbox in HTML, I attempted the following: <input type="number" name="mfi_nam9" class="text1 form-control" id="mfi_name" hidden> However, it only works when I remove the form-control class ...

Is there a way to automatically close the Foundation topbar menu when a link is selected?

On my single page website, I am utilizing Zurb Foundation's fixed topbar which includes anchor links to different sections of the page. My goal is to have the mobile menu close automatically whenever a link inside it is clicked. As it stands now, whe ...

Is it possible to align a clip-path starting from the right edge?

On my webpage, I have a unique hamburger icon that is positioned 2rem away from the right and top edges. I am looking for a convenient way to create a clip path that grows from its center point. Calculating the center point is not difficult but unfortunate ...

Utilize Jquery to extract the functions from a PHP file

I'm a beginner with both jQuery and PHP. Currently, I have an HTML page where I need to utilize functions from a PHP file using jQuery, and then display the results in the HTML. My goal is to count the number of files in three different directories a ...

What causes the appearance of a slight space between a child element positioned absolutely and its parent element with borders? All the margins, padding, and positions have been assigned a value of

I have encountered a peculiar issue that I managed to recreate in a simplified version. In my scenario, the parent element has the CSS property position: relative, while the child element with the ::after pseudo-element has position: absolute and all direc ...

Having trouble getting transitionProperty to work in the style object of your ReactJS CSS?

In my current setup, I have been utilizing css transitions triggered by property updates to manage animations within my react components and everything has been functioning smoothly up until now. However, I am encountering an issue where I only want to tr ...

Issue encountered while trying to play sound on PhoneGap

Greetings! I am attempting to integrate sound into various buttons and components of a game. I found this code on the PhoneGap website which works perfectly fine during testing: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...

Inability of Internet Explorer 10 to load resource files

As I work on constructing a website using a combination of HTML5, CSS3, and jQuery, I have encountered an issue when trying to view the page in Internet Explorer X. Despite my efforts to troubleshoot by clearing the cache and adjusting security settings, t ...

Sending HTML parameters to a PHP file

I have been trying to pass the parameters from the current HTML page to a PHP page using a form. In my header in the HTML, I currently have the following function: <script> function getURLParameter(name) { return decodeURIComponent((new Re ...

The removeClass method in Jquery seems to be ineffective when attempting to create a simulated active button using a DIV element

Since CSS is not my strong suit, I'm attempting to create a pressed button effect using the :active pseudo-class by applying a "glow" class to a DIV element with jQuery's addClass method and then removing it after a brief delay. This is how I ha ...

Save JSON data in an HTML document or vice versa

Hey there, the title of this post might seem a bit unclear but I couldn't think of another way to phrase it. So here's the dilemma: I have some data that is JSON encoded and stored in a .json file: {"foo":"bar", "bar":"foo", "far":"boo"} Additi ...