用 CSS accent-color 和 appearance 设置复选框样式
只需改变复选框颜色时,可以使用 accent-color。要自己绘制边框和勾选标记,请给实际的 <input type="checkbox"> 设置 appearance: none,再设置勾选与焦点状态的样式。两种做法都要保留含义明确的标签。
选择只改颜色还是自定义外观
input[type="checkbox"] {
accent-color: #176b86;
}
accent-color 会为受支持的原生控件设置强调色。浏览器和操作系统仍可决定部分外观细节。如果需要控制边框和勾选标记,可以使用 appearance。示例中的输入框保持可见,也能通过键盘聚焦。参见 CSS UI Level 4 · accent-color 和 CSS UI Level 4 · appearance。
运行原生与自定义复选框示例
将示例保存为 checkbox.html 后打开,无需额外图片或 JavaScript。第一个选项使用原生控件,第二和第三个选项使用 CSS 中的 SVG 绘制勾选标记。
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>可用键盘操作的复选框</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
form { max-width: 480px; }
.option { display: flex; align-items: center; gap: 10px;
min-height: 44px; cursor: pointer; }
.option input { flex: none; margin: 0; }
.native { width: 20px; height: 20px; accent-color: #176b86; }
.custom { appearance: none; width: 20px; height: 20px;
border: 2px solid #4d6578; border-radius: 4px;
background: white; cursor: pointer; }
.custom:checked { border-color: #176b86; background-color: #176b86;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8 6 11 13 4' fill='none' stroke='%23fff' stroke-width='2'/%3E%3C/svg%3E");
background-position: center; background-repeat: no-repeat; background-size: 16px; }
input:focus-visible { outline: 3px solid #b95700; outline-offset: 3px; }
.disabled { cursor: default; color: #647380; }
.custom:disabled { opacity: 0.55; cursor: not-allowed; }
button { margin-top: 12px; min-height: 44px; padding: 8px 16px; font: inherit; }
@media (forced-colors: active) {
.custom, .custom:checked {
appearance: auto;
background: revert;
border: revert;
}
.custom:disabled { opacity: 1; }
}
</style>
</head>
<body>
<h1>比较复选框样式</h1>
<form method="get">
<label class="option" for="news">
<input class="native" type="checkbox" id="news" name="news" value="yes">
<span>接收通知 · 原生样式</span>
</label>
<label class="option" for="terms">
<input class="custom" type="checkbox" id="terms" name="terms" value="yes" required>
<span>同意条款 · 自定义样式</span>
</label>
<label class="option disabled" for="locked">
<input class="custom" type="checkbox" id="locked" checked disabled>
<span>此选项不可更改</span>
</label>
<button type="submit">查看提交值</button>
</form>
</body>
</html>
点击前两个选项的标签可切换选中状态,也可按 Tab 移动焦点后按空格切换。第三项有 disabled,会保持选中,在普通 Tab 导航中会被跳过。
保留勾选、焦点和强制颜色下的状态
自定义控件使用 :checked 设置底色和勾选标记,用 :focus-visible 显示焦点轮廓。标签的 for 必须与输入框唯一的 id 一致。保留看得见的勾选形状,让选中状态无需只靠颜色区分。
在强制颜色模式下,无论是否勾选,示例都会恢复 appearance: auto 并重置自定义背景和边框,使系统能绘制勾选标记。将 #176b86 临时改成 #7546a3 试验颜色变化,检查标签和焦点正常后再还原。参见 CSS Color Adjustment · Forced colors。
检查必填验证和表单提交值
不勾选第二项就提交,可以看到 required 验证。只勾选第二项再提交,地址中会出现 terms=yes。也勾选第一项并重新提交后,会多出 news=yes。
表单会提交已勾选且启用的输入框的 name 和 value。未勾选或禁用的项目通常会被省略,服务器需要处理缺失值。示例使用 GET 重新打开当前文档,让地址显示提交值。参见 HTML Standard · Checkbox。




