user10454516
user10454516

Reputation: 1143

Add image into custom checkbox

I've made custom checkbox, here is what i've made so far, but what I need is something like this, where there is also text and one image (not icon/fa-fa icon) in the center of the checkbox. Did u know how to achieve it? Here is the code from snippet:

div {
  display: inline-block;
}

input[type="checkbox"][id^="cb"] {
  display: none;
}

label {
    display: block;
    position: relative;
    cursor: pointer;
    height: 120px;
    outline: 1px solid white;
    width: 120px;
}
label:before {
  background-color: red;
  color: white;
  display: block;
  position: absolute;
  width: 20px;
  height: 20px;
  top: 1px;
  left: 1px;
  text-align: center;
  line-height: 20px;
  transition-duration: 0.4s;
  -webkit-transition-duration: 0.4s;
  -moz-transition-duration: 0.4s;
  -ms-transition-duration: 0.4s;
}
label img {
  height: 100%;
  width: 100%;
}
:checked + label {
  border-color: red;
}
:checked + label:before {
  content: "✓";
  background-color: red;
  outline: 1px solid red;
  z-index: 99;
}
:checked + label img {
    z-index: -1;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    height: 95%;
    width: 95%;
    outline: 3px solid red;
}
<div>
  <div>
    <input type="checkbox" id="cb1" />
    <label for="cb1"><img src="http://lorempixel.com/103/103" /></label>
  </div>&nbsp;
  <div>
    <input type="checkbox" id="cb2" />
    <label for="cb2"><img src="http://lorempixel.com/102/102" /></label>
  </div>
</div>

Thanks in advance for your help.

Upvotes: 1

Views: 426

Answers (2)

user10454516
user10454516

Reputation: 1143

<div>
  <div><input type="checkbox" id="cb1" />
    <label for="cb1">
    <img src="http://lorempixel.com/103/103" />
    <img src="https://dummyimage.com/120/000000/ffffff" />
    <span>Text</span>
    </label>
  </div>&nbsp;
  <div><input type="checkbox" id="cb2" />
    <label for="cb2">
      <img src="http://lorempixel.com/102/102" />
      <img src="https://dummyimage.com/120/000000/ffffff" />
    <span>Text</span>
    </label>
  </div>
</div>

div {
  display: inline-block;
}

input[type="checkbox"][id^="cb"] {
  display: none;
}

label {
  display: block;
  position: relative;
  cursor: pointer;
  height: 120px;
  outline: 1px solid white;
  width: 120px;
}

label:before {
  background-color: red;
  color: white;
  display: block;
  position: absolute;
  width: 20px;
  height: 20px;
  top: 1px;
  left: 1px;
  text-align: center;
  line-height: 20px;
  transition-duration: 0.4s;
}

label img {
  height: 100%;
  width: 100%;
  z-index: 1;
}

label img + img {
  height: 60%;
  width: 60%;
  z-index: 2;
  position: absolute;
  top: 10%;
  left: 20%;
}

span {
  z-index: 2;
  color: red;
  position: absolute;
  top: 75%;
  left: 40%;
  text-align: center;
}

:checked + label {
  border-color: red;
}

:checked + label:before {
  content: "✓";
  background-color: red;
  outline: 1px solid red;
  z-index: 3;
}


:checked + label img + img {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 60%;
  width: 60%;
  outline: 0px solid transparent;
}

:checked + label img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 98%;
  width: 98%;
  outline: 3px solid red;
}

Upvotes: 0

Arno Tenkink
Arno Tenkink

Reputation: 1528

You're doing just fine at your own. Your code looks a bit complex because you can use the label as a block-element (like a div or a wrapper). Take a step back, look at your layout 'inside' the checkbox and see it as a normal layout/styling element. Nothing fancy.

.styled {
  /* could also be a background */
  background-color: red;
  padding: 1rem 4rem;
  text-align: center;
  cursor: pointer;
}

span {
  display: block;
  font-size: 16px;
  font-family: Arial;
  margin-top: 10px;
}

label {
  display: block;
  max-width: 100px;
  margin: 0 auto;
}

#checkbox {
  display: none;
}

input:checked+label {
  background-color: green;
}
<input id="checkbox" type="checkbox">
<label class="styled" for="checkbox">
  <img src="https://placehold.it/20x20" alt="image 1">
  <img src="https://placehold.it/40x40" alt="image 2">
  <img src="https://placehold.it/60x60" alt="image 3">
  <span>Text</span>
</label>

Upvotes: 2

Related Questions