Pure-CSS复选框,标签不起作用

时间:2018-01-08 03:05:16

标签: html css

我在尝试检查相关复选框输入时是否显示检查。目前,除非我将输入字段移到 span 标记之外,否则这不起作用。

*由于使用 .NET ,HTML结构无法更改(至少这是我的理解)*



body {
  background-color: #1790b5;
  height: 100%;
  font-family: "Open Sans", sans-serif;
}

.checkBox {
  display: inline-block;
  width: 100%;
  height: 100%;
  vertical-align: middle;
  text-align: center;
}

.checkBoxLabel {
  display: inline-block;
  color: #fff;
  cursor: pointer;
  position: relative;
}
.checkBoxLabel span {
  display: inline-block;
  position: relative;
  background-color: transparent;
  width: 25px;
  height: 25px;
  transform-origin: center;
  border: 2px solid #bdc3c7;
  vertical-align: -6px;
  margin-right: 10px;
}
.checkBoxLabel span:before, .checkBoxLabel span:after {
  content: "";
  width: 0;
  background: #fff;
  position: absolute;
}

.inputCheckBox {
  display: none;
}
.inputCheckBox:checked + label span {
  background-color: #fff;
}
.inputCheckBox:checked + label span:after {
  content: "\2714";
  color: blue;
  top: -6px;
  font-size: 28px;
  left: 2px;
}

   <!-- .Net Code -->
  <div class="checkBox">
    <asp:CheckBox name="cb1" class="box" ID="cb1" runat="server"></asp:CheckBox>
    <asp:Label AssociatedControlId="cb1" class="checkBox-label" runat="server"></asp:Label>
  </div>

  <!-- renders: -->

  <div class="checkBox">
    <span class="box">
      <input type="checkbox" name="cb" id="cb1">
    </span>
    <label for="cb1" class="checkBox-label">Check</label>
  </div>
&#13;
&#13;
&#13;

我还没有尝试使用JS点击事件功能,但我认为它会更简单&#34;如果我用 JUST CSS做到这一点。

3 个答案:

答案 0 :(得分:0)

根据我们交易的评论,这不是一个CSS问题,而是一个Webforms问题。

首先解决CSS问题,考虑到webforms控件呈现HTML的方式,您可以通过CSS实现所需的功能,因为您可以在类似于父选择器的情况下做到这一点。存在。

现在解决您的问题。

内置的webforms控件可能很棒,但正如您所发现的那样,有时它们会带来不必要的标记和开销。有时您希望更多地控制呈现的HTML。这是将runat="server"添加到常规HTML元素可以提供很大帮助的地方。您可以获得大部分webform服务器端支持,并可以更好地控制最终的HTML输出。

在您的实例中,您需要以下内容:

<div class="checkBox">
  <input type="checkbox" name="cb1" class="box" ID="cb1" runat="server" >
  <label id="cb1Label" for="cb1" class="checkBox-label" runat="server"></label>
</div>

编辑CSS更改

由于我们巧妙地更改了HTML,因此我们需要更改您的SCSS。使用.box

删除嵌套
@mixin checkBox {
  position: absolute;
  top: 90px;
  left: 0;
  display: block;
  width: 24px;
  height: 24px;
}

body {
  background: #2c3e50;
}

input[type="checkbox"] {
  position: absolute;
  opacity: 1;
}

.checkBox-label {
  position: relative;
  display: block;
  padding: 0 0 0 2em;
  height: 1.5em;
  line-height: 1.5;
  cursor: pointer;
  &:before,
  &:after {
    @include checkBox;
    content: "";
    border: 2px solid #bdc3c7;
    background: #fff;
  }
  &:after {
    @include checkBox;
  }
}


/* Checkbox */

// when input checked. Add checkbox to lable after class
.checkBox>input[type="checkbox"]+.checkBox-label::after {
  content: "\2714";
  color: #000;
  line-height: 1.5;
  text-align: center;
  font-size: 1.3em;
}

// Onclick adds / removes check from checkboxes
.checkBox input[type="checkbox"] + .checkBox-label::after {
  -webkit-transform: scale(0);
  -ms-transform: scale(0);
  -o-transform: scale(0);
  transform: scale(0);
}

