AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79563785
Accepted
Afrose ahamed
Afrose ahamed
Asked: 2025-04-09 16:01:47 +0800 CST2025-04-09 16:01:47 +0800 CST 2025-04-09 16:01:47 +0800 CST

如何修复 z-index 堆栈问题

  • 772

我的背景图像与 card::after 重叠,我试图将 card::after 分配到 card 类后面,但是当背景图像渲染时我看不到 card::after 类,我为 .background-image 添加了 z-index:0,为 .card 添加了 z-index:1,为 .card::after 伪类添加了 z-index:-1

我需要背景位于 .card::after 后面,而 .card::after 位于 card 后面。请参考下面的图片和代码

在背景图像呈现之前按预期显示 .card::after 在此处输入图片描述

背景图像渲染后不显示 .card::after 在此处输入图片描述

.image-wrapper {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;  
}

.background-img {
    width: 100vw;
    height: 100vh;
    background-size: 100%;
    z-index: 0;
}

.overlay-text {
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-size: 3rem;
    font-weight: 900;
    text-shadow: 1px 1px 2px #000;
    border: 1px solid #FF5733;
    background-color: #FF5733;
    padding: 0 6rem;
    border-radius: 10px;
}

.sticky-notes {
    position: absolute;
    top: 25%;
    left: 0;
    right: 0;
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 16px;
    padding: 20px;
    max-width: 1600px; /* Controls how wide the card row is */
    justify-content: center;
    cursor: pointer;
}
.card {
    width: 250px;
    height: 250px;
    color: white;
    border-radius: 12px;
    padding: 16px;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    font-family: 'Arial', sans-serif;
    position: relative;
    z-index: 2; /* Ensures the card is above the background image */
}

.card::after{
    content: '';
    position: absolute;
    top: -4px;
    left: -4px;
    right: -4px;
    bottom: -4px;
    border-radius: 14px; /* slightly more than the card to match the curve */
    background: red;
    z-index: -1; /* behind the card */
}
.appreciator, .text{
    font-weight: 400;
    font-size: 0.8rem;
    white-space: nowrap;      /* Prevents text from wrapping */
    overflow: hidden; 
}

.avatar img {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    margin: 0 auto;
}

.appreciate-text{
    font-size: 1.3rem;
    font-weight: 700;
    text-align: center;
    margin-top: 10px;
}


.icon-buttons {
    display: flex;
    justify-content: center;
    gap: 30px; /* space between like & comment icons */
    margin-top: 10px;
}


.navigation-buttons {
    position: absolute;
    top: 50%;
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 30px;
    transform: translateY(-50%);
    pointer-events: none; /* allows click-through by default */
}

.arrow-button {
    background-color: orangered;
    color: white;
    font-size: 2rem;
    border: none;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    pointer-events: auto; /* re-enables interaction on buttons */
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 10px rgba(0,0,0,0.3);
}

.arrow-button:hover {
    background-color: rgba(0, 0, 0, 0.8);
}
        <div class="image-wrapper">
                <img src={backgroundImageUrl} alt="background" class="background-img" />
                <div class="overlay-text">
                    <h1>WALL OF FAME</h1>
                </div>
                <div class="create-appreciation">
                        <lightning-button label="Create Appreciation" variant="brand" onclick={handleCreateAppreciation} class="create-button"></lightning-button>
                </div>    
        </div>

        <div class="navigation-buttons">
                <lightning-button-icon icon-name="utility:arrow_left" alternative-text="Delete" variant="bare-inverse" size="large"   class="arrow-button left-arrow"></lightning-button-icon>
                <lightning-button-icon icon-name="utility:arrow_right" alternative-text="Delete" variant="bare-inverse" size="large"   class="arrow-button left-arrow"></lightning-button-icon>
            </div>
        

        <div class="sticky-notes">
                <template for:each={appreciations} for:item="note">
                        <div key={note.Id} class="card" style={note.style}>
                                <div class="note-content">
                                        <h2 class="appreciator">{note.name}</h2>
                                        <div class="avatar">
                                                <img src="https://www.lightningdesignsystem.com/assets/images/avatar2.jpg" alt="avatar" />
                                            </div>
                                            <div class="text">is appreciated</div>
                                            <div class="appreciate-text">{note.appreciated}</div>        
                                </div>
                           <div>    <div class="icon-buttons">    
                                    <lightning-button-icon icon-name="utility:like" alternative-text="Delete" title="Delete" onclick={handleDelete} data-id={note.Id} class="delete-button"></lightning-button-icon>
                                    <lightning-button-icon icon-name="utility:comments" alternative-text="Delete" title="Delete" onclick={handleDelete} data-id={note.Id} class="delete-button"></lightning-button-icon>
                                        </div>  
                           </div>   
                        </div>
                </template>  
        </div>              

