单选框样式
时间: 2023-07-20 17:04:09 浏览: 96
以下是几种单选框样式:
1. 原生单选框样式:
```html
<input type="radio" name="radioGroup" id="radio1" value="option1">
<label for="radio1">Option 1</label>
<input type="radio" name="radioGroup" id="radio2" value="option2">
<label for="radio2">Option 2</label>
```
2. 自定义样式的单选框:
```html
<style>
.custom-radio {
display: inline-block;
position: relative;
padding-left: 28px;
margin-bottom: 12px;
cursor: pointer;
font-size: 16px;
user-select: none;
}
.custom-radio input {
position: absolute;
opacity: 0;
cursor: pointer;
}
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 20px;
width: 20px;
background-color: #eee;
border-radius: 50%;
}
.custom-radio:hover input ~ .checkmark {
background-color: #ccc;
}
.custom-radio input:checked ~ .checkmark {
background-color: #2196F3;
}
.checkmark:after {
content: "";
position: absolute;
display: none;
}
.custom-radio input:checked ~ .checkmark:after {
display: block;
}
.custom-radio .checkmark:after {
top: 6px;
left: 6px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
</style>
<label class="custom-radio">
<input type="radio" name="radioGroup" value="option1">
<span class="checkmark"></span>
Option 1
</label>
<label class="custom-radio">
<input type="radio" name="radioGroup" value="option2">
<span class="checkmark"></span>
Option 2
</label>
```
3. 使用 CSS 伪元素的单选框:
```html
<style>
.radio-container {
display: inline-block;
margin-right: 10px;
}
.radio-container input[type="radio"] {
display: none;
}
.radio-container label {
display: inline-block;
margin-right: 5px;
padding-left: 25px;
position: relative;
cursor: pointer;
font-size: 16px;
user-select: none;
}
.radio-container label:before {
content: "";
display: inline-block;
height: 16px;
width: 16px;
border-radius: 50%;
border: 2px solid #ccc;
position: absolute;
left: 0;
top: 2px;
}
.radio-container label:after {
content: "";
display: none;
height: 8px;
width: 8px;
border-radius: 50%;
background-color: #2196F3;
position: absolute;
left: 4px;
top: 6px;
}
.radio-container input[type="radio"]:checked + label:after {
display: block;
}
</style>
<div class="radio-container">
<input type="radio" name="radioGroup" id="radio1" value="option1">
<label for="radio1">Option 1</label>
</div>
<div class="radio-container">
<input type="radio" name="radioGroup" id="radio2" value="option2">
<label for="radio2">Option 2</label>
</div>
```
阅读全文