小码哥的IT人生

Bootstrap 5 表单浮动标签

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

Bootstrap 5 表单浮动标签

浮动标签/动画标签

默认情况下,使用标签(label)时,它们通常出现在输入字段的顶部:

通过使用浮动标签,您可以在输入字段内插入标签,并在单击输入字段时使它们浮动/动画化:

示例代码:

<div class="form-floating mb-3 mt-3">
  <input type="text" class="form-control" id="email" placeholder="请输入电邮地址" name="email">
  <label for="email">电邮</label>
</div>
<div class="form-floating mt-3 mb-3">
  <input type="text" class="form-control" id="pwd" placeholder="请输入密码" name="pswd">
  <label for="pwd">密码</label>
</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>
  <form action="/demo/demo_form.php">
    <div class="form-floating mb-3 mt-3">
      <input type="text" class="form-control" id="email" placeholder="请输入电邮地址" name="email">
      <label for="email">电邮</label>
    </div>
    <div class="form-floating mt-3 mb-3">
      <input type="text" class="form-control" id="pwd" placeholder="请输入密码" name="pswd">
      <label for="pwd">密码</label>
    </div>
    <button type="submit" class="btn btn-primary">提交</button>
  </form>
</div>
</body>
</html>

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

关于浮动标签的注意事项:

<label> 元素必须在 <input> 元素之后,并且每个 <input> 元素都需要 placeholder 属性(即使未显示)。

Textarea

也适用于文本域:

示例代码:

<div class="form-floating">
  <textarea class="form-control" id="comment" name="text" placeholder="Comment goes here"></textarea>
  <label for="comment">评论</label>
</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>浮动标签 - Textarea</h2>
  <p>请在 textarea 里面点击查看浮动标签效果:</p>
  <form action="/demo/demo_form.php">
    <div class="form-floating mb-3 mt-3">
      <textarea class="form-control" id="comment" name="text" placeholder="在此处评论">

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

选择菜单

您还可以在选择菜单上使用“浮动标签”。但是,它们不会浮动/动画化。标签将始终出现在左上角的选择菜单内:

示例代码:

<div class="form-floating">
  <select class="form-select" id="sel1" name="sellist">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
  </select>
  <label for="sel1" class="form-label">选择列表(选择一项):</label>
</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>
  <form action="/demo/demo_form.php">
    <div class="form-floating mb-3 mt-3">
      <select class="form-select" id="sel1" name="sellist">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
      </select>
      <label for="sel1" class="form-label">选择列表(选择一项):</label>
    </div>
    <button type="submit" class="btn btn-primary">提交</button>
  </form>
</div>
</body>
</html>

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

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

苏公网安备 32030202000762号

© 2021-2024