簡體   English   中英

如何將圖像添加到自定義CSS按鈕

[英]How to add an image to a custom CSS button

我有以下按鈕HTML代碼:

<button class="button" style="width: 95%;">PHYSICIANS</button>

它是由CSS3在這里定制的:

button {
    border: 1px solid rgba(0,0,0,0.3);
    background: #eee;
    color: #515151;
    font-size: 24px;
    font-weight: 700;
    padding: 21px 34px;
    text-decoration: none;
    background: -webkit-gradient(linear, left bottom, left top, color-stop(0.21, rgb(203,203,203)), color-stop(0.58, rgb(227,226,226)));
    background: -moz-linear-gradient(center bottom, rgb(203,203,203) 21%, rgb(227,226,226) 58%);
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-box-shadow: 0 0 0 5px rgba(255,255,255,0.3) /* glass edge */, inset 0 1px 0 0 rgba(255,255,255,0.5) /* top highlight */, inset 0 -3px 0 0 rgba(0,0,0,0.5) /* bottom shadow */;
    -webkit-box-shadow: 0 0 0 5px rgba(255,255,255,0.3), inset 0 1px 0 0 rgba(255,255,255,0.5), inset 0 -3px 0 0 rgba(0,0,0,0.5);
    box-shadow: 0 0 0 5px rgba(255,255,255,0.3), inset 0 1px 0 0 rgba(255,255,255,0.5), inset 0 -3px 0 0 rgba(0,0,0,0.5);
    text-shadow: 0 1px rgba(255,255,255,0.6);
}
button::-moz-focus-inner, a.button::-moz-focus-inner {
    padding:0;
    border:0;
}
button:hover, a.button:hover {
    background: #cbcbcb;
    cursor: pointer;
}
button:active, a.button:active {
    background: #ccc;
    padding: 22px 34px 20px;
    -moz-box-shadow: 0 0 0 5px rgba(255,255,255,0.3), inset 0 -1px 0 0 rgba(255,255,255,0.5), inset 0 2px 5px 0 rgba(0,0,0,0.2);
    -webkit-box-shadow: 0 0 0 5px rgba(255,255,255,0.3), inset 0 -1px 0 0 rgba(255,255,255,0.5), inset 0 2px 5px 0 rgba(0,0,0,0.2);
    box-shadow: 0 0 0 5px rgba(255,255,255,0.3), inset 0 -1px 0 0 rgba(255,255,255,0.5), inset 0 2px 5px 0 rgba(0,0,0,0.2);
    text-shadow: none;
}

顯示以下按鈕:

如何修改上面的代碼,以便向其中添加縮略圖。 像這樣:

默認圖像大小為260X190,但我想將其放大以適合按鈕內。

您可以使用偽元素 :before

請參閱我制作的這個工作演示: http : //jsfiddle.net/A7UsX/1/

button:before {
    content: " ";
    display: inline-block;
    background: url("http://www.wdc.com/Global/images/icons/icon_supporthelp.gif") no-repeat;
    height: 30px;
    width: 50px;
}

我做了一個紅色背景,向您展示我的所作所為。 background更改為您想要的圖像,並根據自己的喜好更改heightwidth

您可以使用:before偽元素。

button:before{content: ' '; display:inline-block;  position:absolute; content:url(http://lorempixel.com/25/25); }

我建議您使用SVG圖片,而不要使用jpg或png格式。 因此在這種情況下,您可以使用background-image調用background-image並指定位置。

button:before { content:''; display:inline-block; height:1em; width:1em; 
background-image:url('images/image.svg'); background-size:contain; background-repeat:no-repeat;  } 

在這里,您可以查看正在運行的演示。 http://jsbin.com/damujidi/1/edit

您可以添加引導程序[ ref1 ] [ ref2 ]中常用的<i>

<button class="button">
    <i class="btnbg"></i>PHYSICIANS
</button>

CSS:

.btnbg {
    background-image: url('http://i.stack.imgur.com/QgIOj.png');
    background-position: bottom left;
    background-repeat: no-repeat;
    background-size: contain;
    display: inline-block;
    height: 35px;
    line-height: 35px;
    vertical-align: middle;
    width: 70px;
}

.button {
    line-height: 35px;
    vertical-align: middle;
}

見jsFiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM