惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
U
Unit 42
F
Fortinet All Blogs
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
Y
Y Combinator Blog
罗磊的独立博客
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
I
InfoQ
博客园 - 叶小钗
博客园 - 聂微东
Last Week in AI
Last Week in AI

博客园 - smil、梵音

css渐变边框的实现 C primer Plus 第六版第4章节复习题5记录 C语言第4章末尾程序找错记录 css 的z-index的层级问题 由webai项目的 AI工作台 项目有感记录 C语言标准(C89/C90)中的32个关键字全列表 Windows 系统的 vscode 通过 Remote-SSH 连接虚拟机的 Ubuntu 系统运行 C 语言的代码 Windows 系统 vscode Remote-SSH 远程连接虚拟机里面的Ubuntu系统的失败处理 Ubuntu虚拟机里安装了SSH,开启SSH服务的时候报错 VS Code的Remote - SSH功能 Ubuntu系统里面安装 g++ ,sudo apt update 的含义 C语言是不是必须得通过gcc编译成可执行的程序? c语言用gcc编译过后,执行 ./hello.c 报错 ./hello.c: 权限不够 windows系统的虚拟机里面的Ubuntu系统安装VMware Tools Ubuntu的应用中心搜索 vscode 的时候,筛选条件为Snap包、debian包是什么意思,如何安装vscode Ubuntu系统是否就是Linux系统,安装软件和Windows系统有何区别 Ubuntu系统里面vscode运行C语言的第一个 Hello World 程序 vscode 中安装C/C++相关的插件 Ubuntu 安装一个轻量级的中文输入法Fcitx5 Ubuntu安装百度网盘 Ubuntu系统里面安装vscode 乌版图系统截屏快捷键 Ubuntu 系统里面运行C++ TortoiseGit 的安装与汉化 安装虚拟机+ ubuntu24.04 系统 联想小新安装 ubuntu24.04 双系统 | 安装乌版图系统 | windows安装 ubuntu24.04 双系统 利用faststone capture制作gif动图 Sublime Text 常用编辑快捷键速查表 vscode 中的 css 样式代码不显示折叠图标的解决方法 IDEA中git的Cherry-Pick的可视化使用(个人总结) IDEA中git的Cherry-Pick的使用
如何改变type="checkbox"的input复选框的样式
smil、梵音 · 2025-03-26 · via 博客园 - smil、梵音

<input type="checkbox">默认的样式是由浏览器控制的,直接修改它的样式比较困难。可以通过一些CSS技巧来实现自定义样式,包括改变颜色。

直接上例子吧:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Custom Checkbox</title>
  <style>
    /* 隐藏原生的checkbox */
    .custom-checkbox input[type="checkbox"] {
      opacity: 0;
      position: absolute;
    }

    /* 自定义样式 */
    .custom-checkbox label {
      display: inline-block;
      width: 20px;
      height: 20px;
      border: 2px solid red;
      border-radius: 4px;
      position: relative;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    /* 当checkbox被选中时的样式 */
    .custom-checkbox input[type="checkbox"]:checked + label {
      background-color: blue; /* 改变选中时的背景颜色 */
    }

    /* 添加一个小勾勾 */
    .custom-checkbox input[type="checkbox"]:checked + label::after {
      content: "";
      position: absolute;
      top: 4px;
      left: 6px;
      width: 8px;
      height: 4px;
      border: 2px solid white;
      border-top: none;
      border-right: none;
      transform: rotate(-45deg);
    }
  </style>

  <style>
    /* 自定义样式 */
    .custom-checkbox input[type="checkbox"] {
      width: 20px;
      height: 20px;
      border: 2px solid #000;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s ease, border-color 0.3s ease;
    }

    /* 当checkbox被选中时的样式 */
    .custom-checkbox input[type="checkbox"]:checked {
      background-color: blue; /* 改变选中时的背景颜色 */
      border-color: blue; /* 改变选中时的边框颜色 */
    }
  </style>
</head>
<body>
  <div class="custom-checkbox">
    <input type="checkbox" id="checkbox1">
    <label for="checkbox1"></label>
  </div>

  <br/>
  <h1>----------------------</h1>
  <div class="custom-checkbox">
    <input type="checkbox" id="checkbox3">
    <label for="checkbox3"></label>
  </div>
</body>
</html>