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

Tạo nhóm nút bấm dọc (Vertical Button Group) với CSS

Nhóm nút bấm dọc thường dùng cho sidebar, menu điều hướng hoặc form actions xếp chồng nhau. Sử dụng display: block hoặc Flexbox để xếp dọc.

Ví dụ cơ bản (Float/Block)

<!DOCTYPE html>
<html>
<head>
  <style>
    .mybtn .button {
      background-color: #e67e22;
      border: 1px solid #27ae60;
      width: 120px;
      color: white;
      font-size: 14px;
      padding: 10px;
      text-align: center;
      text-decoration: none;
      display: block;
      margin-bottom: 5px;
      border-radius: 4px;
      cursor: pointer;
      transition: background 0.2s;
    }
    .mybtn .button:hover {
      background-color: #d35400;
    }
  </style>
</head>
<body>
  <div class="mybtn">
    <button class="button">Kết quả 1</button>
    <button class="button">Kết quả 2</button>
    <button class="button">Kết quả 3</button>
    <button class="button">Kết quả 4</button>
  </div>
</body>
</html>

Cách hiện đại: Flexbox Column

.btn-group-vertical {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: fit-content;
}
.btn-group-vertical .btn {
  width: 100%;
  text-align: left;
  padding: 12px 16px;
}

Nhóm nút bo góc liền mạch (Connected)

.btn-group-vertical .btn {
  border-radius: 0;
  border-bottom: 1px solid rgba(0,0,0,0.1);
}
.btn-group-vertical .btn:first-child {
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
}
.btn-group-vertical .btn:last-child {
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
  border-bottom: none;
}

Kết hợp với icon

.btn-group-vertical .btn {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-start;
}
.btn-group-vertical .btn svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

Responsive: Dọc trên mobile, ngang trên desktop

.btn-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
@media (min-width: 768px) {
  .btn-group {
    flex-direction: row;
  }
  .btn-group .btn {
    flex: 1;
  }
}