
- Bootstrap 教程
- Bootstrap - 首頁
- Bootstrap - 概述
- Bootstrap - 環境設定
- Bootstrap - RTL(從右到左)
- Bootstrap - CSS 變數
- Bootstrap - 顏色模式
- Bootstrap 佈局
- Bootstrap - 斷點
- Bootstrap - 容器
- Bootstrap - 網格系統
- Bootstrap - 列
- Bootstrap - 間距
- Bootstrap - 實用工具類
- Bootstrap - CSS 網格
- Bootstrap 元件
- Bootstrap - 手風琴
- Bootstrap - 警報
- Bootstrap - 徽章
- Bootstrap - 麵包屑
- Bootstrap - 按鈕
- Bootstrap - 按鈕組
- Bootstrap - 卡片
- Bootstrap - 走馬燈
- Bootstrap - 關閉按鈕
- Bootstrap - 摺疊
- Bootstrap - 下拉選單
- Bootstrap - 列表組
- Bootstrap - 模態框
- Bootstrap - 導航欄
- Bootstrap - 導航和選項卡
- Bootstrap - 側邊欄
- Bootstrap - 分頁
- Bootstrap - 佔位符
- Bootstrap - 氣泡提示
- Bootstrap - 進度條
- Bootstrap - 滾動偵聽
- Bootstrap - 載入動畫
- Bootstrap - 吐司提示
- Bootstrap - 工具提示
- Bootstrap 表單
- Bootstrap - 表單
- Bootstrap - 表單控制元件
- Bootstrap - 選擇框
- Bootstrap - 複選框和單選按鈕
- Bootstrap - 滑塊
- Bootstrap - 輸入組
- Bootstrap - 浮動標籤
- Bootstrap - 佈局
- Bootstrap - 表單驗證
- Bootstrap 輔助類
- Bootstrap - 清除浮動
- Bootstrap - 顏色和背景
- Bootstrap - 彩色連結
- Bootstrap - 焦點環
- Bootstrap - 圖示連結
- Bootstrap - 位置
- Bootstrap - 比例
- Bootstrap - 堆疊
- Bootstrap - 拉伸連結
- Bootstrap - 文字截斷
- Bootstrap - 垂直線
- Bootstrap - 視覺隱藏
- Bootstrap 實用工具類
- Bootstrap - 背景
- Bootstrap - 邊框
- Bootstrap - 顏色
- Bootstrap - 顯示
- Bootstrap - Flex 佈局
- Bootstrap - 浮動
- Bootstrap - 互動
- Bootstrap - 連結
- Bootstrap - 物件適應
- Bootstrap - 不透明度
- Bootstrap - 溢位
- Bootstrap - 位置
- Bootstrap - 陰影
- Bootstrap - 大小
- Bootstrap - 間距
- Bootstrap - 文字
- Bootstrap - 垂直對齊
- Bootstrap - 可見性
- Bootstrap 演示
- Bootstrap - 網格演示
- Bootstrap - 按鈕演示
- Bootstrap - 導航演示
- Bootstrap - 部落格演示
- Bootstrap - 滑塊演示
- Bootstrap - 走馬燈演示
- Bootstrap - 頁首演示
- Bootstrap - 頁尾演示
- Bootstrap - 英雄圖演示
- Bootstrap - 特色演示
- Bootstrap - 側邊欄演示
- Bootstrap - 下拉選單演示
- Bootstrap - 列表組演示
- Bootstrap - 模態框演示
- Bootstrap - 徽章演示
- Bootstrap - 麵包屑演示
- Bootstrap - 巨大塊演示
- Bootstrap - 粘性頁尾演示
- Bootstrap - 相簿演示
- Bootstrap - 登入演示
- Bootstrap - 定價演示
- Bootstrap - 結賬演示
- Bootstrap - 產品演示
- Bootstrap - 封面演示
- Bootstrap - 儀表盤演示
- Bootstrap - 粘性頁尾導航欄演示
- Bootstrap - 砌體佈局演示
- Bootstrap - 啟動模板演示
- Bootstrap - RTL 相簿演示
- Bootstrap - RTL 結賬演示
- Bootstrap - RTL 走馬燈演示
- Bootstrap - RTL 部落格演示
- Bootstrap - RTL 儀表盤演示
- Bootstrap 有用資源
- Bootstrap - 問答
- Bootstrap - 快速指南
- Bootstrap - 有用資源
- Bootstrap - 討論
Bootstrap - 表單佈局
本章將討論 Bootstrap 表單佈局。使用表單佈局選項(從內聯到水平,再到自定義網格)為表單新增結構。
表單佈局
Bootstrap 不會為<form>元素提供預設樣式,但瀏覽器預設會提供一些功能性的高亮顯示。每一組表單欄位都必須位於<form>元素內。
請務必為<form>內的<button>指定幷包含 type 屬性,因為它們的預設值為type="submit"。
表單預設垂直堆疊。Bootstrap 對幾乎所有表單控制元件都使用display: block和width: 100%。要更改每個表單的佈局,請使用其他類。
實用工具類
要為表單新增一些結構,應使用邊距實用工具類。對標籤、控制元件、可選表單文字和表單驗證訊息進行基本分組。
為保持一致性,建議堅持使用margin-bottom實用工具類,並在整個表單中採用單一方向。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout </title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <div class="mb-3"> <label for="formGroupExampleInput" class="form-label">Username</label> <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Username"> </div> <div class="mb-3"> <label for="formGroupExampleInput2" class="form-label">Password</label> <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="password"> </div> </body> </html>
表單網格
使用網格類構建更復雜的表單。為需要多列、不同寬度和附加對齊選項的表單佈局構建網格。您需要使用$enable-grid-classes啟用 Sass 變數。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="row mt-2"> <div class="col"> <input type="text" class="form-control" placeholder="Username" aria-label="Username"> </div> <div class="col"> <input type="text" class="form-control" placeholder="Password" aria-label="Password"> </div> </div> </body> </html>
間距
要控制內聯和塊方向上的間距寬度,請使用間距修飾符類。透過使用$enable-grid-classes,您可以啟用 Sass 變數。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <div class="row g-3 mt-2"> <div class="col"> <input type="text" class="form-control" placeholder="Username" aria-label="Username"> </div> <div class="col"> <input type="text" class="form-control" placeholder="Password" aria-label="Password"> </div> </div> </body> </html>
使用網格系統建立更復雜的佈局。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <form class="row g-3"> <div class="col-md-6"> <label for="inputFirst" class="form-label">First name</label> <input type="text" class="form-control" id="inputFirst"> </div> <div class="col-md-6"> <label for="inputLast" class="form-label">Last name</label> <input type="text" class="form-control" id="inputLast"> </div> <div class="col-md-6"> <label for="inputEmail" class="form-label">Email</label> <input type="email" class="form-control" id="inputEmail"> </div> <div class="col-6"> <label for="inputPassword" class="form-label">Password</label> <input type="Password" class="form-control" id="inputPassword" placeholder="password"> </div> <div class="col-12"> <label for="inputAddress" class="form-label">Address</label> <input type="text" class="form-control" id="inputAddress" placeholder="Address"> </div> <div class="col-md-6"> <label for="inputAdharno" class="form-label">Adharcard no</label> <input type="text" class="form-control" id="inputAdharno"> </div> <div class="col-md-6"> <label for="inputMobno" class="form-label">Mobile no</label> <input type="text" class="form-control" id="inputMobno"> </div> <div class="col-12"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="gridCheck"> <label class="form-check-label" for="gridCheck"> I agree to terms and conditions </label> </div> </div> <div class="col-12"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </body> </html>
水平表單
使用類.row來形成組,以使用網格建立水平表單。使用類.col-*-*定義標籤和控制元件的寬度。
要使表單與其關聯的表單控制元件垂直居中,請將類.col-form-label應用於<label>。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <form> <div class="row mb-3"> <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label> <div class="col-sm-10"> <input type="email" class="form-control" id="inputEmail3"> </div> </div> <div class="row mb-3"> <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label> <div class="col-sm-10"> <input type="password" class="form-control" id="inputPassword3"> </div> </div> <fieldset class="row mb-3"> <legend class="col-form-label col-sm-2 pt-0">Language Known</legend> <div class="col-sm-10"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="gridCheck" id="gridCheck1" value="option1" checked> <label class="form-check-label" for="gridCheck1"> English </label> </div> <div class="form-check"> <input class="form-check-input" type="checkbox" name="gridCheck" id="gridCheck2" value="option2"> <label class="form-check-label" for="gridCheck2"> Hindi </label> </div> <div class="form-check"> <input class="form-check-input" type="checkbox" name="gridCheck" id="gridCheck2" value="option2"> <label class="form-check-label" for="gridCheck2"> marathi </label> </div> <div class="form-check disabled"> <input class="form-check-input"type="checkbox" name="gridCheck" id="gridCheck3" value="option3" disabled> <label class="form-check-label" for="gridCheck3"> Others </label> </div> </div> </fieldset> <div class="row mb-3"> <div class="col-sm-10 offset-sm-2"> <div class="form-check"> <input class="form-check-input" type="radio" id="gridRadio1"> <label class="form-check-label" for="gridRadio1"> Radio Button </label> </div> </div> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </body> </html>
水平表單標籤大小
將類.col-form-label-sm或.col-form-label-lg應用於您的<label>或<legend>,以分別遵循.form-control-sm和.form-control-lg的大小。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <div class="row mb-3"> <label for="colFormSm" class="col-sm-2 col-form-label col-form-label-sm">Username</label> <div class="col-sm-10"> <input type="email" class="form-control form-control-sm" id="colFormSm" placeholder="Username"> </div> </div> <div class="row mb-3"> <label for="colFormLabel" class="col-sm-2 col-form-label">Password</label> <div class="col-sm-10"> <input type="text" class="form-control" id="colFormLabel" placeholder="Password"> </div> </div> <div class="row"> <label for="colFormLg" class="col-sm-2 col-form-label col-form-label-lg">Email</label> <div class="col-sm-10"> <input type="email" class="form-control form-control-lg" id="colFormLg" placeholder="email"> </div> </div> </body> </html>
列大小
如前例所示,網格系統允許在.row中使用任意數量的.cols。在它們之間平均分配可用寬度。您還可以使用特定列類,例如.col-sm-7,選擇要佔用更多或更少空間的列子集,而其餘的.col則平均分配剩餘空間。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <div class="row g-3"> <div class="col-sm-6"> <input type="text" class="form-control" placeholder="Email" aria-label="Email"> </div> <div class="col-sm-3"> <input type="text" class="form-control" placeholder="Mobile no" aria-label="Mobile no"> </div> <div class="col-sm-2"> <input type="text" class="form-control" placeholder="Age" aria-label="Age"> </div> </div> </body> </html>
自動調整大小
在下面的示例中,使用 flexbox 實用工具類垂直居中內容,並將.col更改為.col-auto。這樣,列只會佔用其所需的空間。換句話說,列的大小取決於內容。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <form class="row gy-2 gx-3 align-items-center"> <div class="col-auto"> <label class="visually-hidden" for="inputName">Name</label> <input type="text" class="form-control" id="inputName" placeholder="Name"> </div> <div class="col-auto"> <label class="visually-hidden" for="inputGroup"></label> <div class="input-group"> <input type="text" class="form-control" id="inputGroup" placeholder="Username"> <div class="input-group-text">@gmail.com</div> </div> </div> <div class="col-auto"> <label class="visually-hidden" for="selectLanguage">Language</label> <select class="form-select" id="selectLanguage"> <option selected>Choose...</option> <option value="1">English</option> <option value="2">Hindi</option> <option value="3">Marathi</option> </select> </div> <div class="col-auto"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="autoSizingCheck"> <label class="form-check-label" for="autoSizingCheck"> Remember me </label> </div> </div> <div class="col-auto"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </body> </html>
然後您可以再次與特定大小的列類混合使用。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <form class="row gx-3 gy-2 align-items-center mt-2"> <div class="col-sm-3"> <label class="visually-hidden" for="inputName">Name</label> <input type="text" class="form-control" id="inputName" placeholder="Name"> </div> <div class="col-sm-3"> <label class="visually-hidden" for="specificSizeInputGroupUsername">Username</label> <div class="input-group"> <input type="text" class="form-control" id="specificSizeInputGroupUsername" placeholder="Email"> <div class="input-group-text">@gmail.com</div> </div> </div> <div class="col-sm-3"> <label class="visually-hidden" for="selectLanguage">Language</label> <select class="form-select" id="selectLanguage"> <option selected>Choose...</option> <option value="1">English</option> <option value="2">Hindi</option> <option value="3">Marathi</option> </select> </div> <div class="col-auto"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="autoSizingCheck2"> <label class="form-check-label" for="autoSizingCheck2"> Remember me </label> </div> </div> <div class="col-auto"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </body> </html>
內聯表單
要構建響應式水平佈局,請使用類.row-cols-*。
使用間距修飾符類在水平和垂直方向上新增間距。
在狹窄的移動視口中,.col-12對於堆疊表單控制元件等很有用。
要將表單元素居中對齊並使.form-check正確對齊,請使用類.align-items-center。
示例
您可以使用編輯和執行選項編輯和嘗試執行此程式碼。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Layout</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"> </script> </head> <body> <form class="row row-cols-lg-auto g-3 align-items-center"> <div class="col-12"> <label class="visually-hidden" for="inlineFormInputGroupUsername">Username</label> <div class="input-group"> <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username"> <div class="input-group-text">@gmail.com</div> </div> </div> <div class="col-12"> <label class="visually-hidden" for="selectLanguage">Language</label> <select class="form-select" id="selectLanguage"> <option selected>Choose...</option> <option value="1">English</option> <option value="2">Hindi</option> <option value="3">Marathi</option> </select> </div> <div class="col-12"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="inlineFormCheck"> <label class="form-check-label" for="inlineFormCheck"> Remember me </label> </div> </div> <div class="col-12"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </body> </html>