如何在html中并排对齐<label>和文本

时间:2017-05-01 08:39:38

标签: html css

我有标签和文字。如何将它们水平并排对齐?

目前,文本在标签的底部对齐(如图中所示)。我想将文本与标签的中心对齐。

我的HTML代码是

<div>
    <label class="switchnmn">
       <input type="checkbox" checked>
       <div class="slidernmn round"></div>
    </label>
        <b style="font-size:15px">I am able to accept customer's any request date.</b>    
</dlv>

我的css文件是

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

.switchnmn input {display:none;}

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

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

input:checked + .slidernmn {
  background-color: #2196F3;
}

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

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

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

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

我应该在哪里更改?

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以在label和b元素上添加vertical-align: middle

&#13;
&#13;
.switchnmn {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switchnmn input {display:none;}

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

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

input:checked + .slidernmn {
  background-color: #2196F3;
}

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

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

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

.slidernmn.round:before {
  border-radius: 50%;
}
label, b {
  vertical-align: middle;
}
&#13;
<div>
  <label class="switchnmn">
    <input type="checkbox" checked>
    <div class="slidernmn round"></div>
  </label>
  <b style="font-size:15px">I am able to accept customer's any request date.</b>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

将所有三个元素定义为display: inline-block;vertical-align: middle;,如下所示:

.x > * {
  display: inline-block;
  vertical-align: middle;
}

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

.switchnmn input {
  display: none;
}

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

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

input:checked+.slidernmn {
  background-color: #2196F3;
}

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

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


/* Rounded sliders */

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

.slidernmn.round:before {
  border-radius: 50%;
}
<div class="x">
  <label for="y" class="switchnmn">
       <input type="checkbox" id="y" checked>
       <div class="slidernmn round"></div>
    </label>
  <b style="font-size:15px">I am able to accept customer's any request date.</b>
  </dlv>