How To Create Accessible Switch Toggle Style Checkbox

stackui
0

No javascript required! Customize the branding easily by changing the values in the mixins.

Switch Colors

Switch Sizes

.sm-switch use this class for small switch

.md-switch use this class for medium switch

.lg-switch use this class for large switch

How to use it

You need to Use this class.can-toggle

HTML for Switch

 
    
     <div class="can-toggle">
     <input id="cb" type="checkbox">
     <label for="cb">
    <div class="can-toggle-switch" data-checked="On" data-unchecked="Off"></div>
    <div class="can-toggle-label-text">Swiming</div>
     </label>
   </div>
    
   

you can use selected switch color and sizes same as we did for bootstrap buttons.

CSS for Switch with a colors & Sizes varaitions

 
       
.can-toggle {
  position: relative;
}
.can-toggle *, .can-toggle *:before, .can-toggle *:after {
  box-sizing: border-box;
}
.can-toggle input[type="checkbox"] {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
}
.can-toggle input[type="checkbox"][disabled] ~ label {
  pointer-events: none;
}
.can-toggle input[type="checkbox"][disabled] ~ label .can-toggle-switch {
  opacity: 0.4;
}
.can-toggle input[type="checkbox"]:checked ~ label .can-toggle-switch:before {
  content: attr(data-unchecked);
  left: 0;
}
.can-toggle input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  content: attr(data-checked);
}
.can-toggle label {
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
      -ms-flex-align: center;
          align-items: center;
}
.can-toggle label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
  padding-left: 32px;
}
.can-toggle label .can-toggle-switch {
  position: relative;
}
.can-toggle label .can-toggle-switch:before {
  content: attr(data-checked);
  position: absolute;
  top: 0;
  text-transform: uppercase;
  text-align: center;
}
.can-toggle label .can-toggle-switch:after {
  content: attr(data-unchecked);
  position: absolute;
  z-index: 5;
  text-transform: uppercase;
  text-align: center;
  background: white;
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}
.can-toggle input[type="checkbox"][disabled] ~ label {
  color: rgba(119, 119, 119, 0.5);
}
.can-toggle input[type="checkbox"]:focus ~ label .can-toggle-switch, .can-toggle input[type="checkbox"]:hover ~ label .can-toggle-switch {
  background-color: #777;
}
.can-toggle input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  color: #5e5e5e;
}
.can-toggle input[type="checkbox"]:hover ~ label {
  color: #6a6a6a;
}
.can-toggle input[type="checkbox"]:checked ~ label:hover {
  color: #55bc49;
}
.can-toggle input[type="checkbox"]:checked ~ label .can-toggle-switch {
  background-color: #70c767;
}
.can-toggle input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  color: #29c7ca;
}
.can-toggle input[type="checkbox"]:checked:focus ~ label .can-toggle-switch, .can-toggle input[type="checkbox"]:checked:hover ~ label .can-toggle-switch {
  background-color: #29c7ca;
}
.can-toggle input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  color: #29c7ca;
}
.can-toggle label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.can-toggle label .can-toggle-switch {
  -webkit-transition: background-color 0.3s cubic-bezier(0, 1, 0.5, 1);
          transition: background-color 0.3s cubic-bezier(0, 1, 0.5, 1);
  background: #848484;
}
.can-toggle label .can-toggle-switch:before {
  color: rgba(255, 255, 255, 0.5);
}
.can-toggle label .can-toggle-switch:after {
  -webkit-transition: -webkit-transform 0.3s cubic-bezier(0, 1, 0.5, 1);
  transition: transform 0.3s cubic-bezier(0, 1, 0.5, 1);
  color: #777;
}
.can-toggle input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 3px 3px rgba(0, 0, 0, 0.4);
}
.can-toggle input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  -webkit-transform: translate3d(65px, 0, 0);
          transform: translate3d(65px, 0, 0);
}
.can-toggle input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 3px 3px rgba(0, 0, 0, 0.4);
}
.can-toggle label {
  font-size: 14px;
}
.can-toggle label .can-toggle-switch {
  height: 36px;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 134px;
      -ms-flex: 0 0 134px;
          flex: 0 0 134px;
  border-radius: 4px;
}
.can-toggle label .can-toggle-switch:before {
  left: 67px;
  font-size: 12px;
  line-height: 36px;
  width: 67px;
  padding: 0 12px;
}
.can-toggle label .can-toggle-switch:after {
  top: 2px;
  left: 2px;
  border-radius: 2px;
  width: 65px;
  line-height: 32px;
  font-size: 12px;
}
.can-toggle label .can-toggle-switch:hover:after {
  box-shadow: 0 3px 3px rgba(0, 0, 0, 0.4);
}

   

