使用html和CSS的按钮徽章

时间:2014-11-19 10:52:59

标签: html5 css3

大家好,我正在尝试做这样的事情

enter image description here

我需要按钮

到目前为止,我尝试使用SVG和类似这样的按钮

<div style="position:relative;">
        <div style="border-radius:50px;background-color:#F3B200;border:2px solid;width:35px;height:35px;position:absolute;left:84%;margin-top:-18px;">
        <center>5</center>
        </div>
        <div>
            <button class="btn btn-success btn-lg" style="width:170px;">
            <center> MYbutton</center>
         </button>
         </div>
         </div> 

但这并不适用。我需要它才能做出回应。

1 个答案:

答案 0 :(得分:9)

这可能是我能想到的最简单的方法:

为您的按钮添加自定义data属性。在这种情况下,我使用了data-countcount部分可以是您想要的任何内容):

<button data-count="5"></button>

使用data属性的值作为content伪元素的:before

button:before {
    content: attr(data-count);
}

以下完整演示......

&#13;
&#13;
button {
    background: linear-gradient(to bottom, rgba(37,130,188,1) 0%,rgba(41,137,216,1) 32%,rgba(41,137,216,1) 42%,rgba(175,224,234,1) 100%);
    width: 60px;
    height: 60px;
    border-radius: 10px;
    border: none;
    margin-top: 40px;
    margin-left: 40px;
    position: relative;
    box-shadow: 0 2px 5px rgba(0,0,0,0.4);
}

button:before {
    content: attr(data-count);
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    display: block;
    border-radius: 50%;
    background: rgb(67, 151, 232);
    border: 1px solid #FFF;
    box-shadow: 0 1px 3px rgba(0,0,0,0.4);
    color: #FFF;
    position: absolute;
    top: -7px;
    left: -7px;
}

button.badge-top-right:before {
    left: auto;
    right: -7px;
}

button.badge-bottom-right:before {
    left: auto;
    top: auto;
    right: -7px;
    bottom: -7px;
}

button.badge-bottom-left:before {
    top: auto;
    bottom: -7px;
}
&#13;
<button data-count="5"></button>
<button data-count="5" class="badge-top-right"></button>
<button data-count="5" class="badge-bottom-right"></button>
<button data-count="5" class="badge-bottom-left"></button>
&#13;
&#13;
&#13;