小码哥的IT人生

Bootstrap 5 表单

Bootstrap 2023-08-10 09:26:55小码哥的IT人生shichen

Bootstrap 5 表单

堆叠表单

所有设置了 .form-control 类的 <input> 和 <textarea> 元素都可获得正确的表单样式:

示例代码:

<form action="/action_page.php">
  <div class="mb-3 mt-3">
    <label for="email" class="form-label">电子邮件:</label>
    <input type="email" class="form-control" id="email" placeholder="请输入电子邮件地址" name="email">
  </div>
  <div class="mb-3">
    <label for="pwd" class="form-label">密码:</label>
    <input type="password" class="form-control" id="pwd" placeholder="请输入密码" name="pswd">
  </div>
  <div class="form-check mb-3">
    <label class="form-check-label">
      <input class="form-check-input" type="checkbox" name="remember"> 记住我
    </label>
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>堆叠的表单</h2>
  <form action="/demo/demo_form.php">
    <div class="mb-3 mt-3">
      <label for="email">电子邮件:</label>
      <input type="email" class="form-control" id="email" placeholder="请输入电子邮件地址" name="email">
    </div>
    <div class="mb-3">
      <label for="pwd">密码:</label>
      <input type="password" class="form-control" id="pwd" placeholder="请输入密码" name="pswd">
    </div>
    <div class="form-check mb-3">
      <label class="form-check-label">
        <input class="form-check-input" type="checkbox" name="remember">记住我
      </label>
    </div>
    <button type="submit" class="btn btn-primary">提交</button>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

另外请您注意,我们为每个 label 元素添加了 .form-label 类以确保正确的填充。

复选框有不同的标记。它们被设置了 .form-check 类的容器元素包围。label 设置 .form-check-label 类,而复选框和单选按钮使用 .form-check-input

Textarea

示例代码:

<label for="comment">评论:</label>
<textarea class="form-control" rows="5" id="comment" name="text"></textarea>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>Textarea</h2>
  <p>也可以使用 .form-control 类来设置 textarea 的样式:</p>
  <form action="/demo/demo_form.php">
    <div class="mb-3 mt-3">
      <label for="comment">Comments:</label>
      <textarea class="form-control" rows="5" id="comment" name="text">

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

表单行 / 网格(行内表单)

如果您希望表单元素并排显示,请使用 .row.col

示例代码:

<form>
  <div class="row">
    <div class="col">
      <input type="text" class="form-control" placeholder="请输入电子邮件地址" name="email">
    </div>
    <div class="col">
      <input type="password" class="form-control" placeholder="请输入密码" name="pswd">
    </div>
  </div>
</form>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>行内表单</h2>
  <p>如果您希望表单元素并排显示,请使用 .row 和 .col:</p>
  <form>
    <div class="row">
      <div class="col">
        <input type="text" class="form-control" placeholder="请输入电子邮件地址" name="email">
      </div>
      <div class="col">
        <input type="password" class="form-control" placeholder="请输入密码" name="pswd">
      </div>
    </div>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

您将在 Bootstrap 网格 章节中学到有关列和行的更多内容。

表单控件尺寸

您可用 .form-control-lg.form-control-sm 更改 .form-control 输入控件的大小:

示例代码:

<input type="text" class="form-control form-control-lg" placeholder="大型输入控件">
<input type="text" class="form-control" placeholder="普通输入控件">
<input type="text" class="form-control form-control-sm" placeholder="小型输入控件">

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>表单控件大小</h2>
  <p>您可用 .form-control-lg 或 .form-control-sm 更改 .form-control 输入控件的大小:</p>
  <form>
    <input type="text" class="form-control form-control-lg" placeholder="大型输入控件">
    <input type="text" class="form-control mt-3" placeholder="普通输入控件">
    <input type="text" class="form-control form-control-sm mt-3" placeholder="小型输入控件">
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

禁用和只读

请使用 disabled 和/或 readonly 属性禁用输入字段:

示例代码:

<input type="text" class="form-control" placeholder="普通输入控件">
<input type="text" class="form-control" placeholder="被禁用的输入控件" disabled>
<input type="text" class="form-control" placeholder="只读的输入控件" readonly>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>禁用和只读</h2>
  <p>请使用 disabled 和/或 readonly 属性禁用输入字段:</p>
  <form>
    <input type="text" class="form-control" placeholder="普通输入控件">
    <input type="text" class="form-control mt-3" placeholder="被禁用的输入控件" disabled>
    <input type="text" class="form-control mt-3" placeholder="只读的输入控件" readonly>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

纯文本输入

请使用 .form-control-plaintext 类来设置没有边框的输入字段的样式,但保留适当的外边距和内边距:

示例代码:

<input type="text" class="form-control-plaintext" placeholder="纯文本输入">
<input type="text" class="form-control" placeholder="普通输入控件">

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>禁用和只读</h2>
  <p>请使用 disabled 和/或 readonly 属性禁用输入字段:</p>
  <form>
    <input type="text" class="form-control" placeholder="普通输入控件">
    <input type="text" class="form-control mt-3" placeholder="被禁用的输入控件" disabled>
    <input type="text" class="form-control mt-3" placeholder="只读的输入控件" readonly>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

拾色器

如需正确设置 type="color" 的输入样式,请使用 .form-control-color 类:

示例代码:

<input type="color" class="form-control form-control-color" value="#CCCCCC">

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>拾色器</h2>
  <p>如需正确设置 type="color" 的输入样式,请使用 .form-control-color 类:</p>
  <form>
    <label for="myColor" class="form-label">拾色器</label>
    <input type="color" class="form-control form-control-color" id="myColor" value="#CCCCCC" title="请选择一种颜色">
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024