使用CSS将背景图像添加到按钮 [英] Adding background image to button using CSS

查看:112
本文介绍了使用CSS将背景图像添加到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将背景图像添加到已经具有背景颜色的按钮(或具有相同类的链接)上.

I am trying to add a background image to a button (or link with the same class) which already has a background color.

这是jsfiddle: http://jsfiddle.net/BNvke/

Here is the jsfiddle: http://jsfiddle.net/BNvke/

该按钮本身看起来很不错,但是我正在尝试使其如此操作,以便如果我添加某个类,则将调整填充并显示背景图像,但是该图像不显示.这是CSS/HTML:

The button looks great by itself, but I am trying to make it so that if I add a certain class, the padding will be adjusted and a background image will be displayed, however the image does not show. Here is the CSS/HTML:

.button {
    padding: 10px; 
    margin-right: 8px;
    margin-bottom: 10px;
    font-family: Arial, Tahoma, Verdana, FreeSans, sans-serif;
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
    display: inline-block;
    white-space: nowrap;
    line-height: 1em;
    position: relative;
    outline: none;
    overflow: visible;
    cursor: pointer;
    border-radius: 4px;
    -moz-border-radius: 4px; 
    -webkit-border-radius: 4px; 
    box-shadow: 1px 1px 2px 0 #CCCCCC;
    -moz-box-shadow: 1px 1px 2px 0 #CCCCCC;
    -webkit-box-shadow: 1px 1px 2px 0 #CCCCCC;
}

.button_blue {
    border: 1px solid #305875;
    color: #FBFBFB;
    background-color: #3D6E97;
}

.button_blue:hover {
    color: #FBFBFB;
    opacity: 0.9; 
    filter: alpha(opacity=90);        
}

.button_about { 
    background-image: url(http://i47.tinypic.com/2ni0ahd.png) 3px 5px no-repeat;
    padding-left: 35px;
    padding-right: 15px;
}​

<p><a class="button button_blue">Without Background</a></p>

<p><a class="button button_blue button_about">With Background</a></p>​

如何显示背景图片?

推荐答案

请参见 http://jsfiddle.net/BNvke/1/

只是改变

background-image url(http://i47.tinypic.com/2ni0ahd.png) 3px 5px no-repeat;

使用

background: url(http://i47.tinypic.com/2ni0ahd.png) 3px 5px no-repeat;

并向上移动最后一条规则,以便可以为级联应用为.button_blue定义的有关background-color的规则

and move up the last rule so the rule about background-color defined for .button_blue can be applied on cascade

这篇关于使用CSS将背景图像添加到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