Thẻ HTML <button>


Thí dụ

Một nút có thể nhấp được đánh dấu như sau:

<button type="button">Click Me!</button>

Thêm các ví dụ "Hãy tự mình thử" bên dưới.


Định nghĩa và Cách sử dụng

Thẻ <button>xác định một nút có thể nhấp.

Bên trong một phần tử , <button>bạn có thể đặt văn bản (và các thẻ như ,,,, <i>v.v. ) . Điều đó là không thể với một nút được tạo bằng phần tử!<b><strong><br><img><input>

Mẹo: Luôn chỉ định typethuộc tính cho một <button>phần tử để cho các trình duyệt biết loại nút đó là.

Mẹo: Bạn có thể dễ dàng tạo kiểu cho các nút bằng CSS! Xem các ví dụ bên dưới hoặc truy cập hướng dẫn Nút CSS của chúng tôi .


Hỗ trợ trình duyệt

Element
<button> Yes Yes Yes Yes Yes

Thuộc tính

Attribute Value Description
autofocus autofocus Specifies that a button should automatically get focus when the page loads
disabled disabled Specifies that a button should be disabled
form form_id Specifies which form the button belongs to
formaction URL Specifies where to send the form-data when a form is submitted. Only for type="submit"
formenctype application/x-www-form-urlencoded
multipart/form-data
text/plain
Specifies how form-data should be encoded before sending it to a server. Only for type="submit"
formmethod get
post
Specifies how to send the form-data (which HTTP method to use). Only for type="submit"
formnovalidate formnovalidate Specifies that the form-data should not be validated on submission. Only for type="submit"
formtarget _blank
_self
_parent
_top
framename
Specifies where to display the response after submitting the form. Only for type="submit"
name name Specifies a name for the button
type button
reset
submit
Specifies the type of button
value text Specifies an initial value for the button

Thuộc tính toàn cầu

Thẻ <button>cũng hỗ trợ Thuộc tính chung trong HTML .


Thuộc tính sự kiện

Thẻ <button>cũng hỗ trợ Thuộc tính sự kiện trong HTML .



Các ví dụ khác

Thí dụ

Sử dụng CSS để tạo kiểu cho các nút:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

.button1 {background-color: #4CAF50;} /* Green */
.button2 {background-color: #008CBA;} /* Blue */
</style>
</head>
<body>

<button class="button button1">Green</button>
<button class="button button2">Blue</button>

</body>
</html>

Thí dụ

Sử dụng CSS để tạo kiểu cho các nút (với hiệu ứng di chuột):

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  border: none;
  color: white;
  padding: 16px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  transition-duration: 0.4s;
  cursor: pointer;
}

.button1 {
  background-color: white;
  color: black;
  border: 2px solid #4CAF50;
}

.button1:hover {
  background-color: #4CAF50;
  color: white;
}

.button2 {
  background-color: white;
  color: black;
  border: 2px solid #008CBA;
}

.button2:hover {
  background-color: #008CBA;
  color: white;
}

</style>
</head>
<body>

<button class="button button1">Green</button>
<button class="button button2">Blue</button>

</body>
</html>

Các trang liên quan

Tham chiếu DOM HTML: Đối tượng Nút

Hướng dẫn CSS: Nút tạo kiểu


Cài đặt CSS mặc định

Không có.