The z-index property does not seem to apply to pseudo elements

I am facing an issue while trying to add a pseudo element after a div in my react project. Surprisingly, the code works perfectly fine in CodePen but fails to work on my local machine. I have applied z-index only in CodePen, which seems to resolve the issue there. However, when I apply z-index for both the parent div and the pseudo-element in my project, it still doesn't work. Can someone please help me identify the mistake I made? Your guidance is much appreciated. Thank you in advance. My code snippet is as follows: HTML:

        <div className='cards'>
          <div className='card active'>Card</div>
          <div className='card'>Card</div>
          <div className='card'>Card</div>
        </div>

CSS:

.card {
  width: 300px;
  height: 200px;
  background-color: #fff;
  border-radius: 5px;
  &.active {
    position: relative;
    box-shadow: 10px 10px 60px rgba(0, 0, 0, 0.1);
    z-index: 10;
    &::after {
      position: absolute;
      content: "";
      top: 0;
      left: 0;
      width: calc(100% + 5px);
      height: calc(100% + 15px);
      border-radius: 5px;
      background-color: #923929;
      z-index: -1000;
      transform: rotateZ(-2deg);
      transform-origin: top left;
    }
  }
}
.cards {
  padding: 5rem;
  display: flex;
  gap: 20rem;
  background-color: #92392920;
}

The output can be seen here: https://i.sstatic.net/I39CF.png

If the z-index for the parent div is removed, the output looks like this: https://i.sstatic.net/fxY3O.png

Answer №1

By applying a z-index to the parent element, you are creating a new stacking context

To resolve this, simply remove the z-index from the parent element

UPDATE:

Yes, I also need to include a background for the section element.

.what_we_do{
  background-color: #fff6f6;
  padding:5rem;
}
.card {
  width: 300px;
  height: 200px;
  background-color: #fff;
  border-radius: 5px
}

.card.active {
  position: relative;
  box-shadow: 10px 10px 60px rgba(0, 0, 0, 0.1);
}

.card.active::after {
  position: absolute;
  content: "";
  top: 0;
  left: 0;
  width: calc(100% + 5px);
  height: calc(100% + 15px);
  border-radius: 5px;
  background-color: #923929;
  z-index: -1;
  transform: rotateZ(-2deg);
  transform-origin: top left;
}

.cards {
  padding: 5rem;
  display: flex;
  gap: 20rem;
  background-color: #92392920;;
  position: relative;
  z-index: 1;
}
<section class="what_we_do">
<div class="cards">

  <div class="card active">Card</div>
  <div class="card">Card</div>
  <div class="card">Card</div>
</div>
</section>

UPDATE: From Comment

I would like to add effects for the active card, such as a brown background color.

.card.active{
  background-color:#923929;
}

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 is the best way to ensure the box div is centered responsively?

