css - 减少复选框/单选按钮组的重复 CSS 代码
问题描述
我有以下 CSS 样式复选框和单选按钮组,我喜欢通过组合设置的样式来缩短它 for.blg-buttons input[type=radio]
和 for .blg-buttons input[type=checkbox]
。目前,我为这些单独编写样式(即使两者的样式相同)并且不确定将它们组合的最佳方式。
通过在 CSS 中也使用嵌套的 span、span:hover 和 :not(:checked) 使代码变得更长一些。
/* App-Wide Radio Button Group */
/* Padding on the Span - Margin on the Label */
/* ========================================= */
.blg-buttons {
display: flex;
justify-content: center;
flex-wrap: wrap;
flex-direction: row;
flex: 1;
}
.blg-buttons input[type=radio] {
visibility:hidden;
width:0px;
height:0px;
overflow:hidden;
}
.blg-buttons label {
display: flex;
line-height: 0;
font-size: 0.85vw;
flex: 1;
align-items: center;
margin: 0.2vw;
font-weight: 700;
}
.blg-buttons input[type=radio]+span {
cursor: pointer;
display: flex;
flex: 1;
justify-content: center;
vertical-align: top;
line-height: 1;
font-size: 1vw;
padding: .5vw;
border-radius: .35vw;
border: .15vw solid #333;
/*width: 90%;*/
/* text-align: center; <-- you don't need this with flex */
color: #333;
background: #eee;
text-align: center;
}
/* button colors */
.blg-buttons input[type=radio]:not(:checked) + span {
cursor: pointer;
background-color: #EEE;
color: #333;
}
/* button colors */
.blg-buttons input[type=radio]:not(:checked) + span:hover{
cursor: pointer;
background: #888;
}
/* button colors */
.blg-buttons input[type=radio]:checked + span{
cursor: pointer;
background-color: #333;
color: #EEE;
}
.blg-buttons input[type=checkbox] {
visibility:hidden;
width:0px;
height:0px;
overflow:hidden;
}
.blg-buttons input[type=checkbox]+span {
cursor: pointer;
display: flex;
flex: 1;
justify-content: center;
vertical-align: top;
line-height: 1;
font-size: 1vw;
padding: .5vw;
border-radius: .35vw;
border: .15vw solid #333;
/*width: 90%;*/
/* text-align: center; <-- you don't need this with flex */
color: #333;
background: #eee;
text-align: center;
}
/* button colors */
.blg-buttons input[type=checkbox]:not(:checked) + span {
cursor: pointer;
background-color: #EEE;
color: #333;
}
/* button colors */
.blg-buttons input[type=checkbox]:not(:checked) + span:hover{
cursor: pointer;
background: #888;
}
/* button colors */
.blg-buttons input[type=checkbox]:checked + span{
cursor: pointer;
background-color: #333;
color: #EEE;
}
/* =========== */
即我可以将.blg-buttons input[type=radio]:checked + span
and组合.blg-buttons input[type=checkbox]:checked + span
成一个组.blg-buttons input[type=checkbox & radio]:checked + span
,或者类似的东西吗?
我也可以嵌套这样的东西吗?:
.blg-buttons {
label { }
...
}
非常感谢任何有关如何缩短此 CSS 的帮助,谢谢!
解决方案
你可以做以下事情
CSS
input[type=radio], input[type=checkbox]
{
//here should be your css
}
SCSS/LESS
.blg-buttons {
input[type=radio] {}
input[type=checkbox] {}
}
推荐阅读
- javascript - 令牌过期时如何设置新令牌?
- c# - 调试时解决方案没有错误
- synology - 如何在 Synology NAS 中导出用户列表和密码
- wpf - 建立连接时操作被中止
- spring-aop - Spring Framework中的加载时间编织未正确记录
- php - 无需重新加载 AJAX/PHP 即可更新页面
- python-3.x - 将 dtype float64 的图形转换为 float32
- encryption - 如何在 groovy 中缩短 URL?
- android - 如何以编程方式确定 Android 应用程序是否在三星 Galaxy 上进入睡眠状态?
- python - 测试的 sql 脚本上的 SQLite3 语法错误