利用CSS3美化單選、復(fù)選按鈕的顯示樣式

前言
相信大家都知道在表單元素中,單選按鈕和復(fù)選按鈕都具有選中和未選中狀態(tài)。要覆寫這兩個(gè)按鈕默認(rèn)樣式比較困難。在CSS3中,我們可以通過狀態(tài)選擇器“:checked”配合其他標(biāo)簽實(shí)現(xiàn)自定義樣式。利用CSS3我們可以打造非常具有個(gè)性化的用戶表單,本文中實(shí)現(xiàn)的效果非常不錯(cuò),感興趣的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)。
效果圖如下
實(shí)例代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>復(fù)選單選樣式</title> <link rel="stylesheet" href="style.css"> </head> <style> form { border: 1px solid #ccc; padding: 20px; width: 300px; } .wrapper { margin-bottom: 10px; } /*復(fù)選框*/ .checkbox-box { display: inline-block; width: 20px; height: 20px; margin-right: 10px; position: relative; border: 2px solid orange; vertical-align: middle; } .checkbox-box input { opacity: 0; position: absolute; top:0; left:0; z-index:10; } .checkbox-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; } .checkbox-box input[type="checkbox"] + span { opacity:0; } .checkbox-box input[type="checkbox"]:checked + span { opacity: 1; } /*單選框*/ .redio-box { display: inline-block; width: 30px; height: 30px; margin-right: 10px; position: relative; background: orange; vertical-align: middle; border-radius: 100%; } .redio-box input { opacity: 0; position: absolute; top:0; left:0; width: 100%; height:100%; z-index:100;/*使input按鈕在span的上一層,不加點(diǎn)擊區(qū)域會(huì)出現(xiàn)不靈敏*/ } .redio-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; } .redio-box input[type="radio"] + span { opacity: 0; } .redio-box input[type="radio"]:checked + span { opacity: 1; } </style> <body> <h2>復(fù)選框:</h2> <form action="#"> <div class="wrapper"> <div class="checkbox-box"> <input name="1" type="checkbox" checked id="usename" /> <span>√</span> </div> <label for="usename">體育</label> </div> <div class="wrapper"> <div class="checkbox-box"> <input name="1" type="checkbox" id="usepwd" /> <span>√</span> </div> <label for="usepwd">音樂</label> </div> <div class="wrapper"> <div class="checkbox-box"> <input name="1" type="checkbox" id="checkbox3" /> <span>√</span> </div> <label for="checkbox3">讀書</label> </div> <div class="wrapper"> <div class="checkbox-box"> <input name="1" type="checkbox" id="checkbox4" /> <span>√</span> </div> <label for="checkbox4">運(yùn)動(dòng)</label> </div> </form> <h2>單選框</h2> <form action="#"> <div class="wrapper"> <div class="redio-box"> <input type="radio" checked="checked" id="boy" name="1" /><span></span> </div> <label for="boy">男</label> </div> <div class="wrapper"> <div class="redio-box"> <input type="radio" id="girl" name="1" /><span></span> </div> <label for="girl">女</label> </div> </form> </body> </html>
注意:
+ 是css的相鄰選擇符。
關(guān)系選擇符只有四種,是 空格 > + ~ (包含選擇符、子選擇符、相鄰選擇符、兄弟選擇符)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
CSS3美化單選/復(fù)選/開關(guān)按鈕樣式特效源碼
CSS3美化單選/復(fù)選/開關(guān)按鈕樣式特效源碼是一款純CSS3的單選框,復(fù)選框,開關(guān)按鈕UI庫,實(shí)現(xiàn)了按鈕可選和按鈕不可選的效果,非常不錯(cuò),歡迎有需要的朋友前來下載使用2017-12-21CSS3實(shí)現(xiàn)單選和多選按鈕美化樣式特效源碼
CSS3實(shí)現(xiàn)單選和多選按鈕美化樣式特效源碼是一款可以和多種字體圖標(biāo)結(jié)合使用的按鈕表單特效。本段代碼可以在各個(gè)網(wǎng)頁使用,有需要的朋友可以直接下載使用2017-09-26- 純css3制作checkbox單選按鈕美化樣式特效源碼是一款勾選按鈕美化,單選按鈕美化效果代碼。本段特效源碼可以在各大網(wǎng)站使用,有需要的朋友直接下載使用2017-06-06
超酷CSS3復(fù)選框和單選按鈕點(diǎn)擊動(dòng)畫庫checked.css特效
checked.css是一套能夠制作復(fù)選框和單選按鈕點(diǎn)擊動(dòng)畫的CSS3動(dòng)畫庫的特效,內(nèi)置23種相應(yīng)的動(dòng)畫特效,歡迎下載使用2016-09-14CSS3實(shí)現(xiàn)的多種復(fù)選框和單選按鈕美化效果源碼
是一段單純的實(shí)現(xiàn)了復(fù)選框和單選按鈕美化效果的CSS代碼,本段代碼適用于所有網(wǎng)頁使用,有需要的朋友們可以提取代碼中的相關(guān)段落修改使用2015-01-27基于CSS3實(shí)現(xiàn)的復(fù)選框和單選按鈕美化的動(dòng)態(tài)特效
一款基于CSS3實(shí)現(xiàn)的復(fù)選框和單選按鈕美化的動(dòng)態(tài)特效2014-01-27- css3制作單選框按鈕美化,方形的單選按鈕選中ui特效,非常不錯(cuò),喜歡的朋友快來下載吧2020-12-29