Badges For Buttons using html and CSS

后端 未结 1 1122
盖世英雄少女心
盖世英雄少女心 2020-12-30 16:25

Hello everyone I am trying to do something like this

\"enter

I need it for bu

相关标签:
1条回答
  • 2020-12-30 17:11

    This is probably the simplest way I can think of:

    Give your button a custom data attribute. In this case I've used data-count (the count part could be anything you wish):

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

    Use the value of the data attribute as the content of a :before pseudo element:

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

    Full demo below....

    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;
    }
    <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>

    0 讨论(0)
提交回复
热议问题