html - 如何为我的复选框赋予背景颜色?
问题描述
我想为我的复选框赋予背景颜色。这是我的代码
input[type='checkbox'] {
width: 20px;
height: 20px;
background: white;
border-radius: 5px;
border: 2px solid red;
vertical-align: middle;
margin: 10px;
cursor: pointer;
}
<div class="card-header">
<input type="checkbox" class="customCheckBox" name="checkbox">
<span>data:
<span class="font-weight-bold">user</span>
</span>
</div>
但是,复选框没有获得背景颜色。我在哪里做错了?
解决方案
浏览器原生地为许多输入类型提供了一些样式。如果你想覆盖这些,你必须自己重做很多,但这是可行的(并且经常这样做)。您只需要通过以下方式阻止默认外观:
-moz-appearance: none;
-webkit-appearance: none;
现在您可以看到复选框可以有红色背景。但复选标记不再出现。
input[type='checkbox'] {
-moz-appearance: none;
-webkit-appearance: none;
width: 20px;
height: 20px;
background: red;
border-radius: 5px;
border: 2px solid red;
vertical-align: middle;
margin: 10px;
cursor: pointer;
}
<div class="card-header">
<input type="checkbox" class="customCheckBox" name="checkbox">
<span>data:
<span class="font-weight-bold">user</span>
</span>
</div>
要添加复选标记,我能想到的最简单的方法是在检查输入时添加一个伪元素:
input[type='checkbox'] {
-moz-appearance: none;
-webkit-appearance: none;
width: 20px;
height: 20px;
background: red;
border-radius: 5px;
border: 2px solid red;
vertical-align: middle;
margin: 10px;
cursor: pointer;
}
input[type='checkbox']:checked::after {
content: '✓';
display: inline-block;
text-align: center;
width: 100%;
}
<div class="card-header">
<input type="checkbox" class="customCheckBox" name="checkbox">
<span>data:
<span class="font-weight-bold">user</span>
</span>
</div>
推荐阅读
- algolia - InstantSearch.js v4.8.5 x search-insights 1.7.1 不发送点击事件
- microsoft-dynamics - 对 Dynamics 365 API 和访问采购订单的困惑
- python - Python - 如果仅在 'if __name__ == '__main__' 中调用它,它是否被视为私有方法?
- javascript - 如何修复“未找到模块:错误:无法解析 'mvc/form/form-repeat'”?
- python - Selenium 无法按名称找到表单元素
- python - Python程序为输入数字打印菱形图案?
- xcode - Xcode 12:分发内容页面仅显示 2 个选项
- python - 对齐菜单栏中的条目
- python - Python - 通过解包以太网帧获取 0xc0a8 期望 0x800 的类型不期望数据
- android - 当应用程序目标为(Android 11 / API 30)或更高版本时,如何将文件保存在android的根文件夹中?