小码哥的IT人生

Bootstrap 5 输入组

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

Bootstrap 5 输入组

输入组

.input-group 类是一种容器,通过在输入字段的前面或后面添加图标、文本或按钮作为“帮助文本”来增强输入。

如需设置指定帮助文本的样式,请使用 .input-group-text 类:

示例代码:

<form>
  <div class="input-group">
    <span class="input-group-text">@</span>
    <input type="text" class="form-control" placeholder="用户名">
  </div>
  <div class="input-group">
    <input type="text" class="form-control" placeholder="您的电邮">
    <span class="input-group-text">@example.com</span>
  </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>.input-group 类是一种容器,通过在输入字段的前面或后面添加图标、文本或按钮作为“帮助文本”来增强输入。</p>
  <p>如需设置指定帮助文本的样式,请使用 .input-group-text 类:</p>
  <form action="/demo/demo_form.php">
    <div class="input-group mb-3">
      <span class="input-group-text">@</span>
      <input type="text" class="form-control" placeholder="用户名" name="usrname">
    </div>
    <div class="input-group mb-3">
      <input type="text" class="form-control" placeholder="您的电邮" name="email">
      <span class="input-group-text">@example.com</span>
    </div>
    <button type="submit" class="btn btn-primary">提交</button>
  </form>
</div>
</body>
</html>

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

输入组大小

请将 .input-group-sm 类用于小型输入组,将 .input-group-lg 用于大型输入组:

示例代码:

<div class="input-group mb-3 input-group-sm">
   <span class="input-group-text">小型</span>
  <input type="text" class="form-control">
</div>
<div class="input-group mb-3">
  <span class="input-group-text">默认</span>
  <input type="text" class="form-control">>
</div>
<div class="input-group mb-3 input-group-lg">
  <span class="input-group-text">大型</span>
  <input type="text" class="form-control">
</div>

 

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

<!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>请将 .input-group-sm 类用于小型输入组,将 .input-group-lg 用于大型输入组:</p>
  <div class="input-group input-group-sm mb-3">
    <span class="input-group-text">小型</span>
    <input type="text" class="form-control">
  </div>
  <div class="input-group mb-3">
    <span class="input-group-text">默认</span>
    <input type="text" class="form-control">
  </div>
  <div class="input-group input-group-lg mb-3">
    <span class="input-group-text">大型</span>
    <input type="text" class="form-control">
  </div>
</div>
</body>
</html>

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

多个输入和助手

添加多个输入控件或插件:

示例代码:

<!-- 多个输入控件 -->
<div class="input-group mb-3">
  <span class="input-group-text">人名</span>
  <input type="text" class="form-control" placeholder="姓">
  <input type="text" class="form-control" placeholder="名">
</div>
<!-- 多个插件/帮助文本 -->
<div class="input-group mb-3">
  <span class="input-group-text">一</span>
  <span class="input-group-text">二</span>
  <span class="input-group-text">三</span>
  <input type="text" class="form-control">
</div>

 

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

<!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>添加多个输入控件或插件:</p>
  <!-- 多个输入控件 -->
  <div class="input-group mb-3">
    <span class="input-group-text">人名</span>
    <input type="text" class="form-control" placeholder="姓">
    <input type="text" class="form-control" placeholder="名">
  </div>
  <!-- 多个插件/帮助文本 -->
  <div class="input-group mb-3">
    <span class="input-group-text">一</span>
    <span class="input-group-text">二</span>
    <span class="input-group-text">三</span>
    <input type="text" class="form-control">
  </div>
</div>
</body>
</html>

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

带复选框和单选框的输入组

您还可以使用复选框或单选按钮代替文本:

示例代码:

<div class="input-group mb-3">
  <div class="input-group-text">
    <input type="checkbox">
  </div>
  <input type="text" class="form-control" placeholder="一些文本">
</div>
<div class="input-group mb-3">
  <div class="input-group-text">
    <input type="radio">
  </div>
  <input type="text" class="form-control" placeholder="一些文本">
</div>

 

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

<!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">
  <h3>带复选框和单选框的输入组</h3>
  <p>您还可以使用复选框或单选按钮代替文本:</p>
  <!-- Multiple inputs -->
  <div class="input-group mb-3">
    <div class="input-group-text">
      <input type="checkbox">
    </div>
    <input type="text" class="form-control" placeholder="一些文本">
  </div>
  <div class="input-group mb-3">
    <div class="input-group-text">
      <input type="radio">
    </div>
    <input type="text" class="form-control" placeholder="一些文本">
  </div>
</div>
</body>
</html>

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

输入组按钮

示例代码:

<div class="input-group mb-3">
  <button class="btn btn-outline-primary" type="button">基础按钮</button>
  <input type="text" class="form-control" placeholder="一些文本">
</div>
<div class="input-group mb-3">
  <input type="text" class="form-control" placeholder="搜索">
  <button class="btn btn-success" type="submit">Go</button>
</div>
<div class="input-group mb-3">
  <input type="text" class="form-control" placeholder="一些文本">
  <button class="btn btn-primary" type="button">OK</button>
  <button class="btn btn-danger" type="button">Cancel</button>
</div>

 

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

<!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">
  <h3>输入组按钮</h3>
  <div class="input-group mb-3 mt-3">
    <button class="btn btn-outline-primary" type="button">基础按钮</button>
    <input type="text" class="form-control" placeholder="一些文本">
  </div>
  <div class="input-group mb-3">
    <input type="text" class="form-control" placeholder="搜索">
    <button class="btn btn-success" type="submit">Go</button>
  </div>
  <div class="input-group mb-3">
    <input type="text" class="form-control" placeholder="一些文本">
    <button class="btn btn-primary" type="button">OK</button>
    <button class="btn btn-danger" type="button">Cancel</button>
  </div>
</div>
</body>
</html>

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

带下拉按钮的输入组

在输入组中添加下拉按钮。请注意,您不需要像往常一样的 .dropdown 包装容器。

示例代码:

<div class="input-group mt-3 mb-3">
  <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">
    下拉按钮
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">链接 1</a></li>
    <li><a class="dropdown-item" href="#">链接 2</a></li>
    <li><a class="dropdown-item" href="#">链接 3</a></li>
  </ul>
  <input type="text" class="form-control" placeholder="用户名">
</div>

 

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

<!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">
  <h3>带下拉按钮的输入组</h3>
  <p>在输入组中添加下拉按钮。请注意,您不需要像往常一样的 .dropdown 包装器。</p>
  <div class="input-group mt-3 mb-3">
    <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">
      下拉按钮
    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">链接 1</a></li>
      <li><a class="dropdown-item" href="#">链接 2</a></li>
      <li><a class="dropdown-item" href="#">链接 3</a></li>
    </ul>
    <input type="text" class="form-control" placeholder="用户名">
  </div>
</div>
</body>
</html>

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

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

苏公网安备 32030202000762号

© 2021-2024