Creating a mask overlay that covers the entire screen while excluding a specific sibling div

My goal is to create a div that acts as a mask covering the entire window. It should have an opacity of 0.5 and a background color of lightgray. However, there is one particular div on the screen that I do not want to be affected by the mask. I attempted to set its z-index higher than the mask's, but that approach did not work. The code snippet in question is shown below:

.parent {
  height: 300px;
  background-color: red;
}

.sub1 {
  height: 100px;
  background-color: yellow;
  z-index: 10;
  opacity: 1;
}

.mask {
  position: fixed;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  opacity: 0.5;
  background-color: lightgray;
  z-index: 1;
}
    <div class="parent">
      
      <div class="sub1">
        hello
      </div>
      
      <div class="mask">
      </div>
    </div>

Even though I aimed for the sub1 div to appear on top of the mask, it still displayed with an opacity of 0.5.

The complete code can be accessed and run from this link: https://codepen.io/zhaoyi0113/pen/vzERYY.

Answer №1

Make sure to include a position property in the styling of .sub1 to ensure that the z-index functions properly

The z-index CSS attribute determines the stacking order of a positioned element
z-index | MDN Docs

I suggest using position: relative; to avoid affecting your document layout.

.sub1 {
  height: 100px;
  background-color: yellow;
  z-index: 10;
  opacity: 1;
  position: relative;
}

Answer №2

You have the option to utilize the calc() function and specify the height of the mask by subtracting the height of the sub, for example, 100px. Additionally, it is advisable to remove the position: fixed property from the CSS of the mask. The updated CSS code for the mask would look like this:

.mask {
  left: 0;
  top: 0;
  width: 100%;
  height: -webkit-calc(100% - 100px);
  height: -moz-calc(100% - 100px);
  height: calc(100% - 100px);
  opacity: 0.5;
  background-color: lightgray;
  z-index: 1;
}

Below you can find a functional demo:

.parent {
  height: 300px;
  background-color: red;
}

.sub1 {
  height: 100px;
  background-color: yellow;
  z-index: 10;
  opacity: 1;
}

.mask {
  left: 0;
  top: 0;
  width: 100%;
  height: -webkit-calc(100% - 100px);
  height: -moz-calc(100% - 100px);
  height: calc(100% - 100px);
  opacity: 0.5;
  background-color: lightgray;
  z-index: 1;
}
<div class="parent">
  
  <div class="sub1">
    hello
  </div>
  
  <div class="mask">
  </div>
</div>

Check out the working Codepen here.

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

Invoke a class method once the Promise has been successfully resolved

I'm facing an issue with my simple class class A{ constructor(){ this.loadComponents().then(function(values) {callbackOnLoad();}); } callbackOnLoad(){ //do some things } loadComponents(){ ... return Promise.all([p1,p2,p3,p4,p ...

Common causes leading to my header component experiencing hydration errors in Next.js

I am currently developing a job portal and encountering an issue with user authentication using JWT in combination with local storage. The problem arises in my header component when the user is authenticated, and if the web page is reloaded, I receive a hy ...

Access the child component within an @ChildComponent directive in Angular

Is it possible to retrieve child components of another component? For instance, consider the following QueryList: @ContentChildren(SysColumn) syscolumns: QueryList<SysColumn>; This QueryList will contain all instances of the SysColumns class, which ...

Align the image vertically within a column with a height of 100%

I'm currently struggling to vertically center an image within a Bootstrap 4 layout. The navbar has a fixed height of 100px, and the columns also need to have a height of 100px due to their background styling. Despite using the align-items-center clas ...

Using Javascript to Access ASMX Web Services

I am looking to make a call to a webservice using JavaScript. Here is the code I have written: var method="GetStock"; var url = "http://www.examplewebsite.com/ServiceGetStock.asmx"; $.ajax({ type: "POST", url: url + "/GetStock ...

What exactly do discrete animations entail?

In the MDN animation documentation, it mentions a type of animation known as "discrete". Can you explain what this term signifies? ...

Difficulty recognizing sessions in production for a self-hosted Next.js application using Clerk Dev Auth

Having trouble finding the next step in debugging as I couldn't resolve the issue on Clerk's Discord or GH Issues. It seems like it might be a Next.js problem rather than a Clerk one? I am currently running a self-hosted next.js app in a docker ...

Image failed to load

Issue Encountered in Browser Console: https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 404 While attempting to display the image on the browser, I am uncertain if the problem lies within my code or with food2fork. Code from index.js: // Alway ...

I am looking to host several iterations of jQuery on a content delivery network within my Nuxt application

Currently, we are loading jQuery 3.1.4 externally from a CDN on the top page. index.vue head: { bodyAttrs: { id: 'overview' }, script: [ { src: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min ...

Getting just the outer edges of intricate BufferGeometry in Three.js

Currently, I am immersed in a project that involves zone creation and collision detection using Three.js. The primary objective is for my application to effectively manage collisions and produce a BufferGeometry as the final output. My aim is to visually r ...

Using Python and Selenium, you can locate an element inside another element

I've got the xpath for an element: /html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div/div[1] Inside that div, there's another element: /html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[ ...

Encountering an Unknown Error while Parsing JSON in Google Apps Script

Having trouble parsing JSON data retrieved from an API call. The error message "TypeError: Cannot read property "id" from undefined. (line 42, file "")" keeps popping up. I'm fairly new to Apps Script. Any thoughts on what might be causing this issue? ...

Can a custom javascript object be exported in various formats similar to the Date object?

Consider the following scenario where I have a custom object: function MyObject(a, b){ this.prop = a; this.name = b; this.doSomething = function(){ return "something"; } } var a = new MyObject(4, 'name'); I am looking fo ...

Determining the Ideal Size for Expandable Text Headers

I need help adjusting the spacing between headers on my expandable sections, similar to what is shown here. I have managed to change the font size but as I increase it, the headers start to overlap. I am quite new to this so forgive me if this is a simple ...

What is causing the button within my ExtJS grid to display as "[object Object]"?

Within an ExtJS grid, I am facing a scenario where I need to display a button in a specific column when the content of a cell meets certain criteria. To achieve this, I define the column with the button using a rendering function as shown below: { he ...

When using react-router-dom, a blank page may appear for routes that contain more than one path segment followed by a single forward slash "/"

Whenever I attempt to set up a route with more than one "/" (e.g. "/testing/test"), React simply displays a blank page instead of showing the Test component along with the navigation bar and footer. Interestingly, the route "/testi ...

delayedExecution function

I have been extensively researching my issue and want to avoid posting a duplicate question. Despite trying the methods suggested in my research, I am unable to get my function to delay as intended! Could someone please review my syntax and help me identi ...

What is preventing the buttons from filling the entire space of the parent element in this case?

https://i.stack.imgur.com/kbiWi.png I'm trying to figure out how to make the Repos and Stars buttons fill the entire height of their parent container, similar to the Github icon. Unfortunately, the code below is not achieving this effect. I attempted ...

Sending Node.js array from endpoint to Javascript function

I am facing an issue with my HTML chart that is supposed to display an array from a JavaScript function called 'window.testDataArray'. Instead of using a sample array, I want to fetch the array data from a server endpoint. However, I am unsure ab ...

Having issues with using jsonplaceholder.typicode.com as a data source in AngularJS

I am facing a challenge with my AngularJS project focused on creating a Users application. Instead of storing the users' data array within the controller, as I have already accomplished successfully in this example (jsFiddle), I aim to utilize jsonpl ...