一波CSS的Checkbox復(fù)選框樣式代碼分享

Checkbox復(fù)選框是一個(gè)可能每一個(gè)網(wǎng)站都在使用的HTML元素,但大多數(shù)人并不給它們?cè)O(shè)置樣式,所以在絕大多數(shù)網(wǎng)站它們看起來是一樣的。為什么不把你的網(wǎng)站中的Checkbox設(shè)置一個(gè)與眾不同的樣式,甚至可以讓它看起來一點(diǎn)也不像復(fù)選框。
在本教程中,我們將創(chuàng)建5個(gè)不同的選擇框,你可以在你的網(wǎng)站上使用它。
查看演示,可以看到我們將要?jiǎng)?chuàng)建的復(fù)選框樣式。
演示地址
首先,需要添加一段CSS隱藏所有的Checkbox復(fù)選框,下面我們會(huì)改變它的外觀。要做到點(diǎn)需要添加一段代碼到你的CSS文件中。
- /**
- * 隱藏默認(rèn)的checkbox
- */
- input[type=checkbox] {
- visibility: hidden;
- }
隱藏掉所有的Checkbox復(fù)選框后,我們需要添加一個(gè)label HTML元素,我們都知道,當(dāng)點(diǎn)擊的有for屬性的label標(biāo)簽時(shí),對(duì)應(yīng)的Checkbox復(fù)選框會(huì)被選中。這意味著,我們可以通過label的點(diǎn)擊事件來處理我們的Checkbox復(fù)選框。
樣式一
此復(fù)選框風(fēng)格就像一個(gè)解鎖滑塊,滑塊選中和未選中狀態(tài)會(huì)顯示在的不同位置。當(dāng)單擊滑塊按鈕(label標(biāo)簽),將會(huì)選中復(fù)選框,然后滑塊移動(dòng)到ON位置。
我們開始創(chuàng)建復(fù)選框區(qū)的HTML。
- <section>
- <!-- Checbox One -->
- <h3>Checkbox One</h3>
- <div class="checkboxOne">
- <input type="checkbox" value="1" id="checkboxOneInput" name="" />
- <label for="checkboxOneInput"></label>
- </div>
- </section>
因?yàn)檫@個(gè)樣式的復(fù)選框,一個(gè)label不足以完成任務(wù),我們用一個(gè)DIV元素包含checkbox,我們需要使用它們來做黑色條帶和圓角。
- /**
- * Create the slider bar
- */
- .checkboxOne {
- width: 40px;
- height: 10px;
- background: #555;
- margin: 20px 80px;
- position: relative;
- border-radius: 3px;
- }
現(xiàn)在,我們可以把label作為條帶上的滑塊,我們希望按鈕效果是從條帶的一側(cè)移動(dòng)到另一側(cè),我們可以添加label的過渡。
- /**
- * Create the slider from the label
- */
- .checkboxOne label {
- display: block;
- width: 16px;
- height: 16px;
- border-radius: 50%;
- -webkit-transition: all .5s ease;
- -moz-transition: all .5s ease;
- -o-transition: all .5s ease;
- -ms-transition: all .5s ease;
- transition: all .5s ease;
- cursor: pointer;
- position: absolute;
- top: -3px;
- left: -3px;
- background: #ccc;
- }
現(xiàn)在這個(gè)滑塊在選中(關(guān)閉)位置,當(dāng)我們選中復(fù)選框,我們希望有一個(gè)反應(yīng)發(fā)生,所以我們可以移動(dòng)滑塊到另一端。我們需要知道,判斷復(fù)選框被選中,如果是則改變label元素的left屬性。
- /**
- * Move the slider in the correct position if the checkbox is clicked
- */
- .checkboxOne input[type=checkbox]:checked + label {
- left: 27px;
- }
這就是你需要的第一個(gè)Checkbox復(fù)選框的CSS。
樣式二
此復(fù)選框風(fēng)格像樣式一樣,但不同的是,這個(gè)滑塊按鈕會(huì)改變顏色。當(dāng)您單擊滑塊按鈕,它移動(dòng)到條帶的另一邊,并改變按鈕的顏色。
HTML代碼和樣式一是完全一樣的。
- <section>
- <!-- Checbox Two -->
- <h3>Checkbox Two</h3>
- <div class="checkboxTwo">
- <input type="checkbox" value="1" id="checkboxTwoInput" name="" />
- <label for="checkboxTwoInput"></label>
- </div>
- </section>
這個(gè)DIV會(huì)變成比樣式一大一些的條帶,label依然是作為滑塊,使用下面的CSS來定義它。
- /**
- * Checkbox Two
- */
- .checkboxTwo {
- width: 120px;
- height: 40px;
- background: #333;
- margin: 20px 60px;
- border-radius: 50px;
- position: relative;
- }
這個(gè)樣式中間有一個(gè)黑色的條,滑塊會(huì)沿著它左右滑動(dòng),但是DIV元素已經(jīng)使用了,所以我們需要用:before偽類創(chuàng)建一個(gè)新的元素。
- /**
- * Create the line for the circle to move across
- */
- .checkboxTwo:before {
- content: '';
- position: absolute;
- top: 19px;
- left: 14px;
- height: 2px;
- width: 90px;
- background: #111;
- }
和樣式一一樣,接下來我們?yōu)閘abel寫CSS樣式,把它用作滑塊。
- /**
- * Create the circle to click
- */
- .checkboxTwo label {
- display: block;
- width: 22px;
- height: 22px;
- border-radius: 50%;
- -webkit-transition: all .5s ease;
- -moz-transition: all .5s ease;
- -o-transition: all .5s ease;
- -ms-transition: all .5s ease;
- transition: all .5s ease;
- cursor: pointer;
- position: absolute;
- top: 9px;
- z-index: 1;
- left: 12px;
- background: #ddd;
- }
我要實(shí)現(xiàn)和樣式一差不多的選中狀態(tài),當(dāng)選中時(shí)改變label的left和background屬性。
- /**
- * Create the click event for the checkbox
- */
- .checkboxTwo input[type=checkbox]:checked + label {
- left: 84px;
- background: #26ca28;
- }
樣式三
這個(gè)復(fù)選框的樣式比樣式二更復(fù)雜一些,它和前面的例子一樣會(huì)左右滑動(dòng),并且當(dāng)改變選中和未選中的狀態(tài)時(shí),滑塊滑動(dòng)到另一側(cè)并且在原位置顯示對(duì)應(yīng)的文本。
首先,我們寫HTML代碼,這和前面是相同的。
- <section>
- <!-- Checbox Three -->
- <h3>Checkbox Three</h3>
- <div class="checkboxThree">
- <input type="checkbox" value="1" id="checkboxThreeInput" name="" />
- <label for="checkboxThreeInput"></label>
- </div>
- </section>
然后,我們用相同的方式把div作為滑塊,下面的代碼會(huì)創(chuàng)建一個(gè)黑色圓角的條帶,我們可以把滑塊和文本放到里面。
- /**
- * Checkbox Three
- */
- .checkboxThree {
- width: 120px;
- height: 40px;
- background: #333;
- margin: 20px 60px;
- border-radius: 50px;
- position: relative;
- }
當(dāng)滑塊處于未選中狀態(tài)時(shí),滑塊會(huì)在左側(cè),并且右邊顯示”OFF”,當(dāng)點(diǎn)擊的時(shí)候,滑塊移動(dòng)到右側(cè),左側(cè)顯示”ON”。
但是元素?cái)?shù)量不足以讓我們實(shí)現(xiàn)這些功能,所以我們要用:before和:after兩個(gè)偽類創(chuàng)建兩個(gè)元素,分別放置”ON”和”OFF”。
- /**
- * Create the text for the On position
- */
- .checkboxThree:before {
- content: 'On';
- position: absolute;
- top: 12px;
- left: 13px;
- height: 2px;
- color: #26ca28;
- font-size: 16px;
- }
- /**
- * Create the label for the off position
- */
- .checkboxThree:after {
- content: 'Off';
- position: absolute;
- top: 12px;
- left: 84px;
- height: 2px;
- color: #ddd;
- font-size: 16px;
- }
和前面一樣,我們來添加滑塊的樣式,當(dāng)被點(diǎn)擊時(shí)它會(huì)移動(dòng)到另一側(cè),并且改變顏色。
- /**
- * Create the pill to click
- */
- .checkboxThree label {
- display: block;
- width: 52px;
- height: 22px;
- border-radius: 50px;
- -webkit-transition: all .5s ease;
- -moz-transition: all .5s ease;
- -o-transition: all .5s ease;
- -ms-transition: all .5s ease;
- transition: all .5s ease;
- cursor: pointer;
- position: absolute;
- top: 9px;
- z-index: 1;
- left: 12px;
- background: #ddd;
- }
- /**
- * Create the checkbox event for the label
- */
- .checkboxThree input[type=checkbox]:checked + label {
- left: 60px;
- background: #26ca28;
- }
樣式四
在這個(gè)樣式中,我們會(huì)創(chuàng)建兩個(gè)圓形,當(dāng)點(diǎn)擊時(shí)改變里面的圓形的顏色表示選中與未選中的狀態(tài)。
和前面一樣的HTML代碼。
- <section>
- <!-- Checbox Four -->
- <h3>Checkbox Four</h3>
- <div class="checkboxFour">
- <input type="checkbox" value="1" id="checkboxFourInput" name="" />
- <label for="checkboxFourInput"></label>
- </div>
- </section>
接下來我們要為checkbox創(chuàng)建外面的圓形,使用CSS的border-radius屬性,并且設(shè)置為100%就可以創(chuàng)建一個(gè)正圓形。
- <section>
- <!-- Checbox Four -->
- <h3>Checkbox Four</h3>
- <div class="checkboxFour">
- <input type="checkbox" value="1" id="checkboxFourInput" name="" />
- <label for="checkboxFourInput"></label>
- </div>
- </section>
然后我們用label元素來創(chuàng)建一個(gè)小一點(diǎn)的圓形,它會(huì)根據(jù)checkbox狀態(tài)來改變顏色。
- /**
- * Create the checkbox button
- */
- .checkboxFour label {
- display: block;
- width: 30px;
- height: 30px;
- border-radius: 100px;
- -webkit-transition: all .5s ease;
- -moz-transition: all .5s ease;
- -o-transition: all .5s ease;
- -ms-transition: all .5s ease;
- transition: all .5s ease;
- cursor: pointer;
- position: absolute;
- top: 5px;
- left: 5px;
- z-index: 1;
- background: #333;
- -webkit-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);
- -moz-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);
- box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);
- }
當(dāng)復(fù)選框被選中的時(shí)候,我們要改變內(nèi)圈的背景顏色來表示選中狀態(tài)。
- /**
- * Create the checked state
- */
- .checkboxFour input[type=checkbox]:checked + label {
- background: #26ca28;
- }
樣式五
這個(gè)復(fù)選框的樣式有些不同,它看起來只是比瀏覽器默認(rèn)的checkbox樣式稍微好了些,但是不同的是我們可以根據(jù)自己的需要來定義它的樣式了。
首先還是一樣的HTML代碼
- <section>
- <!-- Checbox Five -->
- <h3>Checkbox Five</h3>
- <div class="checkboxFive">
- <input type="checkbox" value="1" id="checkboxFiveInput" name="" />
- <label for="checkboxFiveInput"></label>
- </div>
- </section>
在前面的例子中,我們把div作為checkbox的滑動(dòng)條帶或者外部的圓圈,但是這一次我們不需要了,可以使用div元素來設(shè)置復(fù)選框的區(qū)域。
- /**
- * Checkbox Five
- */
- .checkboxFive {
- width: 25px;
- margin: 20px 100px;
- position: relative;
- }
label標(biāo)簽用于Click事件和我們要定義的復(fù)選框的方框樣式。
- /**
- * Create the box for the checkbox
- */
- .checkboxFive label {
- cursor: pointer;
- position: absolute;
- width: 25px;
- height: 25px;
- top: 0;
- left: 0;
- background: #eee;
- border:1px solid #ddd;
- }
接下來,我們要?jiǎng)?chuàng)建方框中的對(duì)勾,對(duì)于這一點(diǎn),我們可以使用:after偽類創(chuàng)建一個(gè)新的元素,為了實(shí)現(xiàn)這個(gè)樣式,我們可以創(chuàng)建一個(gè)5px x 9px的長方形并給他加上邊框。這時(shí)候我們?nèi)サ羯厦婧陀疫叺倪吙蛑?,它?huì)看起來像一個(gè)字母L。然后我們可以使用CSS的transform屬性讓它旋轉(zhuǎn)一下,這樣看起來就像是一個(gè)對(duì)勾。
- /**
- * Display the tick inside the checkbox
- */
- .checkboxFive label:after {
- opacity: 0.2;
- content: '';
- position: absolute;
- width: 9px;
- height: 5px;
- background: transparent;
- top: 6px;
- left: 7px;
- border: 3px solid #333;
- border-top: none;
- border-right: none;
- -webkit-transform: rotate(-45deg);
- -moz-transform: rotate(-45deg);
- -o-transform: rotate(-45deg);
- -ms-transform: rotate(-45deg);
- transform: rotate(-45deg);
- }
在上面的CSS中,我們已經(jīng)設(shè)置它的透明度為0.2,所以你會(huì)看到的復(fù)選框有一個(gè)半透明的對(duì)勾。你可以在懸停的時(shí)候加深一點(diǎn),在選中時(shí),可以把設(shè)置為不透明。
- /**
- * Create the hover event of the tick
- */
- .checkboxFive label:hover::after {
- opacity: 0.5;
- }
- /**
- * Create the checkbox state for the tick
- */
- .checkboxFive input[type=checkbox]:checked + label:after {
- opacity: 1;
- }
相關(guān)文章
HTML+CSS實(shí)現(xiàn)單選框、復(fù)選框美觀的樣式
這篇文章主要介紹了HTML+CSS實(shí)現(xiàn)單選框、復(fù)選框美觀的樣式,需要的朋友可以參考下2017-06-2611種炫酷CSS3復(fù)選框checkbox樣式美化特效源碼
這是一款效果非??岬腃SS3復(fù)選框checkbox樣式美化效果,共有11種特效,帶上了一些動(dòng)畫效果非常炫酷,歡迎下載試試2017-05-19- 這篇文章主要為大家分享一款CSS自定義綠色復(fù)選框按鈕樣式,2016-03-04
9款樣式迷人的CSS3自定義Checkbox復(fù)選框特效源碼
天我們來分享一款9款樣式迷人的CSS3漂亮的自定義Checkbox復(fù)選框。這幾款復(fù)選框樣式很豐富,使用起來也比較方便2014-11-07CSS定義Radio單選項(xiàng)和Checkbox復(fù)選框樣式有效代碼
利用css來定義Radio單選項(xiàng)和Checkbox復(fù)選框樣式是無效的,下面有個(gè)不錯(cuò)的方法,大家可以學(xué)習(xí)下2014-02-21純CSS設(shè)置Checkbox復(fù)選框控件的樣式(五種方法)
下面是純CSS設(shè)置Checkbox復(fù)選框控件的五種簡單樣式,有興趣的可以進(jìn)行改動(dòng)將其變成自己想要的樣式2017-07-05