我们直接来看一下具体的实现代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *, ::after, ::before { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .checkbox-diy .checkboxsquare { width: 14px; height: 14px; border: 1px solid #999; cursor: pointer; display: inline-block; position: relative; } .checkbox-diy input:checked + span { border: 1px solid #008c8c; } .checkbox-diy input:checked ~ span { color: #008c8c; } .checkbox-diy input:checked + span.checkboxsquare::after { content: "✔"; display: block; width: 12px; height: 12px; cursor: pointer; position: absolute; top: -6px; } input[type="checkbox"] { display: none; } </style> </head> <body> 请选择多个: <label class="checkbox-diy"> <input type="checkbox" name="php" value="php"> <span class="checkboxsquare"></span> <span>php</span> </label> <label class="checkbox-diy"> <input type="checkbox" name="vue" value="vue"> <span class="checkboxsquare"></span> <span>vue</span> </label> <label class="checkbox-diy"> <input type="checkbox" name="python" value="python"> <span class="checkboxsquare"></span> <span>python</span> </label> </body> </html>
看下实现的效果截图
以上就是使用css样式制作单选框的详细内容。