CSS for Color Variations

   
    
/*** primary Color ***/
.can-toggle.primary-switch input[type="checkbox"][disabled] ~ label {
  color: rgba(181, 62, 116, 0.5);
}
.can-toggle.primary-switch input[type="checkbox"]:focus ~ label .can-toggle-switch, .can-toggle.primary-switch input[type="checkbox"]:hover ~ label .can-toggle-switch {
    background-color: #204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.primary-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  color: #204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:hover ~ label {
 color: #204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:checked ~ label:hover {
  color: #204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:checked ~ label .can-toggle-switch {
  background-color:#204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  color: #204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch, .can-toggle.primary-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch {
  background-color: #204d74;
}
.can-toggle.primary-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.primary-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  color: #204d74;
}
.can-toggle.primary-switch label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.can-toggle.primary-switch label .can-toggle-switch {
  -webkit-transition: background-color 0.3s ease-in-out;
          transition: background-color 0.3s ease-in-out;
  background: #204d74;
}
.can-toggle.primary-switch label .can-toggle-switch:before {
  color: rgba(255, 255, 255, 0.6);
}
.can-toggle.primary-switch label .can-toggle-switch:after {
  -webkit-transition: -webkit-transform 0.3s ease-in-out;
  transition: transform 0.3s ease-in-out;
  color: #204d74;
}

/*** info Color ***/
.can-toggle.info-switch input[type="checkbox"][disabled] ~ label {
  color: rgba(181, 62, 116, 0.5);
}
.can-toggle.info-switch input[type="checkbox"]:focus ~ label .can-toggle-switch, .can-toggle.info-switch input[type="checkbox"]:hover ~ label .can-toggle-switch {
    background-color: #5bc0de;
}
.can-toggle.info-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.info-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  color: #5bc0de;
}
.can-toggle.info-switch input[type="checkbox"]:hover ~ label {
 color: #5bc0de;
}
.can-toggle.info-switch input[type="checkbox"]:checked ~ label:hover {
  color: #31b0d5;
}
.can-toggle.info-switch input[type="checkbox"]:checked ~ label .can-toggle-switch {
  background-color: #31b0d5;
}
.can-toggle.info-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  color: #5cb85c;
}
.can-toggle.info-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch, .can-toggle.info-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch {
  background-color: #2596b7;
}
.can-toggle.info-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.info-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  color: #2596b7;
}
.can-toggle.info-switch label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.can-toggle.info-switch label .can-toggle-switch {
  -webkit-transition: background-color 0.3s ease-in-out;
          transition: background-color 0.3s ease-in-out;
  background: #5bc0de;
}
.can-toggle.info-switch label .can-toggle-switch:before {
  color: rgba(255, 255, 255, 0.6);
}
.can-toggle.info-switch label .can-toggle-switch:after {
  -webkit-transition: -webkit-transform 0.3s ease-in-out;
  transition: transform 0.3s ease-in-out;
  color: #5bc0de;
}

