低代码编程实战平台BUILDER · 拖拽搭建
低代码编程实战平台 Low-Code Builder
BLD-261719 表单 2026.03.26 —

揭秘Bootstrap表单设计:轻松上手,打造美观实用的表单示例

Component
表单
Build
261719
Created
03.26
Updated
03.26

Bootstrap 是一个流行的前端框架,它提供了许多用于快速开发响应式布局和美观的网页组件。其中,表单是网页设计中不可或缺的一部分。本文将深入探讨Bootstrap表单的设计,帮助您轻松上手,打造美观实用的表单示例。

Bootstrap表单概述

Bootstrap提供了丰富的表单控件和布局,使开发者能够快速创建美观且功能齐全的表单。以下是一些Bootstrap表单的基本组件:

  • 输入框(Input):用于接收用户输入的数据。
  • 选择框(Select):提供下拉菜单,用户可以选择其中的选项。
  • 复选框和单选框(Checkbox & Radio):用于用户进行多选或单选操作。
  • 文本域(Textarea):用于接收用户的长文本输入。
  • 按钮(Button):用于提交表单或执行其他操作。

创建基本的Bootstrap表单

以下是一个基本的Bootstrap表单示例,我们将使用HTML和Bootstrap的类来构建它。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bootstrap表单示例</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>

<form>
  <div class="form-group">
    <label for="username">用户名:</label>
    <input type="text" class="form-control" id="username" placeholder="请输入用户名">
  </div>
  <div class="form-group">
    <label for="password">密码:</label>
    <input type="password" class="form-control" id="password" placeholder="请输入密码">
  </div>
  <div class="form-group">
    <label>性别:</label>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="gender" id="male" value="male">
      <label class="form-check-label" for="male">男</label>
    </div>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="gender" id="female" value="female">
      <label class="form-check-label" for="female">女</label>
    </div>
  </div>
  <div class="form-group">
    <label for="email">邮箱:</label>
    <input type="email" class="form-control" id="email" placeholder="请输入邮箱地址">
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>

<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>

在上面的代码中,我们使用了form-group类来创建表单组,每个表单控件都包裹在一个form-group中,以便于布局和控制。form-control类用于创建具有Bootstrap风格的表单控件。

高级表单布局

Bootstrap还提供了多种布局选项,例如水平表单、响应式表单等。

水平表单

要创建水平表单,您可以使用row和form-group类,并将form-group放入一个col类中。

<div class="form-row">
  <div class="form-group col-md-6">
    <label for="inputEmail4">邮箱地址</label>
    <input type="email" class="form-control" id="inputEmail4" placeholder="请输入邮箱地址">
  </div>
  <div class="form-group col-md-6">
    <label for="inputPassword4">密码</label>
    <input type="password" class="form-control" id="inputPassword4" placeholder="请输入密码">
  </div>
</div>

响应式表单

Bootstrap的响应式特性使得表单在不同的设备上都能保持良好的布局。通过使用col-sm-、col-md-等类,您可以控制不同屏幕尺寸下的表单布局。

表单验证

Bootstrap提供了简单的表单验证功能,可以通过添加is-invalid、is-valid类来显示验证信息。

<div class="form-group">
  <label for="inputPassword4">密码</label>
  <input type="password" class="form-control" id="inputPassword4" placeholder="请输入密码">
  <div class="invalid-feedback">
    密码不能为空。
  </div>
</div>

在上面的代码中,当输入框为空时,会显示一个错误信息。

总结

通过使用Bootstrap,您可以轻松地创建美观且功能齐全的表单。本文介绍了Bootstrap表单的基本组件、布局选项以及验证功能,帮助您快速上手。希望您能够将这些知识应用到实际项目中,打造出更多优秀的表单设计。

同件

同组件记录

Same Component