1

How do I create a switch toggle input where a "Y" and "N" appears 'above' the input? In the snippet below, I have a problem where the z-index of the "Y" and "N" covers the input so it is only toggle-able if you click around the z-indexed spans.

Additionally, I would like the letters to change color when the checkbox is toggled, but that is a secondary issue, I think.

body, html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}
.switch__input {
  position: absolute;
  top: 0;
  left: 0;
  width: 90px;
  height: 50px;
  opacity: 0;
  z-index: 0;
}
.switch__label:before {
  content: '';
  text-align: center;
  position: absolute;
  color: red;
  top: 5px;
  left: 0;
  width: 90px;
  height: 35px;
  background-color: rgba(0, 0, 0, 0.26);
  border-radius: 100px;
  z-index: 0;
  -webkit-transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.switch__label:after {
  content: '';
  color: white;
  position: absolute;
  text-align: center;
  font-size: 24px;
  padding: 4.4px 0;
  top: -2px;
  left: 0;
  width: 50px;
  height: 50px;
  background-color: #2d95e5;
  border-radius: 100px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
  z-index: 0;
  -webkit-transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  -webkit-transition-property: left, background-color;
  transition-property: left, background-color;
}
.switch__input:checked + .switch__label:before {
  background-color: rgba(225, 225, 225, 0.5);
}
.switch__input:checked + .switch__label:after {
  left: 40px;
  content: '';
  color: white;
  background-color: #BFBFBF;
}
.yesnocontainer {
  font-family: sans-serif;
  display: flex;
  width: 70px;
  justify-content: space-evenly;
  align-content: center;
}
.yes, .no {
  font-size: 24px;
}
.yes {
  position: relative;
  color: black !important;
  z-index: 999 !important;
}
.no {
  position: relative;
  color: black !important;
  z-index: 999 !important;
}
<div class="switch">
  <input type="checkbox" id="switch1" class="switch__input" checked>
  <label for="switch1" class="switch__label"></label>
  <span class="yesnocontainer">
    <span class="yes">Y</span>
    <span class="no">N</span>
  </span>
</div>
wsyq1n
  • 53
  • 8

2 Answers2

0

.switch__label must be visibled on screen, try this.

body, html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}
/* ADD .switch__label */
.switch__input,.switch__label {
  position: absolute;
  top: 0;
  left: 0;
  width: 90px;
  height: 50px;
  opacity: 0;
  z-index: 0;
}
/*ADD*/
.switch__label{
  z-index:2;
  opacity:1;
}

.switch__label:before {
  content: '';
  text-align: center;
  position: absolute;
  color: red;
  top: 5px;
  left: 0;
  width: 90px;
  height: 35px;
  background-color: rgba(0, 0, 0, 0.26);
  border-radius: 100px;
  z-index: 0;
  -webkit-transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.switch__label:after {
  content: '';
  color: white;
  position: absolute;
  text-align: center;
  font-size: 24px;
  padding: 4.4px 0;
  top: -2px;
  left: 0;
  width: 50px;
  height: 50px;
  background-color: #2d95e5;
  border-radius: 100px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
  z-index: 0;
  -webkit-transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  -webkit-transition-property: left, background-color;
  transition-property: left, background-color;
}
.switch__input:checked + .switch__label:before {
  background-color: rgba(225, 225, 225, 0.5);
}
.switch__input:checked + .switch__label:after {
  left: 40px;
  content: '';
  color: white;
  background-color: #BFBFBF;
}
.yesnocontainer {
  font-family: sans-serif;
  display: flex;
  width: 70px;
  justify-content: space-evenly;
  align-content: center;
}
.yes, .no {
  font-size: 24px;
}
.yes {
  position: relative;
  color: black !important;
  z-index: 1;
}
/* ADD */
.switch__input:checked + .switch__label + .yesnocontainer > .no {
  z-index:3;
}
.no {
  position: relative;
  color: black !important;
  z-index: 1;
}
/* ADD */
.switch__input:not(:checked) + .switch__label + .yesnocontainer > .yes {
  z-index:3;
}
<div class="switch">
  <input type="checkbox" id="switch1" class="switch__input" checked>
  <label for="switch1" class="switch__label"></label>
  <span class="yesnocontainer">
    <span class="yes">Y</span>
    <span class="no">N</span>
  </span>
</div>
Hasan Delibaş
  • 390
  • 2
  • 11
0

 .switch-toggle {
        float: left;
        background:  gray;
     }
     .switch-toggle input {
       position: absolute;
       opacity: 0;
     }
     .switch-toggle input + label {
       padding: 7px;
       float:left;
       color: #fff;
       cursor: pointer;
     }


     .switch-toggle  input:checked  +  .red {
        background:  red;
     }

     .switch-toggle  input:checked  +  .green {
        background:  green;
     }
      <div class="switch-toggle switch-3 switch-candy">
     
       <input id="on" name="state-d" class="green" type="radio" />
       <label for="on" class="green" onclick="">ON</label>
     
       <input id="off" name="state-d" class="red" type="radio" />
       <label for="off"  class="red" onclick="">OFF</label>
     
     </div>
Ghanshyam Nakiya
  • 1,077
  • 11
  • 18