html - 对齐标签和自定义复选框

标签 html css

如何对齐标签和自定义复选框?代码片段如下。我想确保标签和开关垂直对齐,我还应该能够使用标签中的任何文本。我不能使用 position:absolute 方法来对齐标签,因为文本可能会更改并且我不希望有任何重叠。

.switch {
  position: relative;
  display: inline-block;
}

.switch input[type="checkbox"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  -o-appearance: none;
  appearance: none;
  cursor: pointer;
  outline: none;
  height: 25px;
  width: 50px;
  border-radius: 25px;
  background-color: #999;
}

.switch input[type="checkbox"]:checked {
  background-color: green;
}

.switch input[type="checkbox"]:after {
  position: absolute;
  content: ' ';
  height: 16.66667px;
  width: 16.66667px;
  top: 6.5px;
  right: 33px;
  border-radius: 11.11111px;
  background: #fff;
}

.switch input[type="checkbox"]:checked::after {
  right: 6px;
}
<div class="switch">
  <label for="checkbox1">Test Test Test Test</label>
  <input type="checkbox" id="checkbox1">
</div>

最佳答案

您应该能够在输入上使用垂直对齐属性(例如:vertical-align: middle;):

.switch {
  position: relative;
  display: inline-block;
}
.switch input[type="checkbox"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  -o-appearance: none;
  appearance: none;
  cursor: pointer;
  outline: none;
  height: 25px;
  width: 50px;
  border-radius: 25px;
  background-color: #999;
  vertical-align: middle;
}
.switch input[type="checkbox"]:checked {
  background-color: green;
}
.switch input[type="checkbox"]:after {
  position: absolute;
  content: ' ';
  height: 16.66667px;
  width: 16.66667px;
  top: 6.5px;
  right: 33px;
  border-radius: 11.11111px;
  background: #fff;
}
.switch input[type="checkbox"]:checked::after {
  right: 6px;
}
<div class="switch">
  <label for="checkbox1">Test Test Test Test</label>
  <input type="checkbox" id="checkbox1">
</div>

关于html - 对齐标签和自定义复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564673/

相关文章:

jquery - 使用 jQuery 对齐元素

javascript - 替换 ul 中 <li> 标记中的文本

eclipse - Eclipse 中是否有任何方法可以像 Netbeans 那样格式化包含 div 的代码?

css - 滚动条覆盖 Mountain Lion 上 WebKit 中的交互元素

c# - ASP.NET MVC - 样式表必须放在 Site.Master 文件中吗?

html - 动态添加的 DIV 上的 jquery Hide() 函数问题

html - 是否可以在文本区域中设置右下角拖动箭头的样式?

javascript - HTML 响应式 css 不适用于随机图像

javascript - 为什么当我选择日期(使用 HTML5 日期)时,Javascript 显示的日期与日期选择器中的日期不同?

css - IE 问题 : Div positioning