Bootstrap 按钮组

按钮组

Bootstrap 让您可以将一系列按钮组合在一个按钮组中(在一行上):

使用一个带有 .btn-group 样式的 <div> 元素来创建一个按钮组:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>Button 组</h2>
  12. <p>.btn-group 样式类创建一个按钮组:</p>
  13. <div class="btn-group">
  14. <button type="button" class="btn btn-primary">Apple</button>
  15. <button type="button" class="btn btn-primary">Samsung</button>
  16. <button type="button" class="btn btn-primary">Sony</button>
  17. </div>
  18. </div>
  19. </body>
  20. </html>

提示:不要将按钮大小样式应用于按钮组中的每个按钮,而是使用 .btn-group-lg|sm|xs 设置组中所有按钮的大小:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>Button 组 - 设置大小</h2>
  12. <p>使用 <code class="marked">.btn-group-lg|sm|xs</code> 设置组中所有按钮的大小</p>
  13. <h3>大按钮:</h3>
  14. <div class="btn-group btn-group-lg">
  15. <button type="button" class="btn btn-primary">Apple</button>
  16. <button type="button" class="btn btn-primary">Samsung</button>
  17. <button type="button" class="btn btn-primary">Sony</button>
  18. </div>
  19. <h3>默认按钮:</h3>
  20. <div class="btn-group">
  21. <button type="button" class="btn btn-primary">Apple</button>
  22. <button type="button" class="btn btn-primary">Samsung</button>
  23. <button type="button" class="btn btn-primary">Sony</button>
  24. </div>
  25. <h3>小按钮:</h3>
  26. <div class="btn-group btn-group-sm">
  27. <button type="button" class="btn btn-primary">Apple</button>
  28. <button type="button" class="btn btn-primary">Samsung</button>
  29. <button type="button" class="btn btn-primary">Sony</button>
  30. </div>
  31. <h3>非常小的按钮:</h3>
  32. <div class="btn-group btn-group-xs">
  33. <button type="button" class="btn btn-primary">Apple</button>
  34. <button type="button" class="btn btn-primary">Samsung</button>
  35. <button type="button" class="btn btn-primary">Sony</button>
  36. </div>
  37. </div>
  38. </body>
  39. </html>

垂直按钮组

Bootstrap 也支持垂直按钮组:

使用 .btn-group-vertical 来创建一个垂直按钮组:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>垂直按钮组</h2>
  12. <p>使用 .btn-group-vertical 来创建一个垂直按钮组:</p>
  13. <div class="btn-group-vertical">
  14. <button type="button" class="btn btn-primary">Apple</button>
  15. <button type="button" class="btn btn-primary">Samsung</button>
  16. <button type="button" class="btn btn-primary">Sony</button>
  17. </div>
  18. </div>
  19. </body>
  20. </html>

两端对齐的按钮组

要撑满屏幕的整个宽度,请使用 .btn-group-justified 样式:

使用 <a> 元素的实例:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>两端对齐的按钮组</h2>
  12. <p>要撑满屏幕的整个宽度,请使用 .btn-group-justified 样式:</p>
  13. <div class="btn-group btn-group-justified">
  14. <a href="#" class="btn btn-primary">Apple</a>
  15. <a href="#" class="btn btn-primary">Samsung</a>
  16. <a href="#" class="btn btn-primary">Sony</a>
  17. </div>
  18. </div>
  19. </body>
  20. </html>

注意:对于 <button> 元素,必须将每个按钮都用一个 .btn-group 样式包裹

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>两端对齐的按钮组</h2>
  12. <div class="btn-group btn-group-justified">
  13. <div class="btn-group">
  14. <button type="button" class="btn btn-primary">Apple</button>
  15. </div>
  16. <div class="btn-group">
  17. <button type="button" class="btn btn-primary">Samsung</button>
  18. </div>
  19. <div class="btn-group">
  20. <button type="button" class="btn btn-primary">Sony</button>
  21. </div>
  22. </div>
  23. </div>
  24. </body>
  25. </html>

嵌套按钮组 & 下拉目录

嵌套按钮组创建下拉菜单:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>嵌套按钮组</h2>
  12. <p>嵌套按钮组创建下拉菜单:</p>
  13. <div class="btn-group">
  14. <button type="button" class="btn btn-primary">Apple</button>
  15. <button type="button" class="btn btn-primary">Samsung</button>
  16. <div class="btn-group">
  17. <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
  18. Sony <span class="caret"></span></button>
  19. <ul class="dropdown-menu" role="menu">
  20. <li><a href="#">Tablet</a></li>
  21. <li><a href="#">Smartphone</a></li>
  22. </ul>
  23. </div>
  24. </div>
  25. </div>
  26. </body>
  27. </html>

按钮下拉列表

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bootstrap 实例</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h2>按钮下拉列表</h2>
  12. <div class="btn-group">
  13. <button type="button" class="btn btn-primary">Sony</button>
  14. <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
  15. <span class="caret"></span>
  16. </button>
  17. <ul class="dropdown-menu" role="menu">
  18. <li><a href="#">Tablet</a></li>
  19. <li><a href="#">Smartphone</a></li>
  20. </ul>
  21. </div>
  22. </div>
  23. </body>
  24. </html>