html - :checked css not working

标签 html css less

我有一个产品列表小方 block ,当我点击它时(它是一个标签)它应该勾选框并且它确实如此但它应该使边框变为绿色并按住它但它没有我有这个:

.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 来做。

最佳答案

+是一个相邻的兄弟选择器,这意味着它根据选择器匹配它之后的元素。只需添加 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>

关于html - :checked css not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41707141/

相关文章:

html - 如何在任何第一个 child 悬停时突出显示所有第一个 child 等等?

javascript - 扫描JS数组最快的方法是什么

html - 如何使用 Bootstrap 均匀间隔 3 个选择框的内联?

javascript - 使用 JS 设置 HTML 和 BODY CSS

html - 在移动设备上,如何在点击超链接时设置其样式?

jquery - 使用插件位于左侧的表情符号符号和左侧的表情符号框

html - 使用CSS在表格单元格中居中对齐的元素?

css - Reactjs 能否以编程方式处理 :before?

html - 如何在 LESS 中将多个类名传递给 mixin

css - 更少的 CSS 导入结构