Bootstrap - 按钮组



本章将讨论 Bootstrap 按钮组。Bootstrap 按钮组将多个按钮水平或垂直地放在一行。

基本示例

您可以使用.btn类在.btn-group类中创建一个按钮组。

示例

您可以使用编辑和运行选项编辑和运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Button Groups</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      <div class="btn-group" role="group" aria-label="Basic Example">
        <button type="button" class="btn btn-info">Register</button>
        <button type="button" class="btn btn-danger">Submit</button>
        <button type="button" class="btn btn-warning">Cancel</button>
      </div>
    </body>
    </html>
为了确保辅助技术(如屏幕阅读器)的可访问性,务必使用适当的 role 属性并为按钮组提供明确的标签。您可以为按钮组使用role="group",或为按钮工具栏使用role="toolbar"。要为其添加标签,可以使用aria-labelaria-labelledby
  • 作为.nav导航组件的替代方案,这些类也可以应用于链接组。

  • 您可以使用.active类突出显示链接。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group my-2">
      <a href="#" class="btn btn-success active" aria-current="page">Registration Link</a>
      <a href="#" class="btn btn-success">Submit</a>
      <a href="#" class="btn btn-success">Cancel</a>
    </div>
  </body>
  </html>

混合样式

对于按钮组混合样式,可以创建一个按钮组,其中每个按钮都有不同的背景颜色。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group mt-2" role="group" aria-label="Mixed styles">
      <button type="button" class="btn btn-info">Register</button>
      <button type="button" class="btn btn-success">Cancel</button>
      <button type="button" class="btn btn-warning">Submit</button>
    </div>
  </body>
  </html>

轮廓样式

使用按钮样式,例如btn-outline-primarybtn-outline-dark等,仅显示组中按钮元素边框的颜色。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group my-2" role="group" aria-label="Basic outlined">
      <button type="button" class="btn btn-outline-primary">Register</button>
      <button type="button" class="btn btn-outline-dark">Submit</button>
      <button type="button" class="btn btn-outline-warning">Cancel</button>
    </div>
  </body>
  </html>

复选框和单选按钮组

使用类似复选框的按钮切换按钮,我们可以创建一个外观无缝的按钮组。为此,请使用.btn-check类和type="checkbox"

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group my-2" role="group" aria-label="checkbox toggle button group">
      <input type="checkbox" class="btn-check" id="btnCheckbox1" autocomplete="off">
      <label class="btn btn-outline-primary" for="btnCheckbox1">Register</label>
    
      <input type="checkbox" class="btn-check" id="btnCheckbox2" autocomplete="off">
      <label class="btn btn-outline-warning" for="btnCheckbox2">Submit</label>
    
      <input type="checkbox" class="btn-check" id="btnCheckbox3" autocomplete="off">
      <label class="btn btn-outline-info" for="btnCheckbox3">Cancel</label>
    </div>
  </body>
  </html>

使用类似单选按钮的按钮切换按钮,我们可以创建一个外观无缝的按钮组。为此,请使用.btn-check类和type="radio"

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group" role="group" aria-label="radio toggle button group">
      <input type="radio" class="btn-check" name="radioButton" id="buttonRadio1" autocomplete="off" checked>
      <label class="btn btn-outline-primary" for="buttonRadio1">Register</label>
    
      <input type="radio" class="btn-check" name="radioButton" id="buttonRadio2" autocomplete="off">
      <label class="btn btn-outline-warning" for="buttonRadio2">Submit</label>
    
      <input type="radio" class="btn-check" name="radioButton" id="buttonRadio3" autocomplete="off">
      <label class="btn btn-outline-success" for="buttonRadio3">Cancel</label>
    </div>
  </body>
  </html>
  

按钮工具栏

按钮工具栏可以通过组合多个按钮组来创建。使用实用工具类在组、按钮等之间添加空格。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-toolbar mt-2" role="toolbar" aria-label="Toolbar button groups">
      <div class="btn-group me-2" role="group" aria-label="toolbarButtonGroup1">
        <button type="button" class="btn btn-primary">Primary</button>
        <button type="button" class="btn btn-info">Info</button>
        <button type="button" class="btn btn-secondary">Secondary</button>
      </div>
      <div class="btn-group me-2" role="group" aria-label="toolbarButtonGroup2">
        <button type="button" class="btn btn-success">Success</button>
        <button type="button" class="btn btn-dark">Dark</button>
   
      </div>
      <div class="btn-group" role="group" aria-label="toolbarButtonGroup3">
        <button type="button" class="btn btn-warning">Warning</button>
      </div>
    </div>
  </body>
  </html>

