如何將html的radio單選框自定義樣式為正方形和對號
發(fā)布時間:2023-12-14 16:50:23 作者:_小鄭有點困了
我要評論
如何能把html的<input type="radio" name="option">改成自定義的樣式呢?比如想要把他變成正方形,選中的時候是對號,默認的樣式太丑了,今天給大家分享如何將html的radio單選框自定義樣式為正方形和對號,感興趣的朋友一起看看吧
將html的radio單選框自定義樣式為正方形和對號
背景:
如何能把html的
<input type="radio" name="option">改成自定義的樣式呢?比如想要把他變成正方形,選中的時候是對號。默認的樣式太丑了
默認樣式:

自定義樣式:

實現(xiàn)代碼
<!DOCTYPE html>
<html>
<head>
<style>
input[type="radio"] {
display: none;
}
.square-radio {
display: flex;
position: relative;
width: 20px;
height: 20px;
border: 2px solid #333;
cursor: pointer;
}
.square-radio::after {
content: "?";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 14px;
color: #333;
display: none;
}
input[type="radio"]:checked + .square-radio::after {
color: #2196F3;
display: block;
}
</style>
</head>
<body>
<div class="box">
性別:
<label>
男
<input type="radio" name="option">
<span class="square-radio"></span>
</label>
<label>
女
<input type="radio" name="option">
<span class="square-radio"></span>
</label>
</div>
</body>
</html>到此這篇關(guān)于如何將html的radio單選框自定義樣式為正方形和對號的文章就介紹到這了,更多相關(guān)html radio單選框自定義樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
- 下面小編就為大家?guī)硪黄狧TML的checkbox和radio樣式美化的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-08
- 這篇文章主要介紹了html中radio值的獲取、賦值及注冊事件,非常適合新手朋友,喜歡html的朋友不要錯過了哈2014-05-13

