html - 自定义单选按钮
问题描述
我正在尝试自定义一些单选按钮。我想自定义标签以使 is 作为一个可点击的按钮,如下图:
此外,我在悬停时引入了新标签按钮的更改颜色,并在聚焦时引入了另一种颜色,如下图所示:
我有两个主要问题:
1)如果我隐藏输入按钮(例如使用display:none
),我将无法选择我想要的单选按钮。
2 ) 我介绍了在使用属性检查单选按钮时更改颜色,focus-within
但这对我来说还不够满意。事实上,当我加载页面并且默认选中按钮时,我没有看到颜色有任何变化,因为我没有专注于它。
我使用的代码如下:
.form-control {
line-height: 15px;
font-size: 13px;
font-family: 'Oswald', sans-serif;
border: 0;
font-weight: 300;
display: block;
width: 100%;
padding: 0.375rem 0.75rem;
color: #495057;
background-color: #fff;
background-clip: padding-box;
transition: border-color 0.15s ease-in-out,box-shadow 0.15s ease-in-out;
list-style: none;
}
.inline-item {
display: inline-block;
}
.chip-container .chip-selection label {
height: 28px;
padding: 5px 10px 3px 10px;
border-radius: 14px;
background-color: white;
color: #7d7d7d;
border: #7d7d7d 1px solid;
cursor: pointer;
text-transform: none;
}
.chip-container .chip-selection label:hover {
color: $footer_color;
background-color: #D1F5FF;
border: #1b8cab 1px solid;
}
.chip-container .chip-selection label:focus-within {
color: white;
background-color: #1b8cab;
border: #1b8cab 1px solid;
}
<div class="border-short">
<ul class="form-control chip-container">
<li class="inline-item chip-selection"><label for="id_0">
<input type="radio" name="input_th" value="false" id="id_0" checked="">
old checkbox</label>
</li>
<li class="inline-item chip-selection"><label for="id_1">
<input type="radio" name="input_th" value="true" id="id_1">
new trial checkbox</label>
</li>
</ul>
</div>
主要问题是我无法更改 html 结构,并且输入是标签的子级。我最好不要使用任何javascript,我想知道是否有一些CSS技巧来解决我的问题
非常感谢谁能帮助我安德里亚
解决方案
添加opacity
&position
.chip-container .chip-selection label input{
position: absolute;
opacity: 0;
}
.form-control {
line-height: 15px;
font-size: 13px;
font-family: 'Oswald', sans-serif;
border: 0;
font-weight: 300;
display: block;
width: 100%;
padding: 0.375rem 0.75rem;
color: #495057;
background-color: #fff;
background-clip: padding-box;
transition: border-color 0.15s ease-in-out,box-shadow 0.15s ease-in-out;
list-style: none;
}
.inline-item {
display: inline-block;
}
.chip-container .chip-selection label {
height: 28px;
padding: 5px 10px 3px 10px;
border-radius: 14px;
background-color: white;
color: #7d7d7d;
border: #7d7d7d 1px solid;
cursor: pointer;
text-transform: none;
}
.chip-container .chip-selection label:hover {
color: $footer_color;
background-color: #D1F5FF;
border: #1b8cab 1px solid;
}
.chip-container .chip-selection label:focus-within {
color: white;
background-color: #1b8cab;
border: #1b8cab 1px solid;
}
.chip-container .chip-selection label input{
position: absolute;
opacity: 0;
}
<div class="border-short">
<ul class="form-control chip-container">
<li class="inline-item chip-selection"><label for="id_0">
<input type="radio" name="input_th" value="false" id="id_0" checked="">
old checkbox</label>
</li>
<li class="inline-item chip-selection"><label for="id_1">
<input type="radio" name="input_th" value="true" id="id_1">
new trial checkbox</label>
</li>
</ul>
</div>
推荐阅读
- python - 使用 type="button" 上传文件
- c++ - 使用 switch 但我想用字符串而不是 int 进行选择
- python - 将列表保存到 Django 中的模型
- lua - 领导者统计不工作?或者只是没有检测到点击?
- r - 对于不接受向量的函数,我该怎么办?错误:`x` 必须是长度为 1 的字符串
- excel - 自动过滤器不返回任何行:跳过后续步骤
- python - Kedro:如何从命令行传递“列表”参数?
- python - 无限循环中的电子邮件监控。有什么缺点/更好的想法吗?
- c# - 使用 C# 和 Selenium 在非结构化时如何获取元素及其值
- python - 从文本中获取书目列表及其计数 - Python