0

I have some toggles with spans next to them (the spans with "True" and "False" in them). I want the spans to be centered in the vertical space that the toggles takes up. How can I best achieve this?

.question__label-group {
    margin-bottom: 2rem;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}



.slider:before {
  position: absolute;
  content: '';
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: #2196f3;
}



input:focus + .slider {
  box-shadow: 0 0 1px #2196f3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
        <div class="question__label-group">
          <label class="switch">
            <input type="radio" name="1" id="1" />
            <span class="slider"></span>
          </label>
          <span>False</span>
        </div>
        <div class="question__label-group">
          <label class="switch">
            <input type="radio" name="1" id="1" />
            <span class="slider"></span>
          </label>
          <span>True</span>
        </div>
Boris Grunwald
  • 2,602
  • 3
  • 20
  • 36

2 Answers2

1

The easiest way to do this would be to give the .question__label-group class the following styles: display: flex and align-items: center.

Updated your fiddle for you. You may also want to give your slider or span a margin left/right.

.question__label-group {
    margin-bottom: 2rem;
    display: flex;
    align-items: center;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}



.slider:before {
  position: absolute;
  content: '';
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: #2196f3;
}



input:focus + .slider {
  box-shadow: 0 0 1px #2196f3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<div class="question__label-group">
          <label class="switch">
            <input type="radio" name="1" id="1" />
            <span class="slider"></span>
          </label>
          <span>False</span>
        </div>
        <div class="question__label-group">
          <label class="switch">
            <input type="radio" name="1" id="1" />
            <span class="slider"></span>
          </label>
          <span>True</span>
        </div>
EGxo
  • 305
  • 2
  • 14
0

update css:

.question__label-group {
    display: flex;
    align-items: center;
    margin-bottom: 2rem;
}