本文展示如何用纯 CSS 的方框和 SVG 对勾构建一个干净、跨浏览器的自定义复选框。
问题
浏览器默认复选框很难统一设置样式,尤其在移动端,它们可能渲染得比预期大得多。像 width: 14px 这样的常规尺寸约束对原生控件往往无效。
解决方案
用 appearance: none 去掉原生渲染,然后自己画方框和对勾。
实时示例
纯 CSS 自定义复选框
custom-checkbox.css
label {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
}
input[type='checkbox'] {
appearance: none;
-webkit-appearance: none;
width: 16px;
height: 16px;
min-width: 16px;
margin: 0;
border: 1.5px solid #b0c4d8;
border-radius: 4px;
background: #f8fafc;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.05);
cursor: pointer;
position: relative;
transition:
background 0.15s ease,
border-color 0.15s ease;
}
input[type='checkbox']:hover {
border-color: #00b88a;
}
input[type='checkbox']:checked {
background: #00b88a url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%23ffffff' stroke-linecap='round' stroke-linejoin='round' stroke-width='2.5' d='M3.5 8.5l3 3 6-7'/%3E%3C/svg%3E") center/60% no-repeat;
border-color: #00b88a;
box-shadow: none;
}index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom checkbox demo</title>
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #e8eef5;
padding: 2rem;
}
.demo {
display: flex;
flex-direction: column;
gap: 1rem;
max-width: 300px;
}
.demo label {
display: flex;
align-items: center;
gap: 0.6rem;
padding: 0.65rem;
background: rgba(255, 255, 255, 0.7);
border-radius: 8px;
cursor: pointer;
color: #1a2a3a;
transition: background 0.15s ease;
}
.demo label:hover {
background: rgba(120, 169, 255, 0.08);
}
.demo input[type='checkbox'] {
appearance: none;
-webkit-appearance: none;
width: 16px;
height: 16px;
min-width: 16px;
margin: 0;
border: 1.5px solid #b0c4d8;
border-radius: 4px;
background: #f8fafc;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.05);
cursor: pointer;
position: relative;
transition:
background 0.15s ease,
border-color 0.15s ease;
}
.demo input[type='checkbox']:hover {
border-color: #00b88a;
}
.demo input[type='checkbox']:checked {
background: #00b88a url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%23ffffff' stroke-linecap='round' stroke-linejoin='round' stroke-width='2.5' d='M3.5 8.5l3 3 6-7'/%3E%3C/svg%3E") center/60% no-repeat;
border-color: #00b88a;
box-shadow: none;
}
</style>
</head>
<body>
<div class="demo">
<label><input type="checkbox" checked><span>启用通知</span></label>
<label><input type="checkbox"><span>订阅新闻邮件</span></label>
<label><input type="checkbox" checked><span>接受条款</span></label>
</div>
</body>
</html>观感
- 未选中:16px 圆角方块,柔和蓝灰边框、浅色填充和淡淡的内阴影。看起来扁平但有质感。
- 悬停:边框以 0.15s 的快速过渡变为薄荷绿,反馈即时。
- 选中:方框填充薄荷绿,中间出现锐利的白色 SVG 对勾。内阴影消失,让选中态显得实。
SVG 对勾在高 DPI 屏上保持锐利,因为它是矢量图;整个控件在移动端保持小巧,因为 appearance: none 去掉了无视 width 和 height 的原生渲染。
要点
appearance: none和-webkit-appearance: none去掉原生控件。min-width防止方框在 flex 容器中被压扁。- 内联 SVG data URL 让对勾在任意尺寸都清晰,且不产生额外请求。
transition让悬停和选中状态更精致。