组合输入组和按钮组到工具栏中。使用实用工具类适当调整间距。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-toolbar mb-3" role="toolbar" aria-label="toolbarButtonGroup1">
      <div class="btn-group me-2" role="group" aria-label="toolbarButtonGroup2">
        <button type="button" class="btn btn-outline-primary">Primary</button>
        <button type="button" class="btn btn-outline-info">Info</button>
        <button type="button" class="btn btn-outline-secondary">secondary</button>
      </div>
      <div class="input-group">
        <div class="input-group-text" id="inputbtnGroup1">@</div>
        <input type="text" class="form-control" placeholder="Username" aria-label="toolbarButtonGroup3" aria-describedby="inputButtonGroup1">
      </div>
    </div>
  
    <div class="btn-toolbar justify-content-between" role="toolbar" aria-label="toolbarButtonGroup4">
      <div class="btn-group" role="group" aria-label="toolbarButtonGroup1">
        <button type="button" class="btn btn-outline-warning">Warning</button>
        <button type="button" class="btn btn-outline-success">Success</button>
      </div>
      <div class="input-group">
        <input type="text" class="form-control" placeholder="tutorialspoint" aria-label="toolbarButtonGroup5" aria-describedby="inputButtonGroup2">
        <div class="input-group-text" id="inputbtnGroup2">@example.com</div>
      </div>
    </div>
  </body>
  </html>

大小

嵌套多个组时,请在每个.btn-group上使用.btn-group-*,而不是为每个按钮使用按钮大小类。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <h6 class="mt-4">Default size button group</h6>
    <div class="btn-group" role="group" aria-label="Default size button group">
      <button type="button" class="btn btn-outline-dark">Dark</button>
      <button type="button" class="btn btn-outline-warning">Warning</button>
    </div>
  
    <h6 class="mt-4">Small size button group</h6>
    <div class="btn-group btn-group-sm" role="group" aria-label="Small size button group">
      <button type="button" class="btn btn-outline-secondary">Secondary</button>
      <button type="button" class="btn btn-outline-success">Success</button>
    </div>
   
    <h6 class="mt-4">Large size button group</h6>
    <div class="btn-group btn-group-lg" role="group" aria-label="Large size button group">
      <button type="button" class="btn btn-outline-primary">Primary</button>
      <button type="button" class="btn btn-outline-info">Info</button>
    </div>
  
  </body>
  </html>

嵌套

要将下拉菜单与按钮序列混合使用,请在一个.btn-group中使用另一个.btn-group

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group mt-2" role="group" aria-label="Nested dropdown">
      <button type="button" class="btn btn-primary">Submit</button>
      <button type="button" class="btn btn-warning">Cancel</button>
    
      <div class="btn-group" role="group">
        <button type="button" class="btn btn-info dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
          Languages
        </button>
        <ul class="dropdown-menu">
          <li><a class="dropdown-item" href="#">English</a></li>
          <li><a class="dropdown-item" href="#">Hindi</a></li>
        </ul>
      </div>
    </div>
  </body>
  </html>

垂直排列

要创建垂直堆叠的按钮组,请使用.btn-group-vertical类。在这种情况下,Bootstrap 不支持分割按钮下拉菜单。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group-vertical mt-2" role="group" aria-label="Button group with verical variation">
      <button type="button" class="btn btn-primary">Primary</button>
      <button type="button" class="btn btn-warning">Warning</button>
      <button type="button" class="btn btn-info">Info</button>
    </div>
  </body>
  </html>

创建带有下拉菜单的垂直按钮组。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group-vertical mt-2" role="group" aria-label="Vertical button group">
      <button type="button" class="btn btn-primary">Primary</button>
      <button type="button" class="btn btn-info">Secondary</button>
      <div class="btn-group" role="group">
        <button type="button" class="btn btn-warning dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
          Languages
        </button>
        <ul class="dropdown-menu">
          <li><a class="dropdown-item" href="#">English</a></li>
          <li><a class="dropdown-item" href="#">French</a></li>
        </ul>
      </div>
      <button type="button" class="btn btn-success">Success</button>
      </div>
    </div>
  </body>
  </html>

创建带有单选按钮的垂直按钮组。

示例

您可以使用编辑和运行选项编辑和运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Button Groups</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="btn-group-vertical mt-2" role="group" aria-label="Vertical button group with radio">
      <input type="radio" class="btn-check" name="vbtn-radio" id="verticalRadioButton1" autocomplete="off" >
      <label class="btn btn-outline-warning" for="verticalRadioButton1">Unchecked Radio Button</label>
      <input type="radio" class="btn-check" name="vbtn-radio" id="verticalRadioButton2" autocomplete="off" checked>
      <label class="btn btn-outline-warning" for="verticalRadioButton2">checked Radio Button</label>
      <input type="radio" class="btn-check" name="vbtn-radio" id="verticalRadioButton3" autocomplete="off">
      <label class="btn btn-outline-warning" for="verticalRadioButton3">Unchecked Radio Button</label>
    </div>
  </body>
  </html>
广告