内联块的垂直居中

时间:2019-04-09 14:02:05

标签: html css sass

我正在尝试使用块创建自定义复选框

.checkbox-container {
  height: 18px;
  line-height: 18px;
  margin: 0;
  padding: 0;
  display: inline-block;
  cursor: pointer;
  font-size: 14px;
  position: relative;
  vertical-align: middle;
}

.label {
  margin-left: 4px;
}

.input-box {
  display: inline-block;
  height: 18px;
  width: 18px;
}

.off {
  background-color: #fff;
  border: 2px solid red;
}

.on {
  background-color: red;
  border: 2px solid transparent;
}

.on::after {
  content: '';
  position: absolute;
  left: 6px;
  top: 2px;
  width: 7px;
  height: 11px;
  border: solid #fff;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
<div class="checkbox-container">
  <span class="input-box on"></span>
  <span class="label">Checkbox</span>
</div>

如何在此块中垂直居中放置文本?

1 个答案:

答案 0 :(得分:-1)

只需在您的vertical-align: super;类中添加.label道具。

.label {
  margin-left: 4px;
  vertical-align: super;
}

.checkbox-container {
  height: 18px;
  line-height: 18px;
  margin: 0;
  padding: 0;
  display: inline-block;
  cursor: pointer;
  font-size: 14px;
  position: relative;
  vertical-align: middle;
}

.label {
  margin-left: 4px;
  vertical-align: super;
}

.input-box {
  display: inline-block;
  height: 18px;
  width: 18px;
}

.off {
  background-color: #fff;
  border: 2px solid red;
}

.on {
  background-color: red;
  border: 2px solid transparent;
}

.on::after {
  content: '';
  position: absolute;
  left: 6px;
  top: 2px;
  width: 7px;
  height: 11px;
  border: solid #fff;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
<div class="checkbox-container">
  <span class="input-box on"></span>
  <span class="label">Checkbox</span>
</div>