html
  • 1 1 个回答
  • 54 Views

1 个回答

  • Voted
  1. Best Answer
    Calcimicium
    2025-04-09T17:37:43+08:002025-04-09T17:37:43+08:00

    我不确定我是否理解了你的要求。

    从你的截图来看,你只需要在.card元素周围添加一个红色轮廓。要实现这一点,你只需将outline: 4px solid red;属性添加到你的.card类中,并移除.card::after伪元素即可。你的类中不再需要position: relative;和。z-index: 2;.card

    为了提高可读性,我删除了一些元素:

    .image-wrapper {
        position: relative;
        width: 100vw;
        height: 100vh;
        overflow: hidden;  
    }
    
    .background-img {
        width: 100vw;
        height: 100vh;
        background-size: 100%;
        /* z-index: 0; */ /* not necessary anymore */
    }
    
    .sticky-notes {
        position: absolute;
        top: 25%;
        left: 0;
        right: 0;
        margin: 0 auto;
        display: grid;
        grid-template-columns: repeat(6, 1fr);
        gap: 16px;
        padding: 20px;
        max-width: 1600px; /* Controls how wide the card row is */
        justify-content: center;
        cursor: pointer;
    }
    
    .card {
        width: 250px;
        height: 250px;
        color: white;
        border-radius: 12px;
        padding: 16px;
        text-align: center;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        font-family: 'Arial', sans-serif;
        outline: 4px solid red;
        /* position: relative; */ /* not necessary anymore */
        /* z-index: 2;*/ /* not necessary anymore */
    }
    
    .appreciator, .text{
        font-weight: 400;
        font-size: 0.8rem;
        white-space: nowrap;      /* Prevents text from wrapping */
        overflow: hidden; 
    }
    
    .avatar img {
        width: 64px;
        height: 64px;
        border-radius: 50%;
        margin: 0 auto;
    }
    
    .appreciate-text {
        font-size: 1.3rem;
        font-weight: 700;
        text-align: center;
        margin-top: 10px;
    }
    <div class="image-wrapper">
      <img src="https://preview.redd.it/26ig1evbnuh61.png?width=1080&crop=smart&auto=webp&s=2f075f160a65ae1b521e771354120eba83cd5215" alt="background" class="background-img" />   
    </div>
    
    <div class="sticky-notes">
      <div class="card" style="background-color: #8E44AD">
        <div class="note-content">
          <h2 class="appreciator">Lorem Ipusm</h2>
          <div class="avatar">
            <img src="https://www.lightningdesignsystem.com/assets/images/avatar2.jpg" alt="avatar" />
          </div>
          <div class="text">is appreciated</div>
          <div class="appreciate-text">Lorem Ipusm</div>
        </div>  
      </div>
    </div>

    但你的一些选择我不太理解。

    • 为什么在类中使用<img/>元素而不是属性?background-image.image-wrapper
    • 为什么您的内容(<div class="navigation-buttons">和<div class="sticky-notes">)不在您的 .image-wrapper 元素内?
    • 1

相关问题

  • 使用代码点渲染轮廓图标,而不是从 Material Design Google Fonts 中填充

  • 如何使用CSS将选项卡制作为箭头形状

  • 在 html 和 css 中绘制表格内的倾斜线

  • HTML 表格,一行中有 2 行单元格

  • 如何创建 mat-grid-list 角度材质

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve