css - 自定义复选框按钮问题
问题描述
我正在尝试制作一个自定义复选框按钮,单击该按钮将更改其颜色作为它们处于活动状态的指示器。我站起来几乎完成了,但复选框出现在框的左侧,而不是按钮本身。我不知道该怎么做。
input[type=checkbox] {
width: auto;
height: 20px;
cursor: pointer;
position: relative;
opacity: 0;
z-index: 9999;
}
input[type=checkbox] + label {
font-family: "Aharoni";
display: inline-block;
background: #dcdcdc;
color: black;
font-size: 12px;
font-weight: 400;
text-align: center;
border: solid 1px #a8a8a8;
border-bottom-color: black;
border-radius: 0px;
width: auto;
height: 20px;
padding-left:5px;
padding-right:5px;
cursor: pointer;
position: relative;
}
input[type=checkbox]:checked + label,
input[type=checkbox]:active + label {
background:gray;
color:white;
}
<input type="checkbox" name="test" /><label>Text goes here</label></input>
解决方案
你可以这样做,它会解决你的问题:
for
在标签中添加一个属性并id
在类型中设置一个属性,input
这将绑定两个元素,您可以获得所需的结果。
<input id="my_checkbox" type="checkbox" name="test" />
<label for="my_checkbox">
Text goes here
</label>
</input>
在这里,我id="my_checkbox"
在复选框输入中添加了一个并添加到for="my_checkbox"
attr.xml 中。
推荐阅读
- ios - 在设备上调试 iOS 应用程序时,Xcode 无法正确签署框架
- sql-server - SQL、T-SQL 查找
- php - 有没有办法将 CORS 标头设置为 AWS 签名的 URL?
- python - 如何在python中按时间聚合值
- angular - Angular 2/4 Onselect 没有得到
- c++ - []{return i == 1;} 是什么意思,它在 std 等待和条件变量中如何工作?
- linux - 结束内核恐慌 - 不同步:试图杀死 init!退出代码 = 0x00007f00
- javascript - 如何将我的物品分成两组?
- shopify - 如何在shopify的注册表单中添加新字段?
- android - 通知的警报管理器无法正常工作