HTML <fieldset> 標簽

定義和用法

<fieldset> 標簽用于在表單中分組相關元素。

<fieldset> 標簽會在相關元素周圍繪制一個框。

<legend> 標簽為 fieldset 元素定義標題。

另請參閱:

HTML DOM 參考手冊:Fieldset 對象

實例

例子 1

在表單中對相關元素進行分組:

<form action="/action_page.php">
  <fieldset>
    <legend>個人信息:</legend>
    <label for="fname">名字:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">姓氏:</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="提交">
  </fieldset>
</form>

親自試一試

例子 2

使用 CSS 設置 <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>個人信息:</legend>
    <label for="fname">名字:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">姓氏:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">電郵:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">生日:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="提交">
  </fieldset>
</form>
</body>
</html>

親自試一試

提示和注釋

提示:<legend> 標簽用于為 <fieldset> 元素定義標題。

屬性

屬性 描述
disabled disabled 規定應禁用一組相關的表單元素。
form form_id 規定字段集屬于哪個表單。
name 文本 規定字段集的名稱。

全局屬性

<fieldset> 標簽還支持 HTML 中的全局屬性

事件屬性

<fieldset> 標簽還支持 HTML 中的事件屬性

默認的 CSS 設置

大多數瀏覽器將使用以下默認值顯示 <fieldset> 元素:

fieldset {
  display: block;
  margin-left: 2px;
  margin-right: 2px;
  padding-top: 0.35em;
  padding-bottom: 0.625em;
  padding-left: 0.75em;
  padding-right: 0.75em;
  border: 2px groove (internal value);
}

瀏覽器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持