css only checkbox (with content attribute)

前端 未结 4 501
再見小時候
再見小時候 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:14

    I believe this is impossible with just css. Css decorates a html element and does not change its properties. If you click the checkbox, the box will have to do a postback to show it on the page. In which case the css will be the same. You need javascript.

    What makes you not want to use javascript?

    0 讨论(0)
  • 2021-01-20 05:15

    It is possible. Check out these blog posts by Ryan Seddon. He explain how you can play with checkbox and CSS

    http://www.thecssninja.com/css/custom-inputs-using-css

    http://www.thecssninja.com/css/futurebox3

    http://www.thecssninja.com/css/css-tree-menu

    0 讨论(0)
  • 2021-01-20 05:27

    Creating an actual element with CSS isn't possible. You can however style a checkbox using css.

    An example:

    input[type=checkbox] {
        outline: 2px solid #f00;
    }
    

    Relying on pure CSS is also a give-or-take when dealing with different browsers and platforms. I hope this answers your question.

    0 讨论(0)
  • 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;
    }
    <input type="checkbox" />

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