html - 在 SCSS 语法中自定义单选按钮
问题描述
我自定义了我的单选按钮,但我想使用 SCSS 使我的代码更简洁。所以我将我的代码从 CSS 更改为 SCSS 但出现了一个问题:
HTML
<input type="radio" name="test" value="value">
<label>
<span></span>
Test radio
</label>
CSS
input[type="radio"] {
display: none;
}
input[type="radio"] + label span {
display: inline-block;
width: 24px;
height: 24px;
margin-right: 6px;
vertical-align: middle;
background: url(./radio-sprite.svg) -46px 0 no-repeat;
background-size: 92px 24px;
cursor: pointer;
}
input[type="radio"]:checked + label span {
background: url(./radio-sprite.svg) -69px 0px no-repeat;
background-size: 92px 24px;
}
input[type="radio"]:disabled + label span {
background: url(./radio-sprite.svg) -22px 0px no-repeat;
background-size: 92px 24px;
}
SCSS
input[type="radio"] {
display: none;
& > label {
cursor: pointer;
span {
display: inline-block;
width: 24px;
height: 24px;
margin-right: $margin-small;
vertical-align: middle;
background: url(./app/shared/assets/icons/radiobtn-sprite.svg) -46px 0 no-repeat;
background-size: 92px 24px;
cursor: pointer;
}
}
&:checked + label span {
background: url(./app/shared/assets/icons/radiobtn-sprite.svg) -69px 0px no-repeat;
background-size: 92px 24px;
}
&:disabled + label span {
background: url(./app/shared/assets/icons/radiobtn-sprite.svg) -22px 0px no-repeat;
background-size: 92px 24px;
}
}
当我用上面的 SCSS 代码替换我的 CSS 时,我的按钮消失了。我看不出似乎是什么问题。任何帮助,将不胜感激
解决方案
您必须使用+
选择器:
& + label {
cursor: pointer;
span {
display: inline-block;
width: 24px;
height: 24px;
margin-right: $margin-small;
vertical-align: middle;
background: url(./app/shared/assets/icons/radiobtn-sprite.svg) -46px 0 no-repeat;
background-size: 92px 24px;
cursor: pointer;
}
}
推荐阅读
- sql - BigQuery 中按类别划分的首批用户
- git - 我将一个分支与一个过时的本地主分支合并,然后推送。我该如何撤消?
- python - “需要一个数组。提供了类型‘object’。” 如何在我的 Dash 应用程序的 DataTable 函数中解决此错误?
- video - 在该时间点显示帧的视频进度条上悬停的通用术语?
- mysql - MySQL 和两列 (1 -> n)
- javascript - 如何处理 docker-compose 中的 Exec 格式错误
- wordpress - Amazon SES IP 在 DMARC 中的 SPF 检查失败
- python - 在为扫描的 PDF 创建文本层时编辑文本而不弄乱页面外观?
- javascript - 单击单元格内的图标时,Ag-Grid 防止 onRowClicked 事件
- python - Python 获取请求只是返回 []