由於input單選多選的原生樣式通常都不符合需求,所以在實現功能時通常都需要美化按鈕
1. 利用偽類來重置單選按鈕樣式
html
<input type="radio" /> <input type="checkbox" />
css
input{visibility: hidden;} // 讓原生按鈕不顯示 input::before{ content: "";visibility: visible;display:inline-block; width: 0.36rem;height: 0.36rem; // 設置合適的寬高 background: url(../assets/icon/nocheck_icon.png) no-repeat center; // 用自己的圖片替換-這個是未選中的圖片 background-size:contain; } input:checked::before{ content: "";visibility: visible;display:inline-block; width: 0.36rem;height: 0.36rem; background: url(../assets/icon/checked_icon.png) no-repeat center; // 用自己的圖片替換-這個是選中的圖片 background-size:contain; }
2. 利用label來重置單選按鈕樣式
通常都是要和label一起配合使用的,有點擊事件的時候,還是配合label來重寫樣式會更好
html
<input type="radio" name="radio" value="0" id="val_1"><label for="val_1">選項1</label>
css
input{ visibility: hidden; } input+label{ vertical-align: middle; width: 0.3rem; height: 0.3rem; // 設置合適的寬高 background: url(~@/assets/mobile/check.png) no-repeat center; // 用自己的圖片替換-這個是未選中的圖片 background-size:contain; } input:checked+label{ background: url(~@/assets/mobile/checked.png) no-repeat center; // 用自己的圖片替換-這個是選中的圖片 background-size:contain; color: #FF7244; }
