1. 程式人生 > >最簡單清晰的純css實現的單選框radio複選框checkbox自定義樣式

最簡單清晰的純css實現的單選框radio複選框checkbox自定義樣式

<fieldset>
    <legend style="color: red;">自定義單選框radio</legend>
    <div class="login_agree">
        <input type="radio" id="agree_btn">
        <label for="agree_btn"  class="input_agree" style="width: 36%;">同意XXX協議</label>
    </div>
</fieldset>
<fieldset
>
<legend style="color: red;">自定義複選框checkbox</legend> <div class="sex_checkbox"> <input type="checkbox" checked="" id="sex_male" value="1" name="sex"><label for="sex_male"></label> <input type="checkbox" id="sex_female" value="0" name="sex">
<label for="sex_female"></label> </div> </fieldset>

style:

/*公共部分——按鈕樣式*/
        label::before {
            content: "";
            display: inline-block;
            width: 20px;
            height: 20px;
            background: #EEE;
            vertical-align: middle;
            -webkit-border-radius
: 50%
; margin-right: 10px; -webkit-box-sizing:border-box; -webkit-transition:background ease-in .5s
} input[type="checkbox"]:checked+label::before{ background-color: #6399ae; border: 2px #b3b3b3 solid; } /*自定義單選框radio樣式*/ .login_agree { position: relative; } .login_agree input[type="radio"]{ display: none; } .login_agree input[type="radio"]:checked+label::before{ background-color: #f4d345; border: 4px #ccc solid; } /*自定義複選框checkbox*/ .sex_checkbox{ position: relative; } .sex_checkbox input[type="checkbox"]{ display: none; }

複選框中男女性別的選擇用jquery控制二者選其一

/*自定義複選框checkbox需要用js控制,二者只可選擇其一*/
$('.sex_checkbox').find('input[type=checkbox]').bind('click', function(){
    $('.sex_checkbox').find('input[type=checkbox]').not(this).attr("checked", false);
});