为什么我的其他复选框面板在这个奇怪的自定义复选框问题中丢失了?

时间:2018-03-29 02:36:48

标签: html css

小提琴是here

本质上应该是这样的。

enter image description here

但在我的自定义复选框中,正如您在小提琴中看到的那样,它会显示如下。

enter image description here

请注意,在标签

之前,checkgroup中有两个输入

HTML

<div class=" text-left"><span class="font-weight-bold">Include</span> <span class="font-weight-bold">Exclude</span>
  <div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="Chicken"> <input type="checkbox" class="exclude"> <label>
                Chicken
                </label></div>
    </div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="Turkey"> <input type="checkbox" class="exclude"> <label>
                Turkey
                </label></div>
    </div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="Beef"> <input type="checkbox" class="exclude"> <label>
                Beef
                </label></div>
    </div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="Pork"> <input type="checkbox" class="exclude"> <label>
                Pork
                </label></div>
    </div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="Fish"> <input type="checkbox" class="exclude"> <label>
                Fish
                </label></div>
    </div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="No Meat - Vegetarian Only"> <input type="checkbox" class="exclude"> <label>
                No Meat - Vegetarian Only
                </label></div>
    </div>
    <div>
      <div class="checkGroup"><input type="checkbox" class="include" value="No Meat - Vegan Only"> <input type="checkbox" class="exclude"> <label>
                No Meat - Vegan Only
                </label></div>
    </div>
  </div>
</div>

CSS

.checkGroup {
  display: inline;
  .exclude {
    margin-left: 2em;
  }
  .include {
    margin-left: 2em;
  }
  label {
    margin-left: 2em;
  }
  /* Base for label styling */
  [type="checkbox"]:not(:checked),
  [type="checkbox"]:checked {
    position: absolute;
    left: -9999px;
  }
  [type="checkbox"]:not(:checked)+label,
  [type="checkbox"]:checked+label {
    position: relative;
    padding-left: 1.95em;
    cursor: pointer;
  }
  /* checkbox aspect */
  [type="checkbox"]:not(:checked)+label:before,
  [type="checkbox"]:checked+label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 1.25em;
    height: 1.25em;
    border: 2px solid #ccc;
    background: #fff;
    border-radius: 4px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
  }
  /* checked mark aspect */
  [type="checkbox"]:not(:checked)+label:after,
  [type="checkbox"]:checked+label:after {
    content: '✔';
    position: absolute;
    top: .2em;
    left: .275em;
    font-size: 1.4em;
    line-height: 0.8;
    color: #09ad7e;
    transition: all .2s;
    font-family: Helvetica, Arial, sans-serif;
  }
  /* checked mark aspect changes */
  [type="checkbox"]:not(:checked)+label:after {
    opacity: 0;
    transform: scale(0);
  }
  [type="checkbox"]:checked+label:after {
    opacity: 1;
    transform: scale(1);
  }
  /* disabled checkbox */
  [type="checkbox"]:disabled:not(:checked)+label:before,
  [type="checkbox"]:disabled:checked+label:before {
    box-shadow: none;
    border-color: #bbb;
    background-color: #ddd;
  }
  [type="checkbox"]:disabled:checked+label:after {
    color: #999;
  }
  [type="checkbox"]:disabled+label {
    color: #aaa;
  }
}

2 个答案:

答案 0 :(得分:2)

以下是原则:

&#13;
&#13;
.checkGroup {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.checkGroup [type=checkbox] {
  visibility: hidden;
  position: absolute;
}

cbox-image {
  position: relative;
  height: 1em;
  width: 1em;
  margin: 0 .35em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #eee;
  border-radius: 5px;
}

.checkGroup input:checked + cbox-image:after,
.checkGroup input + cbox-image + cbox-image:after { /* checked state */
  content: '✔';
  position: absolute;
  font-size: 1.4em;
  line-height: 0.8;
  color: #09ad7e;
  font-family: Helvetica, Arial, sans-serif;
}

.checkGroup input:checked + cbox-image + cbox-image:after,
.checkGroup input:not(:checked) + cbox-image:after { /* unchecked state */  
  content: none;
}
&#13;
<label class="checkGroup">
   <input type="checkbox" value="Turkey">
   <cbox-image></cbox-image>
   <cbox-image></cbox-image> Turkey
 </label>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在您的评论和聊天记录中,点击<label>不应触发任何复选框点击。

因此,原来的CSS基本没用,因为:

  1. 2个复选框只有1个标签。
  2. 所有CSS都为1标签设置样式,就像有2个标签一样。
  3. 例如,这一行:

    [type="checkbox"]:not(:checked)+label,
    [type="checkbox"]:checked+label {
        ...
    }
    

    什么都没有,因为label中的一个[type="checkbox"]没有足够[type="checkbox"]:not(:checked), [type="checkbox"]:checked {...} 可用,除了它们相互覆盖的事实。

    此外,您的CSS代码包含许多类似于:

    的自我重写CSS
    [type="checkbox"] {...}
    

    两个相反的状态使用相同的样式。它可以简化为

    label

    建议的解决方案

    您可以将大多数样式从[type="checkbox"]转移到[type="checkbox"]:checked::after<a>

    选中fiddle

    不完全是你要求的,但我确定你可以自己调整它。