【问题标题】:CSS with DIV checkbox, the input is not aligned to center of DIV带 DIV 复选框的 CSS,输入未与 DIV 中心对齐
【发布时间】:2016-12-26 02:05:50
【问题描述】:

我有一个checkbox,用于表格和我的网络应用程序的其他部分。

此复选框位于 DIV 内,因此具有更好的外观和感觉。

HTML中的复选框是这样形成的:

<div class="checkbox checkbox-success checkbox-single">
   <input type="checkbox"><label></label>
</div>

我检测到,当我单击复选框的中心时,它可以工作,但如果我在左上角、右上角或左下角进行检查,则它不起作用。

然后我检查Google chrome 中的元素,当我选择input 元素时发现了这个。

这就是它不起作用的原因,因为输入元素没有以 div 为中心。

我正在使用 Bootstrap 3。

这是我在选择 input 元素时可以在 Google chrome 检查器中看到的 CSS。

这是CSS 我猜是从我的Style.css 应用的

.checkbox {
  padding-left: 20px;
}
.checkbox label {
  display: inline-block;
  padding-left: 5px;
  position: relative;
}
.checkbox label::before {
  -o-transition: 0.3s ease-in-out;
  -webkit-transition: 0.3s ease-in-out;
  background-color: #ffffff;
  border-radius: 3px;
  border: 1px solid #cccccc;
  content: "";
  display: inline-block;
  height: 17px;
  left: 0;
  margin-left: -20px;
  position: absolute;
  transition: 0.3s ease-in-out;
  width: 17px;
  outline: none !important;
}
.checkbox input[type="checkbox"] {
  cursor: pointer;
  opacity: 0;
  z-index: 1;
  outline: none !important;
}
.checkbox label::after {
  color: #555555;
  display: inline-block;
  font-size: 11px;
  height: 16px;
  left: 0;
  margin-left: -20px;
  padding-left: 3px;
  padding-top: 1px;
  position: absolute;
  top: 0;
  width: 16px;
}
.checkbox.checkbox-single label {
  height: 17px;
}
.checkbox-success input[type="checkbox"]:checked + label::before {
  background-color: #01ba9a;
  border-color: #01ba9a;
}
.checkbox-success input[type="checkbox"]:checked + label::after {
  color: #ffffff;
}

这是一个CodePen 示例,不显示相同的确切位置,但您会看到输入未居中对齐。

http://codepen.io/anon/pen/Jbqapo

这让我头疼,有什么线索吗?

【问题讨论】:

  • 你能用 fiddle 或 sn-p 解决你的问题吗?
  • 我只是添加了一个指向 CodePen 的链接,您可以在其中看到类似的行为。

标签: css checkbox twitter-bootstrap-3


【解决方案1】:

将以下div 添加到.checkbox input[type="checkbox"] 选择器以覆盖没有!importants 的引导程序,然后添加以下CSS 属性:

div.checkbox input[type="checkbox"] {
  /* add these */
  position: absolute;
  top: 0;
  left: 0;
  width: 16px;
  height: 16px;
  margin: 0;
}

以下片段:

.checkbox {
  padding-left: 20px;
}
.checkbox label {
  display: inline-block;
  padding-left: 5px;
  position: relative;
}
.checkbox label::before {
  -o-transition: 0.3s ease-in-out;
  -webkit-transition: 0.3s ease-in-out;
  background-color: #ffffff;
  border-radius: 3px;
  border: 1px solid #cccccc;
  content: "";
  display: inline-block;
  height: 17px;
  left: 0;
  margin-left: -20px;
  position: absolute;
  transition: 0.3s ease-in-out;
  width: 17px;
  outline: none !important;
}
div.checkbox input[type="checkbox"] {
  cursor: pointer;
  opacity: 0;
  z-index: 1;
  outline: none !important;

  /* add these */
  position: absolute;
  top: 0;
  left: 0;
  width: 16px;
  height: 16px;
  margin: 0;
}
.checkbox label::after {
  color: #555555;
  display: inline-block;
  font-size: 11px;
  height: 16px;
  left: 0;
  margin-left: -20px;
  padding-left: 3px;
  padding-top: 1px;
  position: absolute;
  top: 0;
  width: 16px;
}
.checkbox.checkbox-single label {
  height: 17px;
}
.checkbox-success input[type="checkbox"]:checked + label::before {
  background-color: #01ba9a;
  border-color: #01ba9a;
}
.checkbox-success input[type="checkbox"]:checked + label::after {
  color: #ffffff;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="checkbox checkbox-success checkbox-single">
  <input type="checkbox">
  <label></label>
</div>

这样它将覆盖所有包含div的空间。

【讨论】:

  • 不工作,但我添加了这个:位置:绝对!重要;然后效果很好。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 2021-10-20
  • 2019-05-25
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多