将图像添加到自定义复选框
Posted
技术标签:
【中文标题】将图像添加到自定义复选框【英文标题】:Add image into custom checkbox 【发布时间】:2019-07-04 17:21:49 【问题描述】:我已经制作了自定义复选框,这是我所做的 far,但我需要的是这样的东西,其中还有文本和一个图像(不是图标/fa-fa 图标)复选框的中心。你知道如何实现吗?这是来自 sn-p 的代码:
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>
<div>
<input type="checkbox" id="cb2" />
<label for="cb2"><img src="http://lorempixel.com/102/102" /></label>
</div>
</div>
提前感谢您的帮助。
【问题讨论】:
Use images like checkboxes的可能重复 【参考方案1】:你自己做的很好。您的代码看起来有点复杂,因为您可以将标签用作块元素(如 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" >
<img src="https://placehold.it/40x40" >
<img src="https://placehold.it/60x60" >
<span>Text</span>
</label>
【讨论】:
感谢您的回复,但您提供的与我需要的有点不同。在这里我尝试添加更多图像:codepen.io/anon/pen/WPzYOv#anon-signup,我想在图像上方制作图像 嘿@Akza 我不是在这里为您提供完整的代码解决方案,只是提供有关如何制作它的见解;-) 仍然感谢您的回复。【参考方案2】:<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>
<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;
【讨论】:
以上是关于将图像添加到自定义复选框的主要内容,如果未能解决你的问题,请参考以下文章
IOS:将图像添加到自定义 MKAnnotationview