【问题标题】:CSS: Checkbox with Border. How to get Space between Border and Content?CSS:带边​​框的复选框。如何获得边框和内容之间的空间?
【发布时间】:2016-06-16 22:07:04
【问题描述】:

我有一个复选框:

<div class="row margin-top-15">
    <div class="col-md-12">
        <div class="checkbox_square">
            <input id="check_password_show" type="checkbox" value=""></input>
            <label for="check_password_show"></label>
        </div>
        <p th:text="#{label.common_password_show}"></p>
    </div>
</div>

我创建了一个自定义复选框样式:

input[type=checkbox] {
    visibility: hidden;
}

.checkbox_square {
    position: relative;
}

.checkbox_square label {
    cursor: pointer;
    position: absolute;
    width: 20px;
    height: 20px;
    left: 0px;
    border-radius: 3px;
    border: 2px solid var(--textcolorsecundary);
}

.checkbox_square label:after {
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    opacity: 0;
    content: '';
    position: absolute;
    left: -2px;
    top: -2px;
    right: -2px;
    bottom: -2px;
    background: var(--diagramGreenColor);
    border-radius: 3px;
    border: 2px solid var(--primaryColor);
}

.checkbox_square label:hover::after {
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
    filter: alpha(opacity=30);
    opacity: 0.3;
}

.checkbox_square input[type=checkbox]:checked + label:after {
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    opacity: 1;
}

现在看起来像未选中:

然后选择:

选定的状态与我想要的有点不同。我希望它看起来像:

所以我需要的是绿色背景和蓝色边框之间的一些空间。这有可能吗?

我试图创建一个jsFiddle 来显示我的复选框,但正如您所见,该复选框在 jsFiddle 中根本没有出现。它在我的 IDE 和浏览器中运行良好。

【问题讨论】:

  • 你能创建一个工作的fiddle 吗?
  • 哦,我已经尝试过了,当我粘贴我的代码时,根本没有显示任何复选框:(
  • 我也无法使用您的代码完成这项工作。

标签: html css checkbox


【解决方案1】:

您可以调整.checkbox_square label:after的位置以达到想要的效果。检查以下示例。

input[type=checkbox] {
  visibility: hidden;
}
.checkbox_square {
  position: relative;
}
.checkbox_square label {
  cursor: pointer;
  position: absolute;
  width: 20px;
  height: 20px;
  left: 0px;
  border-radius: 3px;
  border: 2px solid grey;
}
.checkbox_square label:after {
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
  filter: alpha(opacity=0);
  opacity: 0;
  content: '';
  position: absolute;
  left: 2px;
  top: 2px;
  right: 2px;
  bottom: 2px;
  background: green;
  border-radius: 3px;
}
.checkbox_square label:hover::after {
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
  filter: alpha(opacity=30);
  opacity: 0.3;
}
.checkbox_square input[type=checkbox]:checked + label:after {
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
  filter: alpha(opacity=100);
  opacity: 1;
}
.checkbox_square input[type=checkbox]:checked + label {
  border-color: blue;
}
<div class="row margin-top-15">
  <div class="col-md-12">
    <div class="checkbox_square">
      <input id="check_password_show" type="checkbox" value="" />
      <label for="check_password_show"></label>
    </div>
    <p th:text="#{label.common_password_show}"></p>
  </div>
</div>

【讨论】:

  • 不幸的是,这不是我正在寻找的解决方案,因为这样我无法在单击复选框后将边框颜色更改为蓝色。
  • 检查更新的代码。您可以添加.checkbox_square input[type=checkbox]:checked + label 更改边框颜色。
猜你喜欢
  • 2022-01-10
  • 2020-01-31
  • 1970-01-01
  • 2023-03-26
  • 2013-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多