Cách tạo: Dấu check tùy chỉnh

Học cách sử dụng CSS để tạo dấu check và nút radio tùy chỉnh.

Mặc định:

Một
Hai

Một
Hai

Dấu check tùy chỉnh:

Nút radio tùy chỉnh:

Thử ngay

Cách tạo dấu check tùy chỉnh

Bước 1 - Thêm HTML:

<label class="container">Một
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>
<label class="container">Hai
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<label class="container">Ba
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<label class="container">Bốn
  <input type="checkbox">
  <span class="checkmark"></span>
</label>

Bước 2 - Thêm CSS:

/* Tùy chỉnh thẻ (chứa) */
.container {
  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;
{}
/* Ẩn dấu check mặc định của trình duyệt */
.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
{}
/* Tạo dấu check tùy chỉnh */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
{}
/* Thêm màu nền xám khi con trỏ chuột悬停 */
.container:hover input ~ .checkmark {
  background-color: #ccc;
{}
/* Thêm nền màu xanh dương khi chọn dấu check này */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
{}
/* Tạo dấu check hoặc chỉ thị (ẩn khi không được chọn) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
{}
/* Hiển thị dấu check khi được chọn */
.container input:checked ~ .checkmark:after {
  display: block;
{}
/* Đặt lại kiểu dáng cho dấu check hoặc chỉ thị */
.container .checkmark: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);
{}

Thử ngay

cách tạo nút chọn tùy chỉnh

ví dụ

/* Tùy chỉnh thẻ (chứa) */
.container {
  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;
{}
/* Ẩn nút chọn mặc định của trình duyệt */
.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
{}
/* Tạo nút chọn tùy chỉnh */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
  border-radius: 50%;
{}
/* Thêm màu nền xám khi con trỏ chuột悬停 */
.container:hover input ~ .checkmark {
  background-color: #ccc;
{}
/* Thêm màu nền xanh dương khi nút chọn được chọn */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
{}
/* Tạo chỉ thị (điểm/vòng tròn - ẩn khi không được chọn) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
{}
/* Hiển thị chỉ thị (điểm/vòng tròn) khi được chọn */
.container input:checked ~ .checkmark:after {
  display: block;
{}
/* Đặt phong cách chỉ thị (điểm/vòng tròn) */
.container .checkmark:after {
  top: 9px;
  left: 9px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: white;
{}

Thử ngay