1
votes

Bootstrap aligne le texte verticalement sur le bouton de commutation

Je souhaite aligner un texte dans un élément ou sur ce bouton de commutation personnalisé:

<span>Custom switch</span>
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>
.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: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .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%;
}

Je n'arrive pas à le faire fonctionner en définissant la margin ou float sur l'élément de classe switch .

p>


0 commentaires

3 Réponses :


2
votes

Utilisez flex

<span>Custom switch</span>
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>
body {
 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: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .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%;
}


5 commentaires

Flex est en effet la meilleure option


cela fonctionne, mais cela peut entraîner des erreurs dans un groupe de saisie qui est inline comme un form-inline ou un inline -table mode d'affichage. Comment les combiner?


@loretoparisi pouvez-vous en partager un exemple?


jsfiddle.net/updcx1Lo en fait cela fonctionne ici, le problème, je pense, était un CSS personnalisé que j'avais ailleurs!


@loretoparisi vous pouvez regrouper les éléments dans un élément div séparé et donner du style à cet élément au lieu de donner un style flex au corps



1
votes

<div class="parent-div">
  <div class="left-div">
    <span>Custom switch</span>
   </div>
  <div class="right-div">
    <label class="switch">
      <input type="checkbox" checked>
      <span class="slider round"></span>
    </label>
   </div>
<div>
.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: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .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%;
}
.right-div {
display: inline-block;
}
.left-div {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.parent-div{
display:table;
}


0 commentaires

1
votes

Comme alternative à flex, vous pouvez également utiliser des tables css:

<div class="switch-container">
  <span class="switch-label">Custom switch</span>
  <label class="switch">
    <input type="checkbox" checked>
    <span class="slider round"></span>
  </label>
</div>
.switch-container {
  display: table;
}

.switch-label {
  display: table-cell;
  vertical-align: middle;
}

.switch {
  position: relative;
  display: table-cell;
  vertical-align: center;
  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: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .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%;
}


2 commentaires

J'ai ajouté un float: left au .switch-container afin d'aligner les éléments en ligne.


float: left ne doit pas casser l'attribut display: table .