Thẻ <legend> HTML
Định nghĩa và cách sử dụng
<legend>
thẻ định nghĩa <fieldset> phần tử tiêu đề (caption).
Xin xem thêm:
Hướng dẫn tham khảo HTML DOM:Đối tượng Legend
Mô hình
Ví dụ 1
Nhóm các yếu tố liên quan trong biểu mẫu:
<form action="/action_page.php"> <fieldset> <legend>Thông tin cá nhân:</legend> <label for="fname">Tên:</label> <input type="text" id="fname" name="fname"><br><br> <label for="lname">Họ:</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="提交"> </fieldset> </form>
Ví dụ 2
Cho tiêu đề của trường tập điều chỉnh sang phải (sử dụng CSS):
<form action="/action_page.php"> <fieldset> <legend style="float:right">Thông tin cá nhân:</legend> <label for="fname">Tên:</label> <input type="text" id="fname" name="fname"><br><br> <label for="lname">Họ:</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="提交"> </fieldset> </form>
Ví dụ 3
sử dụng CSS để thiết lập <fieldset>
và <legend>
Cách thức:
<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">Tên:</label> <input type="text" id="fname" name="fname"><br><br> <label for="lname">Họ:</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="提交"> </fieldset> </form> </body> </html>
全局属性
<legend>
标签还支持 HTML 中的全局属性。
事件属性
<legend>
标签还支持 HTML 中的事件属性。
默认的 CSS 设置
大多数浏览器将使用以下默认值显示 <legend>
元素:
legend { display: block; padding-left: 2px; padding-right: 2px; border: none; }
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |