純CSS設置Checkbox複選框控件的樣式

Checkbox複選框是一個可能每個網站都在使用的HTML元素,但大多數人並不給它們設置樣式,因此在絕大多數網站它們看起來是同樣的。爲何不把你的網站中的Checkbox設置一個不同凡響的樣式,甚至可讓它看起來一點也不像複選框。css


在本教程中,咱們將建立5個不一樣的選擇框,你能夠在你的網站上使用它。jquery


css-style-checkboxesweb


查看演示,能夠看到咱們將要建立的複選框樣式。瀏覽器


演示地址ide


首先,須要添加一段CSS隱藏全部的Checkbox複選框,下面咱們會改變它的外觀。要作到點須要添加一段代碼到你的CSS文件中。網站


/**spa

 * 隱藏默認的checkbox翻譯

 */orm

input[type=checkbox] {教程

visibility: hidden;

}

隱藏掉全部的Checkbox複選框後,咱們須要添加一個label HTML元素,咱們都知道,當點擊的有for屬性的label標籤時,對應的Checkbox複選框會被選中。這意味着,咱們能夠經過label的點擊事件來處理咱們的Checkbox複選框。


樣式一


checkbox-one


此複選框風格就像一個解鎖滑塊,滑塊選中和未選中狀態會顯示在的不一樣位置。當單擊滑塊按鈕(label標籤),將會選中複選框,而後滑塊移動到ON位置。


咱們開始建立複選框區的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>

由於這個樣式的複選框,一個label不足以完成任務,咱們用一個DIV元素包含checkbox,咱們須要使用它們來作黑色條帶和圓角。


/**

 * Create the slider bar

 */

.checkboxOne {

width: 40px;

height: 10px;

background: #555;

margin: 20px 80px;

position: relative;

border-radius: 3px;

}

如今,咱們能夠把label做爲條帶上的滑塊,咱們但願按鈕效果是從條帶的一側移動到另外一側,咱們能夠添加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;

}

如今這個滑塊在選中(關閉)位置,當咱們選中複選框,咱們但願有一個反應發生,因此咱們能夠移動滑塊到另外一端。咱們須要知道,判斷複選框被選中,若是是則改變label元素的left屬性。


/**

 * Move the slider in the correct position if the checkbox is clicked

 */

.checkboxOne input[type=checkbox]:checked + label {

left: 27px;

}

這就是你須要的第一個Checkbox複選框的CSS。


樣式二


checkbox-two


此複選框風格像樣式同樣,但不一樣的是,這個滑塊按鈕會改變顏色。當您單擊滑塊按鈕,它移動到條帶的另外一邊,並改變按鈕的顏色。


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>

這個DIV會變成比樣式一大一些的條帶,label依然是做爲滑塊,使用下面的CSS來定義它。


/**

 * Checkbox Two

 */

.checkboxTwo {

width: 120px;

height: 40px;

background: #333;

margin: 20px 60px;

 

border-radius: 50px;

position: relative;

}

這個樣式中間有一個黑色的條,滑塊會沿着它左右滑動,可是DIV元素已經使用了,因此咱們須要用:before僞類建立一個新的元素。


/**

 * Create the line for the circle to move across

 */

.checkboxTwo:before {

content: '';

position: absolute;

top: 19px;

left: 14px;

height: 2px;

width: 90px;

background: #111;

}

和樣式一同樣,接下來咱們爲label寫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;

}

我要實現和樣式一差很少的選中狀態,當選中時改變label的left和background屬性。


/**

 * Create the click event for the checkbox

 */

.checkboxTwo input[type=checkbox]:checked + label {

left: 84px;

background: #26ca28;

}

樣式三


checkbox-three


這個複選框的樣式比樣式二更復雜一些,它和前面的例子同樣會左右滑動,而且當改變選中和未選中的狀態時,滑塊滑動到另外一側而且在原位置顯示對應的文本。


首先,咱們寫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做爲滑塊,下面的代碼會建立一個黑色圓角的條帶,咱們能夠把滑塊和文本放到裏面。


/**

 * Checkbox Three

 */

.checkboxThree {

width: 120px;

height: 40px;

background: #333;

margin: 20px 60px;

 

border-radius: 50px;

position: relative;

}

當滑塊處於未選中狀態時,滑塊會在左側,而且右邊顯示」OFF」,當點擊的時候,滑塊移動到右側,左側顯示」ON」。

可是元素數量不足以讓咱們實現這些功能,因此咱們要用:before和:after兩個僞類建立兩個元素,分別放置」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;

}

和前面同樣,咱們來添加滑塊的樣式,當被點擊時它會移動到另外一側,而且改變顏色。


/**

 * 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;

}

樣式四


checkbox-four


在這個樣式中,咱們會建立兩個圓形,當點擊時改變裏面的圓形的顏色表示選中與未選中的狀態。

和前面同樣的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建立外面的圓形,使用CSS的border-radius屬性,而且設置爲100%就能夠建立一個正圓形。


/**

 * Checkbox Four

 */

.checkboxFour {

width: 40px;

height: 40px;

background: #ddd;

margin: 20px 90px;

 

border-radius: 100%;

position: relative;

-webkit-box-shadow: 0px 1px 3px rgba(0,0,0,0.5);

-moz-box-shadow: 0px 1px 3px rgba(0,0,0,0.5);

box-shadow: 0px 1px 3px rgba(0,0,0,0.5);

}

而後咱們用label元素來建立一個小一點的圓形,它會根據checkbox狀態來改變顏色。


/**

 * 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);

}

當複選框被選中的時候,咱們要改變內圈的背景顏色來表示選中狀態。


/**

 * Create the checked state

 */

.checkboxFour input[type=checkbox]:checked + label {

background: #26ca28;

}

樣式五


checkbox-five


這個複選框的樣式有些不一樣,它看起來只是比瀏覽器默認的checkbox樣式稍微好了些,可是不一樣的是咱們能夠根據本身的須要來定義它的樣式了。

首先仍是同樣的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的滑動條帶或者外部的圓圈,可是這一次咱們不須要了,可使用div元素來設置複選框的區域。


/**

 * Checkbox Five

 */

.checkboxFive {

width: 25px;

margin: 20px 100px;

position: relative;

}

label標籤用於Click事件和咱們要定義的複選框的方框樣式。


/**

 * 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;

}

接下來,咱們要建立方框中的對勾,對於這一點,咱們可使用:after僞類建立一個新的元素,爲了實現這個樣式,咱們能夠建立一個5px x 9px的長方形並給他加上邊框。這時候咱們去掉上面和右邊的邊框以後,它會看起來像一個字母L。而後咱們可使用CSS的transform屬性讓它旋轉一下,這樣看起來就像是一個對勾。


/**

 * 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中,咱們已經設置它的透明度爲0.2,因此你會看到的複選框有一個半透明的對勾。你能夠在懸停的時候加深一點,在選中時,能夠把設置爲不透明。


/**

 * 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;

}

這將會爲你建立全新的checkbox複選框樣式。

觀看演示,看看這些複選框是如何工做的。


演示地址


本文翻譯自 How To Style A Checkbox With CSS

自定義select樣式 Selectyze jquery plugin – Skin your own selects lists with jQuery & CSS

拓展閱讀 CSS3 Checkbox Styles

相關文章
相關標籤/搜索