Home  >  Q&A  >  body text

How to vertically center text next to a switch?

I want to make a text in the center of "toggle-label" class vertically with "switch" but I failed This is the code I use

.toggle-label {
  font-weight: bold;
  font-size: 20px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 54px;
  height: 28px;
}

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

.slider-round {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius: 34px;
}

.slider-round:before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius: 50%;
}

.switch input:checked+.slider-round {
  background-color: #e53f71;
}

.switch input:focus+.slider-round {
  box-shadow: 0 0 1px #e53f71;
}

.switch input:checked+.slider-round:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
<span class="toggle-label">BGM</span>
<label class="switch" style="margin-right: 20px;">
     <input type="checkbox" id="bgm" checked>
     <span class="slider-round"></span>
</label>
<span class="toggle-label">SFX</span>
     <label class="switch">
     <input type="checkbox" id="sfx" checked>
<span class="slider-round"></span>
</label>

I tried setting the top padding of the toggle tab but it still doesn't work Please help me center the toggle label vertically with the switch

P粉354948724P粉354948724421 days ago516

reply all(1)I'll reply

  • P粉489081732

    P粉4890817322023-09-16 00:05:29

    Try to add

    Vertical alignment: middle;

    to .toggle-label and .switch.

    This should make them get along well with each other.

    reply
    0
  • Cancelreply