十二氓拼,:checked選擇器
在表單元素中轨蛤,單選按鈕和復(fù)選按鈕都有選中和未選中狀態(tài)。如果有做過嘗試就知道终议,要設(shè)置這兩個(gè)按鈕默認(rèn)樣式是比較困難的汇竭。而在css3中,可以通過:checked選擇器配合其他標(biāo)簽實(shí)現(xiàn)自定義樣式穴张。而:checked表示的是選中狀態(tài)细燎。
代碼例子:
form {
border: 1px solid #ccc;
padding: 20px;
width: 300px;
margin: 30px auto;
}
.wrapper {
margin-bottom: 10px;
}
.box {
display: inline-block;
width: 30px;
height: 30px;
margin-right: 10px;
position: relative;
background: orange;
vertical-align: middle;
border-radius: 100%;
}
.box input {
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;/*使input按鈕在span的上一層,不加點(diǎn)擊區(qū)域會(huì)出現(xiàn)不靈敏*/
}
.box span {
display: block;
width: 10px;
height: 10px;
border-radius: 100%;
position: absolute;
background: #fff;
top: 50%;
left: 50%;
margin: -5px 0 ?0 -5px;
z-index: 1;
}
input[type="radio"] + span {
opacity: 0;
}
input[type="radio"]:checked + span {
opacity: 1;
}
男
女
運(yùn)行效果: