:检查过css不工作

时间:2017-01-17 21:16:13

标签: html css less

我有一个产品列出小方块的东西,当我点击它(它是一个标签)它应该勾选方框它确实但它应该使边框绿色并保持它但它不是我有这个:

.product {
width: 100%;
background: #fff;
border: 4px solid #fff;
border-radius: 4px;
margin-bottom: 20px;
box-shadow: 0 2px 3px #ddd;
text-align:center;
padding-bottom: 15px;
cursor: pointer;

-webkit-transition: all .3s ease;
-moz-transition: all .3s ease;
-o-transition: all .3s ease;
-ms-transition: all .3s ease;
transition: all .3s ease;

a {
    color: #d6d6d6;
    line-height: 25px;
    border-radius: 100%;
    background: #f2f2f2;
    width: 25px;
    height: 25px;
    display: block;
    position: absolute;
    margin: 10px;

    -webkit-transition: all .3s ease;
    -moz-transition: all .3s ease;
    -o-transition: all .3s ease;
    -ms-transition: all .3s ease;
    transition: all .3s ease;

    &:hover {
        background: #e2e1e1;
        color: #333;
        text-decoration: none
    }
}

img {
    width: 80%;
    margin: 15px auto;
    pointer-events: none;
}

span {
    display: block;

    &.brand {
        color: #cdcfd2;
        font-size: 13px;
        font-weight: 300;
    }

    &.name {
        color: #232f3e;
        font-size: 16px;
        font-weight: 600;
    }
}

&:hover {
    border: 4px solid #d9dce1;
    box-shadow: none;
}

}

这非常有用。我的html看起来像这样:

<div class="col-md-2">
<label for="product_id" name="product_id" class="product">
    <a href="#" title="product">i</a>
    <div class="position">
        <input type="checkbox" class="checkbox" name="product_id" id="product_id">
    </div>
    <img src="assets/images/products/image.jpg">
    <span class="brand">Brand</span>
    <span class="name">Product</span>
</label>

我如何应用此checked:示例中的建议?

input[type=checkbox]:checked + label { }

它看起来非常简单,但是当我添加它时它并不起作用我想要的是.product边框在选择时是绿色而不是灰色

我知道我可以使用js,但我不想css应该这样做。

3 个答案:

答案 0 :(得分:2)

工作“+”标签需要逐一进行

的CSS: input[type=checkbox]:checked + label { }

HTML: <input type="checkbox" id="id"><label for="id">...</label>

input[type=checkbox]:checked + label {
  background-color: red;
}
<input type="checkbox" id="id">
<label for="id">Test</label>

答案 1 :(得分:2)

+是一个相邻的兄弟选择器,意味着它匹配基于选择器的元素。只需在label标记后添加<input type="checkbox">标记即可。

.product {
  width: 100%;
  background: #fff;
  border: 4px solid #fff;
  border-radius: 4px;
  margin-bottom: 20px;
  box-shadow: 0 2px 3px #ddd;
  text-align: center;
  padding-bottom: 15px;
  cursor: pointer;
  -webkit-transition: all .3s ease;
  -moz-transition: all .3s ease;
  -o-transition: all .3s ease;
  -ms-transition: all .3s ease;
  transition: all .3s ease;
}
.product a {
  color: #d6d6d6;
  line-height: 25px;
  border-radius: 100%;
  background: #f2f2f2;
  width: 25px;
  height: 25px;
  display: block;
  position: absolute;
  margin: 10px;
  -webkit-transition: all .3s ease;
  -moz-transition: all .3s ease;
  -o-transition: all .3s ease;
  -ms-transition: all .3s ease;
  transition: all .3s ease;
}
.product a:hover {
  background: #e2e1e1;
  color: #333;
  text-decoration: none;
}
.product img {
  width: 80%;
  margin: 15px auto;
  pointer-events: none;
}
.product span {
  display: block;
}
.product span.brand {
  color: #cdcfd2;
  font-size: 13px;
  font-weight: 300;
}
.product span.name {
  color: #232f3e;
  font-size: 16px;
  font-weight: 600;
}
.product:hover {
  border: 4px solid #d9dce1;
  box-shadow: none;
}
input[type=checkbox]:checked + label {
  color: red;
}
<div class="col-md-2">
<label for="product_id" name="product_id" class="product">
    <a href="#" title="product">i</a>
    <div class="position">
      <input type="checkbox" class="checkbox" name="product_id" id="product_id">
      <label for="product_id">label</label>
    </div>
    <img src="assets/images/products/image.jpg">
    <span class="brand">Brand</span>
    <span class="name">Product</span>
</label>

答案 2 :(得分:0)

致任何有困难的人

输入和您要修改的元素需要直接相邻,以便 :checked 规则起作用。

#hidden {
  display: none;
  height: 100px;
  background: red;
}
:checked + #hidden {
  display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<!-- NEED TO BE NEXT TO EACH OTHER ^ -->
<div id="hidden"></div>

<label for="my_checkbox">Show/hide</label>