在表單元素中,單選按鈕和復(fù)選按鈕都具有選中和未選中狀態(tài)。(大家都知道,要覆寫這兩個按鈕默認樣式比較困難)。在CSS3中,我們可以通過狀態(tài)選擇器“:checked”配合其他標簽實現(xiàn)自定義樣式。而“:checked”表示的是選中狀態(tài)。
示例演示:
通過“:checked”狀態(tài)來自定義復(fù)選框效果。
HTML代碼
<form action="#"> <div class="wrapper"> <div class="box"> <input type="checkbox" checked="checked" id="usename" /><span>√</span> </div> <lable for="usename">我是選中狀態(tài)</lable> </div> <div class="wrapper"> <div class="box"> <input type="checkbox" id="usepwd" /><span>√</span> </div> <label for="usepwd">我是未選中狀態(tài)</label> </div> </form>
CSS代碼:
form {
border: 1px solid #ccc;
padding: 20px;
width: 300px;
margin: 30px auto;
}
.wrapper {
margin-bottom: 10px;
}
.box {
display: inline-block;
width: 20px;
height: 20px;
margin-right: 10px;
position: relative;
border: 2px solid orange;
vertical-align: middle;
}
.box input {
opacity: 0;
position: absolute;
top:0;
left:0;
}
.box span {
position: absolute;
top: -10px;
right: 3px;
font-size: 30px;
font-weight: bold;
font-family: Arial;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
color: orange;
}
input[type="checkbox"] + span {
opacity: 0;
}
input[type="checkbox"]:checked + span {
opacity: 1;
}
結(jié)果演示
在CSS編輯器的第47行輸入正確的代碼,實現(xiàn)自定義單選按鈕的效果。
思考一下,我們剛講過的選擇器
請驗證,完成請求
由于請求次數(shù)過多,請先驗證,完成再次請求
打開微信掃碼自動綁定
綁定后可得到
使用 Ctrl+D 可將課程添加到書簽
舉報