Bootstrap 5:范围


目录

    显示目录

定制范围

要设置范围菜单的样式,请将 .form-range 类添加到 type="range" 的输入元素:



例子

<label for="customRange" class="form-label">Custom range</label>
<input type="range" class="form-range" id="customRange">

自己尝试一下→

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-3">
  <h2>Custom Range</h2>
  <p>To create a custom range menu, add the .form-range class to the input element with type="range":</p>
  <form action="/action_page.php">
    <label for="customRange" class="form-label">Custom range</label>
    <input type="range" class="form-range" id="customRange" name="points">
    
    <button type="submit" class="btn btn-primary mt-3">Submit</button>
  </form>
</div>

</body>
</html>

脚步

默认情况下,范围数字之间的间隔为 1。您可以使用 step 属性更改它:

例子

<input type="range" class="form-range" step="10">

自己尝试一下→

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-3">
  <h2>Steps in a Range</h2>
  <p>By default, the interval between the range numbers is 1. You can change it by using the step attribute:</p>
  <form action="/action_page.php">
    <label for="customRange" class="form-label">Custom range</label>
    <input type="range" class="form-range" id="customRange" step="10" name="points">
    
    <button type="submit" class="btn btn-primary mt-3">Submit</button>
  </form>
</div>

</body>
</html>

最小值和最大值

默认情况下,最小值为 0,最大值为 100。您可以使用 min 和/或 max 属性改变它:

例子

<input type="range" class="form-range" min="0" max="4">

自己尝试一下 →

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-3">
  <h2>Min and Max Range</h2>
  <p>Use the min and/or max attribute to specify the minimum/maximum value of a range:</p>
  <form action="/action_page.php">
    <label for="customRange" class="form-label">Custom range</label>
    <input type="range" class="form-range" id="customRange" name="points" min="0" max="4">
    
    <button type="submit" class="btn btn-primary mt-3">Submit</button>
  </form>
</div>

</body>
</html>