html - 图片上方的文字 CSS Z-Index 不起作用

标签 html css

我试图在图像上方强制文本,但是,它不想工作,我已经在文本上尝试了 z-index 100,在图像上尝试了 -100,但它仍然不起作用.. .

主要 HTML:

<div class="menu-defaults menu-overlay">
        <div class="menu-container">
            <div class="menu-gallery">
                <a href="">
                    <div class="menu-gallery-options">
                        <div class="menu-gallery-options-title">
                            <span class="gallery-options-title-style" style="z-index: 1;">HOME</span>
                        </div>
                        <div class="menu-gallery-options-img">
                            <img src="data/_img/static_ex.jpg" style="z-index: -1;" class="gallery-options-img-style">
                        </div>
                    </div>
                </a>
            </div>
        </div>
    </div>

主要 CSS:

    .menu-defaults{
    width: 100%;
    height: 100%;
}
.menu-container{
    width: 90%;
    height: 100%;
    margin: 0 auto;
}
.menu-gallery{
    margin-top: 160px;
}
.menu-gallery-options{
    width: 460px;
    height: 259;
    box-shadow: 0px 0px 20px #000;
    margin: 20px 20px 20px 20px;
}
.menu-gallery-options-title{
    width: 100%;
    height: auto;
    text-align: center;
}
.gallery-options-title-style{
    font-size:32px;
    font-weight: 900;
    color: white;
    font-family: arial;
    text-decoration: none;
}
.menu-gallery-options-img{
    margin: -45px 0;
    padding: 0;
}
.gallery-options-img-style{
    width: 100%;
    height: auto;
}

感谢您的帮助,我四处寻找但找不到任何东西.. :( 谢谢

最佳答案

我假设你不能仅仅交换标题和图像元素的顺序,所以你不得不使用 css 定位。

这是一个实例,其中包含两个元素 position: relative(以便它们遵循 z-index)并在图像上设置了 z-index:

.menu-defaults{
    width: 100%;
    height: 100%;
}
.menu-container{
    width: 90%;
    height: 100%;
    margin: 0 auto;
}
.menu-gallery{
    margin-top: 160px;
}
.menu-gallery-options{
    width: 460px;
    height: 259;
    box-shadow: 0px 0px 20px #000;
    margin: 20px 20px 20px 20px;
}
.menu-gallery-options-title{
    width: 100%;
    height: auto;
    text-align: center;
    position: relative;
}
.gallery-options-title-style{
    font-size:32px;
    font-weight: 900;
    color: white;
    font-family: arial;
    text-decoration: none;
}
.menu-gallery-options-img{
    margin: -45px 0;
    padding: 0;
    position: relative;
    z-index: -1;
}
.gallery-options-img-style{
    width: 100%;
    height: auto;
}
<div class="menu-defaults menu-overlay">
        <div class="menu-container">
            <div class="menu-gallery">
                <a href="">
                    <div class="menu-gallery-options">
                        <div class="menu-gallery-options-title">
                            <span class="gallery-options-title-style" style="z-index: 1;">HOME</span>
                        </div>
                        <div class="menu-gallery-options-img">
                            <img src="https://i.imgur.com/5LGqY2p.jpg?1" style="z-index: -1;" class="gallery-options-img-style">
                        </div>
                    </div>
                </a>
            </div>
        </div>
    </div>

关于html - 图片上方的文字 CSS Z-Index 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376035/

相关文章:

jquery - 追加后无法更改 .CSS 位置

javascript - 调整内容大小时调整饼图大小

html - 如果元素包含带有类的特定 div,如何隐藏元素

Jquery如果单选按钮被选中

html - 如何移动文本列表项以与图像列表项对齐

javascript - Html 5 数据属性转换为 jquery 插件选项格式

javascript - 点击时改变图像,有mouseenter()方法和mouseleave()

html - 如何将 HTML 边框框置于中心

html - Thymeleaf - 如何根据互斥条件应用两种(或更多)样式

javascript - 如何使用angularjs动态设置css样式?