Use CSS to make background-image above background-color in a list

亡梦爱人 提交于 2019-12-19 17:43:09

问题


This is the code

<ul>
    <li class="test">
         <a href="#">
            <h2>Blah</h2>
            <p>Blah Blah Blah</p>
         </a>
    </li>
</ul>

Basically, my list already has some styling, for example, I set the background-color for it.

Now in the class "test", I also set a background-image.

My question is I want to make the background-image stays above the background-color and I dont know how to achieve this. I tried with z-index but I think it is not applicable for background-image.

Please take a look at the image

Edit: This is the CSS Code

For example, this is the styling for my list

#sidebar ul li{  
    background:#ccf;
}

And this is the styling for the class test

.test{
    background-image: url('img/big_tick.png');
    background-repeat:no-repeat;
    background-position: bottom right;
    -moz-background-size: 30%;
    -webkit-background-size: 30%;
    background-size: 30%;    
    margin-right: 30px;
}

回答1:


Add the !important attribute to your background-image. Like this:

.test a{
background-image: url('img/big_tick.png') !important;
background-repeat:no-repeat;
background-position: bottom right;
-moz-background-size: 30%;
-webkit-background-size: 30%;
background-size: 30%;    
margin-right: 30px;

}

I have an example fiddle here

EDIT: I changed the class to .test a to reflect the actual styling scheme




回答2:


Try: adding specificity to the .test class: #sidebar ul li.test

Or, better yet, if it's possible, add background-color to the .test class, and get rid of the element selector altogether.

I would avoid using !important unless you absolutely need it. In the long run, using it will make editing your css a pain. Instead of using !important, I suggest learning about specificity: http://reference.sitepoint.com/css/inheritancecascade



来源:https://stackoverflow.com/questions/8491958/use-css-to-make-background-image-above-background-color-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!