Brodey Sheppard
Brodey Sheppard

Reputation: 135

:checked css not working

I have a product listing little square thing, when I click it ( it's a label ) it should tick the box and it does but it should make the border green and hold it but it doesn't I have this:

.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;
}

}

and this works great it's .less my html looks like this:

<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>

How do I would I apply the suggestions in this checked: example?

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

It looks pretty simple but when I add it it doesn't work all I want is the .product border to be green instead of grey when it's selected

I know I can use js but I don't want to css should do it.

Upvotes: 4

Views: 27668

Answers (3)

Ben Winding
Ben Winding

Reputation: 11787

For anyone struggling

The input and the element that you're modifying need to be directly next to the each other, for the :checked rule to work.

#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>

Upvotes: 2

Michael Coker
Michael Coker

Reputation: 53684

+ is an adjacent sibling selector, meaning it matches the element after it based on the selector. Simply add a label tag after your <input type="checkbox"> tag.

.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>

Upvotes: 5

Andrei Fedorov
Andrei Fedorov

Reputation: 3977

to work "+" tags need to go one by one

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>

Upvotes: 3

Related Questions