Giới thiệu
Inline form (form nội tuyến) hiển thị label, input, button trên một hàng ngang. Responsive: trên desktop nằm ngang, trên mobile xếp dọc (stack). Dùng Flexbox + media query.
Ví dụ hoàn chỉnh - Inline Login Form
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Inline Form</title>
<style>
* { box-sizing: border-box; }
body { font-family: 'Segoe UI', system-ui, sans-serif; background: #f5f5f5; padding: 40px 20px; }
.form-card { max-width: 700px; margin: 0 auto; background: #fff; padding: 32px; border-radius: 16px; box-shadow: 0 4px 24px rgba(0,0,0,0.08); }
h1 { text-align: center; margin: 0 0 24px; font-size: 24px; }
.inline-form {
display: flex;
flex-wrap: wrap;
align-items: flex-end; /* Căn đáy: label, input, button cùng baseline */
gap: 16px;
}
.form-group { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 180px; }
.form-group label { font-size: 13px; font-weight: 500; color: #333; white-space: nowrap; }
.form-group input {
padding: 12px 14px;
font-size: 15px;
border: 1.5px solid #e0e0e0;
border-radius: 8px;
transition: border-color 0.2s, box-shadow 0.2s;
}
.form-group input:focus { outline: none; border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.15); }
.btn-submit {
padding: 12px 24px;
font-size: 15px;
font-weight: 600;
color: #fff;
background: #667eea;
border: none;
border-radius: 8px;
cursor: pointer;
white-space: nowrap;
transition: background 0.2s, transform 0.1s;
}
.btn-submit:hover { background: #5a6fd6; }
.btn-submit:active { transform: scale(0.98); }
/* Mobile: stacked */
@media (max-width: 600px) {
.inline-form { flex-direction: column; align-items: stretch; }
.form-group { width: 100%; }
.btn-submit { width: 100%; }
}
</style>
</head>
<body>
<div class="form-card">
<h1>Inline Login Form</h1>
<form class="inline-form">
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="you@example.com" required autocomplete="email">
</div>
<div class="form-group">
<label for="password">Mật khẩu</label>
<input type="password" id="password" name="password" placeholder="••••••••" required autocomplete="current-password">
</div>
<button type="submit" class="btn-submit">Đăng nhập</button>
</form>
</div>
</body>
</html>
Kỹ thuật Flexbox cốt lõi
display: flex; flex-wrap: wrap: Cho phép xuống dòng khi hẹp.align-items: flex-end: Căn đáy label/input/button - quan trọng cho visual alignment.gap: 16px: Khoảng cách đều, không cần margin phức tạp.flex: 1; min-width: 180px: Input co giãn, tối thiểu 180px trước khi wrap.
Biến thể: Inline Search/Filter Bar
.filter-bar {
display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end;
padding: 20px; background: #fff; border-radius: 12px; margin-bottom: 24px;
}
.filter-group { flex: 1; min-width: 150px; }
.filter-group select { padding: 10px 12px; border: 1px solid #ddd; border-radius: 6px; }
@media (max-width: 640px) { .filter-bar { flex-direction: column; align-items: stretch; } }
Biến thể: Inline Form trong Navbar
.nav-form { display: flex; align-items: center; gap: 8px; }
.nav-form label { margin: 0; font-size: 13px; white-space: nowrap; }
.nav-form input { padding: 8px 12px; font-size: 13px; width: 180px; }
.nav-form button { padding: 8px 16px; font-size: 13px; white-space: nowrap; }
@media (max-width: 768px) {
.nav-form { flex-direction: column; width: 100%; margin-top: 12px; }
.nav-form input { width: 100%; }
}
Best Practices
- Label không bị ẩn: Luôn có label (visually hidden nếu cần) cho accessibility.
- autocomplete: Bật autocomplete cho email/password.
- Submit trên Enter: Mặc định của form, không cần JS.
- Focus order: Tab tuần tự label → input → button.