Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Tạo dấu tích (checkmark/tick) với CSS


Tạo checkmark tùy chỉnh chỉ với CSS, không cần font/icon image.

Cách 1: Pseudo-elements (khuyên dùng)

<!DOCTYPE html>
<html>
<head>
  <style>
    .checkmark {
      width: 40px; height: 40px;
      border: 3px solid #4caf50;
      border-radius: 50%;
      position: relative;
      display: inline-flex;
      justify-content: center;
      align-items: center;
      margin: 20px;
    }
    .checkmark::after {
      content: "";
      width: 10px; height: 20px;
      border: solid #4caf50;
      border-width: 0 3px 3px 0;
      transform: rotate(45deg) translateY(-2px);
      display: block;
    }
    .checkmark.unchecked {
      border-color: #ccc;
    }
    .checkmark.unchecked::after {
      border-color: transparent;
    }
  </style>
</head>
<body>
  <h1>Checkmark CSS</h1>
  <div class="checkmark"></div>
  <div class="checkmark unchecked"></div>
</body>
</html>

Cách 2: Border-only (đơn giản)

<style>
  .tick {
    display: inline-block;
    width: 20px; height: 35px;
    border-right: 4px solid #4caf50;
    border-bottom: 4px solid #4caf50;
    transform: rotate(45deg);
    margin: 20px;
  }
</style>
<div class="tick"></div>

Cách 3: Checkbox custom (thực tế)

<style>
  .custom-checkbox { position: relative; display: inline-flex; align-items: center; cursor: pointer; }
  .custom-checkbox input { position: absolute; opacity: 0; width: 0; height: 0; }
  .checkmark-box {
    width: 24px; height: 24px;
    border: 2px solid #ddd; border-radius: 4px;
    background: white; transition: all 0.2s;
    position: relative;
  }
  .custom-checkbox input:checked ~ .checkmark-box {
    background: #4caf50; border-color: #4caf50;
  }
  .checkmark-box::after {
    content: ""; position: absolute;
    left: 8px; top: 4px; width: 6px; height: 12px;
    border: solid white; border-width: 0 2px 2px 0;
    transform: rotate(45deg); opacity: 0; transition: opacity 0.2s;
  }
  .custom-checkbox input:checked ~ .checkmark-box::after { opacity: 1; }
</style>
<label class="custom-checkbox">
  <input type="checkbox">
  <span class="checkmark-box"></span>
  <span>Đồng ý điều khoản</span>
</label>

Kết quả

3 kiểu: tròn có tích, tick đơn giản border, checkbox custom hoàn chỉnh có animation.