/*** success Color ***/
.can-toggle.success-switch input[type="checkbox"][disabled] ~ label {
  color: rgba(181, 62, 116, 0.5);
}
.can-toggle.success-switch input[type="checkbox"]:focus ~ label .can-toggle-switch, .can-toggle.success-switch input[type="checkbox"]:hover ~ label .can-toggle-switch {
    background-color: #5cb85c;
}
.can-toggle.success-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.success-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  color: #5cb85c;
}
.can-toggle.success-switch input[type="checkbox"]:hover ~ label {
 color: #5cb85c;
}
.can-toggle.success-switch input[type="checkbox"]:checked ~ label:hover {
  color: #31b0d5;
}
.can-toggle.success-switch input[type="checkbox"]:checked ~ label .can-toggle-switch {
  background-color: #31b0d5;
}
.can-toggle.success-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  color: #5cb85c;
}
.can-toggle.success-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch, .can-toggle.success-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch {
  background-color: #2b952b;
}
.can-toggle.success-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.success-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  color: #2b952b;
}
.can-toggle.success-switch label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.can-toggle.success-switch label .can-toggle-switch {
  -webkit-transition: background-color 0.3s ease-in-out;
          transition: background-color 0.3s ease-in-out;
  background: #5cb85c;
}
.can-toggle.success-switch label .can-toggle-switch:before {
  color: rgba(255, 255, 255, 0.6);
}
.can-toggle.success-switch label .can-toggle-switch:after {
  -webkit-transition: -webkit-transform 0.3s ease-in-out;
  transition: transform 0.3s ease-in-out;
  color: #5cb85c;
}


/*** warning Color ***/
.can-toggle.warning-switch input[type="checkbox"][disabled] ~ label {
  color: rgba(181, 62, 116, 0.5);
}
.can-toggle.warning-switch input[type="checkbox"]:focus ~ label .can-toggle-switch, .can-toggle.warning-switch input[type="checkbox"]:hover ~ label .can-toggle-switch {
    background-color: #d58512;
}
.can-toggle.warning-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.warning-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  color: #d58512;
}
.can-toggle.warning-switch input[type="checkbox"]:hover ~ label {
 color: #d58512;
}
.can-toggle.warning-switch input[type="checkbox"]:checked ~ label:hover {
  color: #d58512;
}
.can-toggle.warning-switch input[type="checkbox"]:checked ~ label .can-toggle-switch {
  background-color: #d58512;
}
.can-toggle.warning-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  color: #5cb85c;
}
.can-toggle.warning-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch, .can-toggle.warning-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch {
  background-color: #985f0d;
}
.can-toggle.warning-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.warning-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  color: #985f0d;
}
.can-toggle.warning-switch label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.can-toggle.warning-switch label .can-toggle-switch {
  -webkit-transition: background-color 0.3s ease-in-out;
          transition: background-color 0.3s ease-in-out;
  background: #d58512;
}
.can-toggle.warning-switch label .can-toggle-switch:before {
  color: rgba(255, 255, 255, 0.6);
}
.can-toggle.warning-switch label .can-toggle-switch:after {
  -webkit-transition: -webkit-transform 0.3s ease-in-out;
  transition: transform 0.3s ease-in-out;
  color: #d58512;
}


/*** danger Color ***/
.can-toggle.danger-switch input[type="checkbox"][disabled] ~ label {
  color: rgba(181, 62, 116, 0.5);
}
.can-toggle.danger-switch input[type="checkbox"]:focus ~ label .can-toggle-switch, .can-toggle.danger-switch input[type="checkbox"]:hover ~ label .can-toggle-switch {
    background-color: #ac2925;
}
.can-toggle.danger-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.danger-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  color: #ac2925;
}
.can-toggle.danger-switch input[type="checkbox"]:hover ~ label {
 color: #761c19;
}
.can-toggle.danger-switch input[type="checkbox"]:checked ~ label:hover {
  color: #761c19;
}
.can-toggle.danger-switch input[type="checkbox"]:checked ~ label .can-toggle-switch {
  background-color: #761c19;
}
.can-toggle.danger-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  color: #761c19;
}
.can-toggle.danger-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch, .can-toggle.danger-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch {
  background-color: #761c19;
}
.can-toggle.danger-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.danger-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  color: #761c19;
}
.can-toggle.danger-switch label .can-toggle-label-text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.can-toggle.danger-switch label .can-toggle-switch {
  -webkit-transition: background-color 0.3s ease-in-out;
          transition: background-color 0.3s ease-in-out;
  background: #ac2925;
}
.can-toggle.danger-switch label .can-toggle-switch:before {
  color: rgba(255, 255, 255, 0.6);
}
.can-toggle.danger-switch label .can-toggle-switch:after {
  -webkit-transition: -webkit-transform 0.3s ease-in-out;
  transition: transform 0.3s ease-in-out;
  color: #ac2925;
}

   
  

