想實現一個這種效果的radio樣式怎麼弄?css
只用html和csshtml
<label class="radio-v2"> <input type="radio" name="c"> <span></span>自動彈出 </label> <label class="radio-v2"> <input type="radio" name="c"> <span></span>不自動彈出 </label> <label class="radio-v2"> <input type="radio" name="c" disabled> <span></span>禁用 </label>
.radio-v2 { margin-right: 30px; font-weight: normal; } .radio-v2 input { opacity: 0; z-index: 2; } .radio-v2 span { position: relative; display: inline-block; vertical-align: top; margin-left: -20px; width: 20px; height: 20px; border-radius: 500px; border: 1px solid #AAB8D4; margin-right: 5px; text-align: center; } .radio-v2:hover span { border-color: #4A5FE2; } .radio-v2 input[type=radio]:checked + span:before { position: absolute; content: ""; width: 12px; height: 12px; top: 50%; left: 50%; margin-top: -6px; margin-left: -6px; border-radius: 500px; opacity: 1; transition: color 0.3s ease-out; background-color: #4A5FE2; } .radio-v2 input[type=radio]:checked + span { border-color: #4A5FE2; } .radio-v2 input[type=radio]:disabled + span { border-color: rgba(162, 162, 162, 0.12) !important; cursor: not-allowed; } .radio-v2 input[type=radio]:disabled + span:before { background-color: rgba(162, 162, 162, 0.12); }
首先看結構,是label裏面套了一個input和一個空的span。ide
input要經過opacity: 0;隱藏起來。spa
用span來製做所須要的樣式,span畫外邊框,span加個before僞類來畫裏面的小圓圈。code
經過相鄰兄弟選擇器來控制選擇與不選擇的狀態input[type=radio]:checked + span:before 。orm
看看代碼,很簡單,實踐一下,你會了麼?htm