在这种情况下我应该如何设置 z-index 才能正常工作?

how should i set z-index in this case that work good?

我想在玻璃卡后面的绿色小矩形里设置z-index -999,但是不行。我的代码中有 position rel 和 abs,我对此感到困惑,为什么它不起作用!

.second-card::before {
  content: '';
  display: inline-block;
  width: 5rem;
  height: 2rem;
  background-color: #82c91e;
  border-radius: 2px;

  position: absolute;
  top: -5%;
  left: 50%;
  transform: translateX(-50%);
  z-index: -999;
}

我以前用过。

https://jsfiddle.net/9do7hpnx/1/

问题是您正试图从其父级隐藏绿色矩形,您不能这样做,因为当您将 z 索引应用于父级时,它也会应用于子级(即绿色矩形)以解决此问题创建另一个 div,它不是模糊 div 之类

的子项

这是一个工作演示

https://jsfiddle.net/mcshjxqt/1/

代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Payment</title>
</head><!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Payment</title>
</head>

<body>
  <div class="payment-container">
    <div class="payment-content">
      <div class="container-content">
        <h2 class="payment-title">Checkout</h2>
        <p class="payment-timer">
          <span class="timer-txt">0</span>
          <span class="timer-txt">1</span>
          <span class="timer-semicolumn">:</span>
          <span class="timer-txt">1</span>
          <span class="timer-txt">9</span>
        </p>
      </div>
    </div>

    <div class="payment">
      <div class="card">
        <div class="main-card">
            <div class="main-card--top">
            <div class='greenBg'>
            
            </div>
              <div class="second-card">
                <div class="second-card--logo">
                  <img src="img/logo.png" alt="logo" class="logo-img">
                </div>
                
                <div class="second-card--content">
                  <div class="second-card--txt">
                    <p class="card-name">Benjamin Taylor</p>
                    <div class="card-name--details">
                      <span class=""></span>
                    </div>
                  </div>
                  <div class="second-card--icon">
                    <p class="second-card--date">
                      12 / 05
                    </p>
                    <ion-icon class="sec-icon-wifi" name="wifi-outline"></ion-icon>
                  </div>
                </div>
              </div>
    
              <div class="main-card--content">
                <div class="main-card--order">
                  <p class="order-title">order</p>
                  <p class="order-number">4239421</p>
                </div>
                <div class="main-card--product">
                  <p class="product-title">product</p>
                  <p class="product-name">reels on rails</p>
                </div>
                <div class="main-card--vat">
                  <p class="vat-title">vat (<span class="vat-per">10</span>%)</p>
                  <p class="vat-price">Rp 25.000</p>
                </div>
              </div>
            </div>
          
          <div class="main-card--bottom">
            <div class="main-card--total">
              <div class="total-price--content">
                <p class="total-title">total</p>
                <div class="total-price">
                  <p class="total-price-sym">Rp</p>
                  <p class="totla-price-pay">2.256.100</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="pay">2</div>
    </div>
  </div>


  <!-- JavaScript -->
  <script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script>
  <script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script>
</body>
</html>

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@500;600;700&display=swap');

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

html {
  font-size: 62.5%;
}

body {
  min-height: 100vh;
  font-size: 1rem;
  background-color: #212529;
  color: #fff;
}

/* ================== Payment container ================== */
.payment-container {
  max-width: 100rem;
  margin-inline: auto;
}

/* ================== Payment content ================== */
.payment-content {
  display: grid;
  grid-template-columns: 1fr 2fr;

  padding: 3.2rem;
}

.payment-title {
  font-weight: 600;
  font-size: 2rem;
}

.container-content {
  grid-column: 2;

  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* ================== Payment timer ================== */
.payment-timer {
  display: flex;
  align-items: center;
  gap: .5rem;
}
.timer-txt {
  width: 3.2rem;
  height: 3.2rem;
  font-size: 2rem;
  background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
}

/* ================== Payment ================== */
.payment {
  display: grid;
  grid-template-columns: 1fr 2fr;
}

/* ================== Card ================== */
.main-card {
  background-color: #313538;
  width: 90%;
  margin-inline: auto;
}
.main-card--content {
  display: flex;
  flex-direction: column;
  gap: 1rem;

  margin-bottom: 2rem;
}

.main-card-container--content {
  width: 70%;
  margin-inline: auto;
}

.second-card {
  background: rgba(255, 255, 255, 0.25);
  border-radius: 5px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-bottom: 5px solid #94d82d;

  min-height: 20rem;
  transform: translateY(-60px);
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

.main-card--top {
  width: 75%;
  margin-inline: auto;
  position: relative;
  z-index: 999;
}

.second-card--logo {
  margin-block: 4rem;
  text-align: center;
}

.logo-img {
  width: 6.4rem;
}

.sec-icon-wifi {
  color: #fff;
  width: 2rem;
  height: 2rem;
}

.second-card--content {
  display: flex;
  flex-direction: column;
  gap: 3.2rem;
}

.card-name {
  font-size: 1.4rem;
  margin-block: 5px;
}

.card-name--details {
  font-size: 1.2rem;
}

.second-card--icon {
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-block: 1rem;
}

.second-card--txt {
  text-align: center;
  margin-top: 2rem;
}

.order-title,
.product-title,
.vat-title {
  color: #8a8a8a;
}

.main-card--bottom {
  background-color: #525353;
  padding: 2rem 3rem;
  position: relative;
}

.main-card--bottom::before {
  content: '';
  display: block;
  width: 3rem;
  height: 3rem;
  background: #212529;
  border-radius: 50%;
  position: absolute;
  top: -20%;
  left: -5%;
}

.main-card--bottom::after {
  content: '';
  display: block;
  width: 3rem;
  height: 3rem;
  background: #212529;
  border-radius: 50%;
  position: absolute;
  top: -20%;
  right: -5%;
}

.total-price--content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.total-title {
  text-align: right;
  text-transform: uppercase;
  font-weight: 600;
}

.total-price-sym {
  font-weight: 700;
}

.totla-price-pay {
  font-size: 1.2rem;
  font-weight: 700;
}

.total-price {
  display: flex;
  justify-content: space-between;
}
.greenBg::before {
  content: '';
  display: inline-block;
  width: 5rem;
  height: 2rem;
  background-color: #82c91e;
  border-radius: 2px;

  position: absolute;
  top: -20%;
  left: 50%;
  transform: translateX(-50%);
  z-index: -999;
}