Implement checkbox with pure CSS

Source: Internet
Author: User

For the checkbox implemented by pure css, see my codepen.

The key is to hide the native input and use its related label to detect click events.

Then, the checkbox check is implemented by combining the border on the right and bottom, and rotating it.

Okay, let's look at the source code.

<div class="container">    <div class="checkboxouter">        <input type="checkbox">        <label class="checkbox"></label>             </div></div>
CSS is the key. SASS and Prefix free are used.

/* SASS variable definition */$ border-styles: 2px solid lightgrey; $ tick-color: #27ae60; $ box-color: # fff; /* align the page background and center, not the key code in this case */body {background-color: $ bgcolor ;}. container {position: absolute; top: 50%; left: 50%; height: 40px; width: 40px; text-align: center; transform: translate (-50%,-50% );} /* implement the outer border of the check box */. checkboxouter {height: 40px; width: 40px; border-radius: 3px; background-color: $ box-color; display: B Lock; border: $ border-styles; transition: all. 5 s ;}. checkboxouter: hover {transform: scale (1.5);}/* Original checkbox hiding */input [type = "checkbox"] {opacity: 0; background: red; position: absolute; cursor: pointer; z-index: 1; height: 100%; width: 100%; left: 0; top: 0 ;} /* is the check box in the implementation of this stuff */. checkbox {position: absolute; border-bottom: 3px solid lightgrey; border-right: 3px solid lightgrey; background-colo R: transparent; height: 20px; width: 10px; transform: rotate (45deg); transform-origin:-35% 30%; transition: all 0.2 s ;} /* label to activate the Click Event */input [type = "checkbox"]: checked ~. Checkbox {transition: all 0.3 s; border-bottom: 3px solid $ tick-color; border-right: 3px solid $ tick-color ;}

We recommend a click event processing tutorial on CSS. The use of CSS in 5m3d processes click events.

Well, today's article is a little simple. You can say, "Hai Han ., Please go to my codepen to edit and download the results online.

---------------------------------------------------------------
Front-end development of whqet, focus on web Front-end development technology, and share webpage-related resources.
---------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.