使用html和CSS的按钮徽章

Sur*_*mar 2 html5 css3

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

在此输入图像描述

我需要按钮

到目前为止,我尝试使用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> 
Run Code Online (Sandbox Code Playgroud)

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

Tur*_*nip 12

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

为您的按钮提供自定义data属性.在这种情况下我使用过data-count(count部件可以是你想要的任何东西):

<button data-count="5"></button>
Run Code Online (Sandbox Code Playgroud)

使用作为数据属性的值content一个的:before伪元件:

button:before {
    content: attr(data-count);
}
Run Code Online (Sandbox Code Playgroud)

下面的完整演示......

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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)