.checkBox input[type="checkbox"]:checked + .checkBox-label::after {
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  -o-transform: scale(1);
  transform: scale(1);
}

请参阅:https://codepen.io/anon/pen/zpRMdb ...您注意到仍有一些格式要做。

END编辑

现在服务器端,您可以像访问常规的webform控件一样访问它们:

//TO Set
cb1.Checked = True;
cb1Label.InnerText = "Some Label";
cb1Label.Attributes["for"] = cb1.ClientId;

//To Get
bool isChecked = cb1.Checked;

有关详细信息,请参阅:HtmlInputCheckbox和完整集:HtmlControls

答案 1 :(得分:0)

你可以这样试试。 选中checkbox的邻居作为span,并将span作为标签的父div。 对于标签,您可以使用psuedo类 :: before :: after 作为css背景和内容。

&#13;
&#13;
draggable(v-if="dragMode"...) <----------
.notDraggable(v-else)         <----------
   .dragable1
   .dragable2
   .dragable3-
&#13;
.checkbox {
    position: relative;
    display: block;
    margin-top: 10px;
    margin-bottom: 10px;
       padding-left: 20px;
}

.checkbox label {
    min-height: 20px;
    padding-left: 20px;
    margin-bottom: 0;
    font-weight: 400;
    cursor: pointer;
      display: inline-block;
    padding-left: 5px;
    position: relative;
}
.checkbox span label::before {
    -o-transition: .3s ease-in-out;
    -webkit-transition: .3s ease-in-out;
    background-color: #fff;
    border-radius: 1px;
    border: 1px solid rgba(120,130,140,.13);
    content: "";
    display: inline-block;
    height: 17px;
    left: 0;
    margin-left: -20px;
    position: absolute;
    transition: .3s ease-in-out;
    width: 17px;
    outline: 0!important;
}
.checkbox-primary input[type=checkbox]:checked+span label::before {
    background-color: #ab8ce4;
    border-color: #ab8ce4;
}
.checkbox label::after {
    color: #2b2b2b;
    display: inline-block;
    font-size: 11px;
    height: 16px;
    left: 0;
    margin-left: -19px;
    padding-left: 3px;
    padding-top: 4px;
    position: absolute;
    top: 0;
    width: 16px;
}
.checkbox input[type=checkbox]:checked+span label::after {
    content: "\f00c";
    font-family: FontAwesome;
}
.checkbox-primary input[type=checkbox]:checked+span label::after {
    color: #fff;
}
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

&#13;
&#13;
@mixin checkBox {
  position: absolute;
  top: 90px;
  left: 0;
  display: block;
  width: 24px;
  height: 24px;
}

body {
  background: #2c3e50;
}

input[type="checkbox"] {
  position: absolute;
  opacity: 1;
  left: 6px;
  top: 1px;
}

.checkBox-label {
  position: relative;
  display: block;
  padding: 0 0 0 2em;
  width: auto;
  height: 1.5em;
  line-height: 1.5;
  cursor: pointer;
  &:before,
  &:after {
    @include checkBox;
    content: "";
    border: 2px solid #bdc3c7;
    background: #fff;
  }
  &:after {
    @include checkBox;
  }
}


/* Checkbox */

// when input checked. Add checkbox to lable after class
.box>input[type="checkbox"]+.checkBox-label::after {
  content: "\2714";
  color: #000;
  line-height: 1.5;
  text-align: center;
  font-size: 1.3em;
}

// Onclick adds / removes check from checkboxes
.box input[type="checkbox"]+.checkBox .checkBox-label::after {
  -webkit-transform: scale(0);
  -ms-transform: scale(0);
  -o-transform: scale(0);
  transform: scale(0);
}

.box input[type="checkbox"]:checked+.checkBox .checkBox-label::after {
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  -o-transform: scale(1);
  transform: scale(1);
}
&#13;
 
//.Net Code
  <div class="checkBox">
<asp:CheckBox name="cb1" class="box" ID="cb1" runat="server"></asp:CheckBox>
<asp:Label AssociatedControlId="cb1" class="checkBox-label" runat="server"></asp:Label>
</div>
 //renders:
<div class="checkBox">
  <span class="box">
    <label class="checkBox-label"><input type="checkbox" name="cb">Check</label>
  </span>
  
</div>
&#13;
&#13;
&#13;

相关问题