我有一个面板,如果这个面板被选中(点击它),我把它涂成蓝色。此外,我还向该面板添加了一个小符号(.png图像),表示所选面板以前已经被选中过。

例如,如果用户看到10个面板,其中4个有这个小标志,他就知道他之前已经点击过这些面板。到目前为止,这工作得很好。现在的问题是,我不能同时显示小标志和使面板蓝色。

我将面板设置为蓝色,css背景:#6DB3F2;背景图片加上background-image: url('images/checked.png')。但是背景颜色似乎在图片上方,所以你看不到这个标志。

因此,是否可以为背景颜色和背景图像设置z-index ?


当前回答

你也可以用一个小技巧来使用图像和颜色,就像这样

body {
     background:#000 url('images/checked.png');
 }

其他回答

很有趣的问题,还没见过。这段代码对我来说工作得很好。在chrome和IE9上进行了测试

<html>
<head>
<style>
body{
    background-image: url('img.jpg');
    background-color: #6DB3F2;
}
</style>
</head>
<body>
</body>
</html>

假设你想要一个图标在右边(或左边),那么这应该是最好的工作:

.show-hide-button::after {
    content:"";
    background-repeat: no-repeat;
    background-size: contain;
    display: inline-block;
    background-size: 1em;
    width: 1em;
    height: 1em;
    background-position: 0 2px;
    margin-left: .5em;
}
.show-hide-button.shown::after {
    background-image: url(img/eye.svg);
}

您也可以使用background-size: contains;,但这应该是基本相同的。背景位置将取决于你的图像。

然后你可以很容易地在hover上做一个替代状态:

.show-hide-button.shown:hover::after {
    background-image: url(img/eye-no.svg);
}

对我来说,这个解决方案并不奏效:

background-color: #6DB3F2;
background-image: url('images/checked.png');

但事实恰恰相反:

<div class="block">
<span>
...
</span>
</div>

css:

.block{
  background-image: url('img.jpg') no-repeat;
  position: relative;
}

.block::before{
  background-color: rgba(0, 0, 0, 0.37);
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  width: 100%;
}

也可以使用下一个语法。

background: <background-color> 
            url('../assets/icons/my-icon.svg')
            <background-position-x background-position-y>
            <background-repeat>;

它允许你组合背景-颜色,背景-图像,背景-位置和背景-重复属性。

例子

background: #696969 url('../assets/icons/my-icon.svg') center center no-repeat;

你也可以用一个小技巧来使用图像和颜色,就像这样

body {
     background:#000 url('images/checked.png');
 }