css only checkbox (with content attribute)

前端 未结 4 505
再見小時候
再見小時候 2021-01-20 04:59

how can I make a custom checkbox with css only (no JS no JQ) with content:\"on\" when checked and content:\"off\" when uncheked.

Thanks.

reedit

OK, a

4条回答
  •  终归单人心
    2021-01-20 05:33

    input[type=checkbox] {
        position: relative;
        visibility: hidden;
        cursor: pointer;
    }
    
    input[type=checkbox]:after {
        display: block;
        content: "OFF";
        position: absolute;
        top: 0;
        right: -30px;
        visibility: visible;
        height: 30px;
        line-height: 30px;
        width: 50px;
        text-align: center;
        border-radius: 4px;
        background: #d00;
        color: #fff;
        font-weight: 600;
        cursor: pointer;
    }
    
    input[type=checkbox]:checked:after {
        content: "ON";
        background: #0a0;
    }

提交回复
热议问题