Here is the code for my HTML file: body { background-color: rgb(231, 59, 59); font-family: "Alata"; font-size: 20px; margin: 30px; } h1 { margin-top: 100px; color: #202124; text-align: center; } .box { width: 1000px; } input { positi ...

How can I dynamically adjust the stroke length of an SVG circle using code?

In my design project, I utilized Inkscape to create a circle. With the fill turned off, only the stroke was visible. The starting point was set at 45 degrees and the ending point at 315 degrees. After rotating it 90 degrees, here is the final outcome. < ...

Steps for aligning a grid column to the same height as the element above it and moving it to the left side

I have a setup using material UI where I have a grid positioned on top of a Paper element. Within the grid, there is a text input field in the first column that I want to match the height of the paper element and be aligned with its left border. I have tri ...

Troubleshooting issues with Bootstrap's responsiveness configuration

I've been working on creating a responsive user login page using Angular 5. It looks great on full-screen display. https://i.sstatic.net/YQrL5.png However, when I resize the browser window, the responsiveness seems to break. https://i.sstatic.net/4 ...

Submitting an HTML form results in no data being sent

I am currently facing an issue with a form on my website. The form code is as follows: <form action="" method="post" id="login_form"> <input type="text" name="log_login_form"/> <br/> <input type="password" name="log_password_for ...

Generate and delete dynamic iFrames through variable manipulation

I'm currently developing a landing page specifically for our pilots to conveniently access weather information before taking off. However, due to the limitations posed by our computer security measures, I can only utilize iframes to obtain the necessa ...

What is the best way to apply a png overlay to each img tag when hovered over?

Here is the code snippet I am working with: <div class="latest-post"> <a href="http://localhost/blaze/2011/documentaries/test-big-thumb" rel="bookmark"> <span> <img width="140" height="100" src="http:// ...

Is there a WebKit equivalent to the MDC (Mozilla Documentation Center)?

Experimenting with the newest HTML5 features is rewarding, yet challenging as standards and browser-specific implementations constantly evolve. While Mozilla provides a valuable resource with their MDN Doc Center documenting Gecko changes, I'm curious ...

Tips on automating the process of moving overflowing elements into a dropdown menu

Challenge Description In need of a dynamic navigation bar, I faced the problem of displaying only the first X items on one line and have the remaining items hidden in a "Show more" dropdown. The challenge was to calculate the width of each item accurately ...

Altering the inner HTML content of a div using the ID within an Angular component function

Attempting to change the inner HTML content of a div using its id with another div in an Angular component method. Successfully calling the populateEndpointHomeContent() method and retrieving the content, but encountering issues with subsequent content. Th ...

Expanding div that adjusts to content size and expands to fit entire page

Appreciate your help. I'm currently facing an issue with the "content" and "footer" sections within the "wrapper" element. Below are the styles for these elements: #content { width: 100%; min-height: 100%; position: relative; background- ...

Generating an Xpath for the selected element with the help of Javascript or Jquery - here's how!

On my website, I have implemented a click event on an element. When a user clicks on this element, I want to generate the XPath of that particular element starting from either the html or body tag, also known as the Absolute Xpath. For example, if I click ...

Include a new class in the classList of an element at a specific index

Is it possible to add a class to a specific position in order to maintain a certain order, especially when it goes against the logic that targets the class based on its position? link.closest('item').classList.add('c-class') // Contrad ...

Vue.js V-for not rendering any content

I am currently working on populating an array with objects retrieved from an API. The objects are being fetched successfully, but I am facing issues when trying to display them using v-for in the array. Below are my data variables: data() { return { ...

Generating values in a loop - mastering the art of creating data points

My goal is to create a variable within a loop: box-shadow: 835px 1456px #FFF, 1272px 796px #FFF, 574px 1357px #FFFand so on... The concept is simple: box-shadow: x1 y1 #fff, x2 y2 #fff, x3 y3 #fff, x4 y4 #fff ... xn yn #fff. I attempted to achieve this ...

Developing a trivia game using HTML and JavaScript

I'm in need of some serious assistance with creating a quiz using HTML. My goal is to have a web page where users can visit, take a quiz, and have their responses stored. Unfortunately, I don't have the knowledge or skills required to do this on ...

Enabling a checkbox grid for exclusive rectangular selections

Apologies for the confusing title, I struggled to come up with something more fitting. Let's say my client needs a square area on their homepage where they can upload images that will be placed within various boxes in a grid. The available box optio ...

Modifying the content within a DIV element

I want to make changes to my DIV. <div id="main"> <div id="one"> <div class="red"> ... </div> <img class="avatar" src="img/avatar1.jpg"/> <span class="name"> John < ...

The implementation of jQuery within an included HTML file may not function properly

I'm looking to implement the use of includes for generic HTML elements in my project. However, I've run into an issue where including an HTML file causes jQuery to stop working. Here is a snippet of my code that illustrates the problem. Specifica ...

Using jQuery UI Dialog to delay the appearance of content after :after in IE8

My current approach involves using the :after selector to include asterisks next to required labels: .field-name { color: #444; font-family: verdana; font-size: 0.85em; line-height: 2em; } .field-name.required:after { content: '*&a ...