Cách tạo: Nút tìm kiếm

Học cách sử dụng CSS để tạo nút tìm kiếm.

Độ rộng toàn bộ:

Trung tâm trong biểu mẫu có độ rộng lớn nhất:

Thử ngay

Cách tạo nút tìm kiếm

Bước 1 - Thêm HTML:

<!-- Tải thư viện biểu tượng -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Biểu mẫu -->
<form class="example" action="action_page.php">
  <input type="text" placeholder="Tìm kiếm..." name="search">
  <button type="submit"><i class="fa fa-search"></i></button>
</form>

Bước 2 - Thêm CSS:

* {
  box-sizing: border-box;
}
/* Thiết lập樣式 cho trường tìm kiếm */
form.example input[type=text] {
  padding: 10px;
  font-size: 17px;
  border: 1px solid grey;
  float: left;
  width: 80%;
  background: #f1f1f1;
}
/* Thiết lập樣式 cho nút submit */
form.example button {
  float: left;
  width: 20%;
  padding: 10px;
  background: #2196F3;
  color: white;
  font-size: 17px;
  border: 1px solid grey;
  border-left: none; /* Ngăn chặn viền hai bên */
  cursor: pointer;
}
form.example button:hover {
  background: #0b7dda;
}
/* Xóa float */
form.example::after {
  content: "";
  clear: both;
  display: table;
}

Thử ngay