Bootstrap5 表單

2023-07-10 17:34 更新

在本章中,我們將學(xué)習(xí)如何使用 Bootstrap 創(chuàng)建表單。Bootstrap 通過(guò)一些簡(jiǎn)單的 HTML 標(biāo)簽和擴(kuò)展的類即可創(chuàng)建出不同樣式的表單。

表單元素 <input>, <textarea>, 和 <select> elements 在使用 .form-control 類的情況下,寬度都是設(shè)置為 100%。

Bootstrap5 表單布局

  • 堆疊表單 (全屏寬度):垂直方向
  • 內(nèi)聯(lián)表單:水平方向

Bootstrap 提供了兩種類型的表單布局:

堆疊表單

以下實(shí)例使用兩個(gè)輸入框,一個(gè)復(fù)選框,一個(gè)提交按鈕來(lái)創(chuàng)建堆疊表單:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h2>堆疊表單</h2>
	<form action="">
	  <div class="mb-3 mt-3">
		<label for="email" class="form-label">Email:</label>
		<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
	  </div>
	  <div class="mb-3">
		<label for="pwd" class="form-label">Password:</label>
		<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd">
	  </div>
	  <div class="form-check mb-3">
		<label class="form-check-label">
		  <input class="form-check-input" type="checkbox" name="remember"> Remember me
		</label>
	  </div>
	  <button type="submit" class="btn btn-primary">Submit</button>
	</form>
</div>

</body>
</html>

顯示效果:

實(shí)例中我們使用 .form-label 類來(lái)確保標(biāo)簽元素有一定的內(nèi)邊距。

復(fù)選框(checkboxe)使用不同的標(biāo)記。 它們使用 .form-check 包裹在容器元素周圍。復(fù)選框和單選按鈕使用 .form-check-input,它的標(biāo)簽可以使用 .form-check-label 類。

內(nèi)聯(lián)表單

如果您希望表單元素并排顯示,請(qǐng)使用 .row 和 .col:

以下實(shí)例的兩個(gè)輸入框并排顯示,創(chuàng)建內(nèi)聯(lián)表單:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h2>內(nèi)聯(lián)表單</h2>
  <p>如果您希望表單元素并排顯示,請(qǐng)使用 .row 和 .col:</p>
  <form>
    <div class="row">
      <div class="col">
        <input type="text" class="form-control" placeholder="Enter email" name="email">
      </div>
      <div class="col">
        <input type="password" class="form-control" placeholder="Enter password" name="pswd">
      </div>
    </div>
  </form>
</div>

</body>
</html>

顯示效果:

文本框

使用 textarea 標(biāo)簽創(chuàng)建文本框提交表單,使用 .form-control 類渲染文本框 textareas 標(biāo)簽:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container mt-3">
  <h2>文本框</h2>
  <p>使用 .form-control 類渲染文本框 textareas 標(biāo)簽:</p>
  <form action="/action_page.php">
    <div class="mb-3 mt-3">
      <label for="comment">請(qǐng)輸入評(píng)論:</label>
      <textarea class="form-control" rows="5" id="comment" name="text"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">提交</button>
  </form>
</div>

</body>
</html>

顯示效果:

輸入框大小

我們可以通過(guò)在 .form-control 輸入框中使用 .form-control-lg 或 .form-control-sm 類來(lái)設(shè)置輸入框的大小:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>


<div class="container mt-3">
  <h2>輸入框大小</h2>
  <p>我們可以通過(guò)在 .form-control 輸入框中使用 .form-control-lg 或 .form-control-sm 類來(lái)設(shè)置輸入框的大小:</p>
  <form>
    <input type="text" class="form-control form-control-lg" placeholder="大號(hào)輸入框">
	<input type="text" class="form-control mt-3" placeholder="正常大小輸入框">
	<input type="text" class="form-control form-control-sm mt-3" placeholder="小號(hào)輸入框">
  </form>
</div>

</body>
</html>

顯示效果:


禁用/只讀表單

使用 disabled/readonly 屬性設(shè)置輸入框禁用/只讀:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>


<div class="container mt-3">
  <h2>禁用/只讀表單</h2>
  <p>使用 disabled/readonly 屬性設(shè)置輸入框禁用/只讀:</p>
  <form>
    <input type="text" class="form-control" placeholder="Normal input">
	<input type="text" class="form-control mt-3" placeholder="Disabled input" disabled>
	<input type="text" class="form-control mt-3" placeholder="Readonly input" readonly>
  </form>
</div>

</body>
</html>

純文本輸入

使用 .form-control-plaintext 類可以刪除輸入框的邊框:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>


<div class="container mt-3">
  <h2>純文本輸入</h2>
  <p>使用 .form-control-plaintext 類可以刪除輸入框的邊框:</p>
  <form>
    <input type="text" class="form-control-plaintext" placeholder="無(wú)邊框的輸入框">
	<input type="text" class="form-control" placeholder="正常輸入框">
  </form>
</div>

</body>
</html>

取色器

使用 .form-control-color 類可以創(chuàng)建一個(gè)取色器:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap5 實(shí)例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
  <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>


<div class="container mt-3">
  <h2>取色器</h2>
  <p>使用 .form-control-color 類可以創(chuàng)建一個(gè)取色器:</p>
  <form>
    <input type="color" class="form-control form-control-color" value="#CCCCCC">
  </form>
</div>

</body>
</html>

顯示效果:

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)