Giới thiệu
Icon button kết hợp biểu tượng (icon) và/hoặc văn bản, giúp người dùng nhận diện hành động nhanh hơn. CSS quản lý layout icon+text, spacing, và trạng thái hover/focus.
Ví dụ hoàn chỉnh - 5 Icon Button với Font Awesome
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Icon Buttons CSS</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.icon-btn {
display: inline-flex;
align-items: center;
gap: 10px;
padding: 12px 20px;
font-size: 16px;
font-weight: 600;
font-family: inherit;
color: #fff;
background: #1eadef;
border: none;
border-radius: 8px;
cursor: pointer;
transition: all 0.25s ease;
}
.icon-btn i {
font-size: 18px;
width: 20px;
text-align: center;
color: #21006d;
transition: color 0.25s;
}
.icon-btn:hover {
background: #512c94;
}
.icon-btn:hover i { color: #fff; }
.icon-btn:focus-visible {
outline: 3px solid rgba(30, 173, 239, 0.5);
outline-offset: 2px;
}
.icon-btn:active { transform: scale(0.97); }
</style>
</head>
<body>
<h1 style="text-align:center; margin:40px 0">Icon Buttons Example</h1>
<div style="text-align:center; display:flex; gap:16px; justify-content:center; flex-wrap:wrap">
<button class="icon-btn"><i class="fas fa-home"></i> Trang chủ</button>
<button class="icon-btn"><i class="fas fa-phone"></i> Gọi cho chúng tôi</button>
<button class="icon-btn"><i class="fas fa-map-marker-alt"></i> Ghé thăm</button>
<button class="icon-btn"><i class="fas fa-cog"></i> Cài đặt</button>
<button class="icon-btn"><i class="fas fa-user"></i> Đăng nhập</button>
</div>
</body>
</html>
3 loại Icon Button phổ biến
| Loại | Mô tả | CSS chính |
|---|---|---|
| Icon + Text | Icon bên trái, chữ bên phải. Dùng cho hành động chính. | gap: 8-12px, padding: 10px 16px |
| Icon Only (Icon Button) | Chỉ có icon, nút vuông/tròn. Cần aria-label. |
padding: 12px, width: 44px, height: 44px |
| Text + Trailing Icon | Chữ trước, icon mũi tên/xuống sau. Dùng cho dropdown, accordion. | gap: 6px, icon margin-left: auto |
Icon Only Button - Ví dụ
<button class="icon-btn icon-only" aria-label="Tìm kiếm">
<i class="fas fa-search"></i>
</button>
<style>
.icon-btn.icon-only {
padding: 12px;
width: 44px;
height: 44px;
justify-content: center;
}
.icon-btn.icon-only i { color: #fff; } /* icon luôn trắng */
</style>
Best Practices
- Kích thước tap target: ≥ 44×44px (material design), icon ~20-24px.
- Căn giữa icon:
display: inline-flex; align-items: center; justify-content: center. - Màu icon: Thường khác màu nền để nổi bật, hoặc dùng
currentColorkế thừa màu chữ. - Accessibility: Icon-only button bắt buộc có
aria-labelhoặctitle. - SVG > Font Icon: SVG linh hoạt hơn (màu, size, animate), không chặn render.