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

Tạo nút bấm bo tròn với hiệu ứng hover/active bằng CSS

Sử dụng border-radius để bo góc, pseudo-class :hover cho hiệu ứng chuột di qua, :active cho trạng thái nhấn, và class .active cho trạng thái được chọn.

Ví dụ minh họa

<!DOCTYPE html>
<html>
<head>
  <style>
    .demo {
      display: flex;
      gap: 8px;
      flex-wrap: wrap;
    }
    .demo a {
      color: #e74c3c;
      padding: 8px 16px;
      text-decoration: none;
      border-radius: 6px;
      transition: all 0.2s;
      border: 1px solid transparent;
    }
    .demo a.active {
      background-color: #e67e22;
      color: white;
      border-color: #d35400;
    }
    .demo a:hover:not(.active) {
      background-color: #fef9e7;
      border-color: #f1c40f;
    }
    .demo a:active:not(.active) {
      background-color: #fdebd0;
      transform: scale(0.98);
    }
  </style>
</head>
<body>
  <h2>Các bài Quiz</h2>
  <div class="demo">
    <a href="#" aria-label="Previous"><</a>
    <a href="#">Quiz 1</a>
    <a href="#">Quiz 2</a>
    <a href="#">Quiz 3</a>
    <a href="#" class="active" aria-current="page">Quiz 4</a>
    <a href="#" aria-label="Next">></a>
  </div>
</body>
</html>

Các trạng thái nút bấm

Trạng tháiSelectorMô tả
Mặc định.btnGiao diện bình thường
Hover.btn:hoverChuột di qua
Active (nhấn).btn:activeĐang click chuột
Focus.btn:focus / :focus-visibleTab focus (accessibility)
Disabled.btn:disabledVô hiệu hóa
Selected.btn.active / [aria-pressed="true"]Trạng thái được chọn

Button bo tròn hoàn chỉnh (Pill button)

.btn-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  border-radius: 9999px; /* Fully rounded */
  border: 2px solid #3498db;
  background: white;
  color: #3498db;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-pill:hover {
  background: #3498db;
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(52,152,219,0.3);
}
.btn-pill:active {
  transform: translateY(0) scale(0.98);
}
.btn-pill:focus-visible {
  outline: 2px solid #3498db;
  outline-offset: 2px;
}

Pagination Accessible

.pagination {
  display: flex;
  gap: 4px;
}
.pagination a {
  padding: 8px 12px;
  border-radius: 6px;
}
.pagination a[aria-current="page"] {
  background: #3498db;
  color: white;
}
.pagination a[aria-label] {
  /* Prev/Next buttons */
}