Thẻ <legend> HTML


Thí dụ

Nhóm các phần tử có liên quan trong một biểu mẫu:

<form action="/action_page.php">
  <fieldset>
    <legend>Personalia:</legend>
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

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ẻ <legend>xác định phụ đề cho phần tử <fieldset> .


Hỗ trợ trình duyệt

Element
<legend> Yes Yes Yes Yes Yes

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

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


Thuộc tính sự kiện

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



Các ví dụ khác

Thí dụ

Để chú thích tập hợp trường trôi sang bên phải (với CSS):

<form action="/action_page.php">
  <fieldset>
    <legend style="float:right">Personalia:</legend>
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

Thí dụ

Sử dụng CSS để tạo kiểu cho <fieldset> và <legend>:

<html>
<head>
<style>
fieldset {
  background-color: #eeeeee;
}

legend {
  background-color: gray;
  color: white;
  padding: 5px 10px;
}

input {
  margin: 5px;
}
</style>
</head>
<body>

<form action="/action_page.php">
  <fieldset>
    <legend>Personalia:</legend>
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

</body>
</html>

Các trang liên quan

Tham chiếu DOM HTML: Đối tượng Chú giải


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

Hầu hết các trình duyệt sẽ hiển thị <legend>phần tử với các giá trị mặc định sau:

Thí dụ

legend {
  display: block;
  padding-left: 2px;
  padding-right: 2px;
  border: none;
}