How to style a checkbox using CSS

前端 未结 30 3524
日久生厌
日久生厌 2020-11-21 04:26

I am trying to style a checkbox using the following:

30条回答
  •  渐次进展
    2020-11-21 05:22

    Here is a CSS/HTML-only version, no jQuery or JavaScript needed at all, Simple and clean HTML and really simple and short CSS.

    Here is the JSFiddle

    http://jsfiddle.net/v71kn3pr/

    Here is the HTML:

    Here is the CSS

    #myContainer {
        outline: black dashed 1px;
        width: 200px;
    }
    #myContainer input[type="checkbox"][name="myCheckbox"] {
        display: none;
    }
    #myContainer input[type="checkbox"][name="myCheckbox"]:not(:checked) + label.box {
        display: inline-block;
        width: 25px;
        height: 25px;
        border: black solid 1px;
        background: #FFF ;
        margin: 5px 5px;
    }
    #myContainer input[type="checkbox"][name="myCheckbox"]:checked + label.box {
        display: inline-block;
        width: 25px;
        height: 25px;
        border: black solid 1px;
        background: #F00;
        margin: 5px 5px;
    }
    #myContainer input[type="checkbox"][name="myCheckbox"] + label + label.text {
        font: normal 12px arial;
        display: inline-block;
        line-height: 27px;
        vertical-align: top;
        margin: 5px 0px;
    }
    

    This can be adapted to be able to have individual radio or checkboxes, grooups of checkboxes and groups of radio buttons as well.

    This html/css, will allow you to also capture click on the label, so the checkbox will be checked and unchecked even if you click just on the label.

    This type of checkbox/radio button works perfectly with any form, no problem at all. Have been tested using PHP, ASP.NET (.aspx), JavaServer Faces, and ColdFusion too.

提交回复
热议问题