Computer >> Máy Tính >  >> Lập trình >> CSS

Làm cách nào để tạo hộp kiểm tùy chỉnh và nút radio bằng CSS?

Sau đây là mã để tạo menu điều hướng đáp ứng với biểu mẫu đăng nhập bên trong -

Ví dụ

<!DOCTYPE html>
<html>
<style>
.checkboxContainer {
   display: block;
   position: relative;
   padding-left: 35px;
   margin-bottom: 12px;
   cursor: pointer;
   font-size: 22px;
   -webkit-user-select: none;
   -moz-user-select: none;
   -ms-user-select: none;
   user-select: none;
}
.checkboxContainer input {
   position: absolute;
   opacity: 0;
   cursor: pointer;
   height: 0;
   width: 0;
}
.checkboxMarked {
   position: absolute;
   top: 0;
   left: 0;
   height: 25px;
   width: 25px;
   background-color: #eee;
}
.checkboxContainer:hover input ~ .checkboxMarked {
   background-color: rgb(205, 255, 199);
}
.checkboxContainer input:checked ~ .checkboxMarked {
   background-color: rgb(5, 170, 32);
}
.checkboxMarked:after {
   content: "";
   position: absolute;
   display: none;
}
.checkboxContainer input:checked ~ .checkboxMarked:after {
   display: block;
}
.checkboxContainer .checkboxMarked:after {
   left: 9px;
   top: 5px;
   width: 5px;
   height: 10px;
   border: solid white;
   border-width: 0 3px 3px 0;
   -webkit-transform: rotate(45deg);
   -ms-transform: rotate(45deg);
   transform: rotate(45deg);
}
</style>
<body>
<h1>Custom Checkbox Example</h1>
<label class="checkboxContainer">Rice
   <input type="checkbox" checked="checked">
   <span class="checkboxMarked"></span>
</label>
<label class="checkboxContainer">Flour
   <input type="checkbox">
   <span class="checkboxMarked"></span>
</label>
</body>
</html>

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Làm cách nào để tạo hộp kiểm tùy chỉnh và nút radio bằng CSS?