Thẻ HTML <legend>

Định nghĩa và cách sử dụng thẻ <legend>

Thẻ <legend> xác định chú thích cho phần tử <fieldset>. Ví dụ sau nhóm các yếu tố liên quan trong một biểu mẫu:

<form action="/action_page.php">
  <fieldset>
    <legend>Thông tin cá nhân:</legend>
    <label for="fname">Họ:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Tên:</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">Ngày sinh:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

Hỗ trợ trình duyệt

Phần tử
<legend>
Google ChromeMS EdgeFirefoxSafariOpera

Thuộc tính Global

Thẻ <legend> cũng hỗ trợ các thuộc tính Global 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

Để chú thích fieldset nổi ở bên phải (với CSS):

<form action="/action_page.php">
  <fieldset>
    <legend style="float:right">Thông tin cá nhân:</legend>
    <label for="fname">Họ:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Tên:</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">Ngày sinh:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

Sử dụng CSS để tạo kiểu cho <fieldset><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>Thông tin cá nhân:</legend>
    <label for="fname">Họ:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Tên:</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">Ngày sinh:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

</body>
</html>

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

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

legend {
  display: block;
  padding-left: 2px;
  padding-right: 2px;
  border: none;
}
Chủ Nhật, 08/01/2023 09:27
53 👨 296
0 Bình luận
Sắp xếp theo
    ❖ HTML