CSS for Sizes Variations

   
    

/*** Large Size ***/
.can-toggle.lg-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.lg-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 4px 4px rgba(0, 0, 0, 0.4);
}
.can-toggle.lg-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  -webkit-transform: translate3d(78px, 0, 0);
          transform: translate3d(78px, 0, 0);
}
.can-toggle.lg-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.lg-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 4px 4px rgba(0, 0, 0, 0.4);
}
.can-toggle.lg-switch label {
  font-size: 14px;
}
.can-toggle.lg-switch label .can-toggle-switch {
  height: 50px;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 160px;
      -ms-flex: 0 0 160px;
          flex: 0 0 160px;
  border-radius: 4px;
}
.can-toggle.lg-switch label .can-toggle-switch:before {
  left: 80px;
  font-size: 14px;
  line-height: 50px;
  width: 80px;
  padding: 0 12px;
}
.can-toggle.lg-switch label .can-toggle-switch:after {
  top: 2px;
  left: 2px;
  border-radius: 2px;
  width: 78px;
  line-height: 46px;
  font-size: 14px;
}
.can-toggle.lg-switch label .can-toggle-switch:hover:after {
  box-shadow: 0 4px 4px rgba(0, 0, 0, 0.4);
}

/*** md Size ***/
.can-toggle.md-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.md-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4);
}
.can-toggle.md-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  -webkit-transform: translate3d(65px, 0, 0);
          transform: translate3d(65px, 0, 0);
}
.can-toggle.md-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.md-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4);
}
.can-toggle.md-switch label {
  font-size: 13px;
}
.can-toggle.md-switch label .can-toggle-switch {
  height: 36px;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 134px;
      -ms-flex: 0 0 134px;
          flex: 0 0 134px;
  border-radius: 4px;
}
.can-toggle.md-switch label .can-toggle-switch:before {
  left: 67px;
  font-size: 12px;
  line-height: 36px;
  width: 67px;
  padding: 0 12px;
}
.can-toggle.md-switch label .can-toggle-switch:after {
 top: 2px;
  left: 2px;
  border-radius: 2px;
  width: 65px;
  line-height: 32px;
  font-size: 12px;
}
.can-toggle.md-switch label .can-toggle-switch:hover:after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4);
}


/*** SM Size ***/
.can-toggle.sm-switch input[type="checkbox"]:focus ~ label .can-toggle-switch:after, .can-toggle.sm-switch input[type="checkbox"]:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4);
}
.can-toggle.sm-switch input[type="checkbox"]:checked ~ label .can-toggle-switch:after {
  -webkit-transform: translate3d(44px, 0, 0);
          transform: translate3d(44px, 0, 0);
}
.can-toggle.sm-switch input[type="checkbox"]:checked:focus ~ label .can-toggle-switch:after, .can-toggle.sm-switch input[type="checkbox"]:checked:hover ~ label .can-toggle-switch:after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4);
}
.can-toggle.sm-switch label {
  font-size: 13px;
}
.can-toggle.sm-switch label .can-toggle-switch {
  height: 28px;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 90px;
      -ms-flex: 0 0 90px;
          flex: 0 0 90px;
  border-radius: 2px;
}
.can-toggle.sm-switch label .can-toggle-switch:before {
  left: 40px;
  font-size: 10px;
  line-height: 28px;
  width: 45px;
  padding: 0 12px;
}
.can-toggle.sm-switch label .can-toggle-switch:after {
  top: 1px;
  left: 1px;
  border-radius: 1px;
  width: 44px;
  line-height: 26px;
  font-size: 10px;
}
.can-toggle.sm-switch label .can-toggle-switch:hover:after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4);
}
  
Click here to Download Code Download

Post a Comment

0Comments

Post a Comment (0)
Demos Buy Now