html - 将 z-index 应用于 :pseudo element

标签 html css

我将以下 CSS 规则应用于 DIV:

.icon {
    position:relative;
    display: inline-block;
    width: 90px;
    height: 94px;
    background: url("test-icon-sprite.png") no-repeat scroll transparent;
    background-position: 0 0;
    overflow: hidden;
    z-index: 1000;
}
.icon.plaque:after {
    content: url("test-icon-plaque.png");
    position:absolute;
    width: 90px;
    height: 94px;
    z-index: 1
}

...

<div class="icon plaque"></div>

发生的情况是伪元素中的图像位于另一个元素中的图像之上。那不是我想要的!有什么办法可以解决这个问题吗?

我的目标是仅使用一个 html 元素创建一个带有可选背板(斑 block )的图标,是否可行?

最佳答案

使用多个背景。

.icon {
    background-image: url(test-icon-sprite.png);
}
.icon.plaque {
    background-image: url(test-icon-sprite.png), url(test-icon-plaque.png);
}

关于html - 将 z-index 应用于 :pseudo element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9907745/

相关文章:

html - 绝对位置不起作用

javascript - ChartJS——当我必须缩放时如何更改刻度颜色?

html - HTML 中的自定义下拉列表

html - 没有值的不确定 <progress> 标签的自定义颜色

html - 使用 CSS 添加图像以提交按钮

javascript - Angular Js : Scope Arrays

javascript - jQuery循环遍历每个li并检查一个类

html - <h1> 中的行间距太小 : the texts overlap

html 页面无法显示 css 背景图像

html - 如何选择文本框的行数? (*不是*高度)