将图像添加到自定义复选框

时间:2019-02-11 08:48:55

标签: html css checkbox

我已经制作了自定义复选框,这就是我所做的far,但是我需要的是这样的东西,其中还包含文本和一个图像(不是图标/ fa-fa图标)在复选框的中心。你知道如何实现吗?这是摘录中的代码:

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>

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您做的很好。您的代码看起来有点复杂,因为您可以将标签用作块元素(例如div或包装器)。退后一步,在复选框“内部”查看布局,并将其视为常规布局/样式元素。没有什么花哨。

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

答案 1 :(得分:0)

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