<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3自定义复选框和单选框</title>
<style>
/* 基础样式 */
body {
font-family: 'Arial', sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
color: #333;
}
h1 {
color: #2c3e50;
text-align: center;
margin-bottom: 30px;
}
.container {
display: flex;
flex-wrap: wrap;
gap: 30px;
justify-content: center;
}
.section {
background: #f9f9f9;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
width: 300px;
}
h2 {
color: #3498db;
margin-top: 0;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
/* 自定义复选框样式 */
.checkbox-container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 15px;
cursor: pointer;
font-size: 16px;
user-select: none;
}
/* 隐藏默认复选框 */
.checkbox-container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* 自定义复选框 */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 22px;
width: 22px;
background-color: #eee;
border-radius: 4px;
transition: all 0.3s;
}
/* 悬停效果 */
.checkbox-container:hover input ~ .checkmark {
background-color: #ccc;
}
/* 选中状态 */
.checkbox-container input:checked ~ .checkmark {
background-color: #3498db;
}
/* 创建勾选标记 */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* 显示勾选标记 */
.checkbox-container input:checked ~ .checkmark:after {
display: block;
}
/* 勾选标记样式 */
.checkbox-container .checkmark:after {
left: 8px;
top: 4px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
/* 自定义单选框样式 */
.radio-container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 15px;
cursor: pointer;
font-size: 16px;
user-select: none;
}
/* 隐藏默认单选框 */
.radio-container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* 自定义单选框 */
.radio-btn {
position: absolute;
top: 0;
left: 0;
height: 22px;
width: 22px;
background-color: #eee;
border-radius: 50%;
transition: all 0.3s;
}
/* 悬停效果 */
.radio-container:hover input ~ .radio-btn {
background-color: #ccc;
}
/* 选中状态 */
.radio-container input:checked ~ .radio-btn {
background-color: #3498db;
}
/* 创建内圆 */
.radio-btn:after {
content: "";
position: absolute;
display: none;
}
/* 显示内圆 */
.radio-container input:checked ~ .radio-btn:after {
display: block;
}
/* 内圆样式 */
.radio-container .radio-btn:after {
top: 7px;
left: 7px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
/* 禁用状态 */
.checkbox-container input:disabled ~ .checkmark,
.radio-container input:disabled ~ .radio-btn {
background-color: #e0e0e0;
cursor: not-allowed;
}
/* 链接样式 */
.footer {
text-align: center;
margin-top: 40px;
padding: 20px;
font-size: 14px;
}
.footer a {
color: #3498db;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>CSS3自定义复选框和单选框</h1>
<div class="container">
<div class="section">
<h2>自定义复选框</h2>
<label class="checkbox-container">
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
默认选中
</label>
<label class="checkbox-container">
<input type="checkbox">
<span class="checkmark"></span>
未选中
</label>
<label class="checkbox-container">
<input type="checkbox" disabled>
<span class="checkmark"></span>
禁用状态
</label>
</div>
<div class="section">
<h2>自定义单选框</h2>
<label class="radio-container">
<input type="radio" name="radio" checked="checked">
<span class="radio-btn"></span>
选项一
</label>
<label class="radio-container">
<input type="radio" name="radio">
<span class="radio-btn"></span>
选项二
</label>
<label class="radio-container">
<input type="radio" name="radio" disabled>
<span class="radio-btn"></span>
禁用选项
</label>
</div>
</div>
<div class="footer">
</div>
</body>
</html>
CSS3自定义复选框和单选框
于 2025-06-05 16:18:13 首次发布