将CSS切换与文本对齐

时间:2017-07-10 20:41:51

标签: html css dom

我有一些以CSS为主题的文字和切换。我希望切换与文本垂直对齐。我没试过好几个方法。感谢您能给我的任何帮助。谢谢!



.switch {
  position: relative;
  display: inline-block;
  width: 30px;
  height: 17px;
}

.switch input {
  display: none;
}

.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: 13px;
  width: 13px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #651C0C;
}

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

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


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

<h5>Reveal Links <label class="switch"><input type="checkbox" onchange="v30showall(this)"><div class="slider round"></div></label></h5>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

您需要使用vertical-align:middle;规则

https://www.w3.org/wiki/CSS/Properties/vertical-align

  

vertical-align属性会影响由内联级元素生成的框的线框内的垂直位置。

&#13;
&#13;
.switch {
  position: relative;
  display: inline-block;
  width: 30px;
  height: 17px;
  vertical-align:middle; /* added */
}

.switch input {
  display: none;
}

.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: 13px;
  width: 13px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #651C0C;
}

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

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


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
&#13;
<h5>Reveal Links <label class="switch"><input type="checkbox" onchange="v30showall(this)"><div class="slider round"></div></label></h5>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我个人建议给Reveal Link个自己的容器(在这种情况下为<span>),并将这两个容器浮动到左侧,而不是display: inline-block上的.switch。 1}}:

h5 span {
  float: left;
}

.switch {
  position: relative;
  float: left;
  width: 30px;
  height: 17px;
}

.switch input {
  display: none;
}

.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: 13px;
  width: 13px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #651C0C;
}

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

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


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<h5>
  <span>Reveal Links</span>
  <label class="switch">
    <input type="checkbox" onchange="v30showall(this)">
    <div class="slider round"></div>
   </label>
</h5>

希望这有帮助! :)