Bootstrap 5 按钮
Bootstrap 5 按钮
按钮样式
Bootstrap 5 提供了不同样式的按钮:
示例代码:
<button type="button" class="btn">基础</button> <button type="button" class="btn btn-primary">主要</button> <button type="button" class="btn btn-secondary">次要</button> <button type="button" class="btn btn-success">成功</button> <button type="button" class="btn btn-info">信息</button> <button type="button" class="btn btn-warning">警告</button> <button type="button" class="btn btn-danger">危险</button> <button type="button" class="btn btn-dark">深色</button> <button type="button" class="btn btn-light">浅色</button> <button type="button" class="btn btn-link">链接</button>
完整实例【亲自试一试】:
<!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> <button type="button" class="btn">基础</button> <button type="button" class="btn btn-primary">主要</button> <button type="button" class="btn btn-secondary">次要</button> <button type="button" class="btn btn-success">成功</button> <button type="button" class="btn btn-info">信息</button> <button type="button" class="btn btn-warning">警告</button> <button type="button" class="btn btn-danger">危险</button> <button type="button" class="btn btn-dark">深色</button> <button type="button" class="btn btn-light">浅色</button> <button type="button" class="btn btn-link">链接</button> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
按钮类可用于 <a>
、<button>
或 <input>
元素:
示例代码:
<a href="#" class="btn btn-success">链接按钮</a> <button type="button" class="btn btn-success">按钮</button> <input type="button" class="btn btn-success" value="输入按钮"> <input type="submit" class="btn btn-success" value="提交按钮"> <input type="reset" class="btn btn-success" value="重置按钮">
完整实例【亲自试一试】:
<!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>Button Elements</h2> <a href="#" class="btn btn-success">链接按钮</a> <button type="button" class="btn btn-success">按钮</button> <input type="button" class="btn btn-success" value="输入按钮"> <input type="submit" class="btn btn-success" value="提交按钮"> <input type="reset" class="btn btn-success" value="重置按钮"> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
为什么我们在链接的 href 属性中写一个 #?
由于我们没有任何可链接到的页面,而且我们不想收到 "404" 消息,因此我们将 # 作为链接。在现实生活中,它应是“搜索”页面的真实 URL。
按钮轮廓
Bootstrap 5 还提供了八个轮廓/边框按钮。
将鼠标移到它们上方,可看到额外的“悬停”效果:
示例代码:
<button type="button" class="btn btn-outline-primary">主要</button> <button type="button" class="btn btn-outline-secondary">次要</button> <button type="button" class="btn btn-outline-success">成功</button> <button type="button" class="btn btn-outline-info">信息</button> <button type="button" class="btn btn-outline-warning">警告</button> <button type="button" class="btn btn-outline-danger">危险</button> <button type="button" class="btn btn-outline-dark">深色</button> <button type="button" class="btn btn-outline-light text-dark">浅色</button>
完整实例【亲自试一试】:
<!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> <button type="button" class="btn btn-outline-primary">主要</button> <button type="button" class="btn btn-outline-secondary">次要</button> <button type="button" class="btn btn-outline-success">成功</button> <button type="button" class="btn btn-outline-info">信息</button> <button type="button" class="btn btn-outline-warning">警告</button> <button type="button" class="btn btn-outline-danger">危险</button> <button type="button" class="btn btn-outline-dark">深色</button> <button type="button" class="btn btn-outline-light text-dark">浅色</button> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
按钮尺寸
对大按钮使用 .btn-lg
类,对小按钮使用 .btn-sm
类:
示例代码:
<button type="button" class="btn btn-primary btn-lg">大型</button> <button type="button" class="btn btn-primary">默认</button> <button type="button" class="btn btn-primary btn-sm">小型</button>
完整实例【亲自试一试】:
<!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> <button type="button" class="btn btn-primary btn-lg">大型</button> <button type="button" class="btn btn-primary btn-md">默认</button> <button type="button" class="btn btn-primary btn-sm">小型</button> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
块级按钮
如需创建跨越父元素整个宽度的块级按钮,请在父元素上使用 .d-grid
"helper" 类:
示例代码:
<div class="d-grid"> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> </div>
完整实例【亲自试一试】:
<!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.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>如需创建跨越父元素整个宽度的块级按钮,请在父元素上使用 .d-grid "helper" 类:</p> <div class="d-grid"> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> </div> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
如果您有很多块级按钮,你可以用 .gap-*
类控制它们之间的间距:
示例代码:
<div class="d-grid gap-3"> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> </div>
完整实例【亲自试一试】:
<!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.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>如果您有很多块级按钮,你可以使用 .gap-* 类控制它们之间的间距:</p> <div class="d-grid gap-3"> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> <button type="button" class="btn btn-primary btn-block">全宽按钮</button> </div> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
活动/禁用按钮
按钮可以设置为活动(显示为被按下)或禁用(不可点击)状态:
类 .active
使按钮显示为被按下的状态,而 disabled
属性使按钮不可点击。请注意,<a> 元素不支持 disabled 属性,因此必须使用 .disabled
类使其在视觉上显示为禁用。
示例代码:
<button type="button" class="btn btn-primary active">Active Primary</button> <button type="button" class="btn btn-primary" disabled>Disabled Primary</button> <a href="#" class="btn btn-primary disabled">Disabled Link</a>
完整实例【亲自试一试】:
<!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.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> <button type="button" class="btn btn-primary">主要按钮</button> <button type="button" class="btn btn-primary active">活动的主要按钮</button> <button type="button" class="btn btn-primary" disabled>禁用的主要按钮</button> <a href="#" class="btn btn-primary disabled">禁用链接</a> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
加载器按钮
您还可以向按钮添加 "spinner",您将在我们的 BS5 Spinner 教程中学到更多内容:
示例代码:
<button class="btn btn-primary"> <span class="spinner-border spinner-border-sm"></span> </button> <button class="btn btn-primary"> <span class="spinner-border spinner-border-sm"></span> Loading.. </button> <button class="btn btn-primary" disabled> <span class="spinner-border spinner-border-sm"></span> Loading.. </button> <button class="btn btn-primary" disabled> <span class="spinner-grow spinner-grow-sm"></span> Loading.. </button>
完整实例【亲自试一试】:
<!DOCTYPE html> <html> <head> <title>Bootstrap Example</title> <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> <button class="btn btn-primary"> <span class="spinner-border spinner-border-sm"></span> </button> <button class="btn btn-primary"> <span class="spinner-border spinner-border-sm"></span> Loading.. </button> <button class="btn btn-primary" disabled> <span class="spinner-border spinner-border-sm"></span> Loading.. </button> <button class="btn btn-primary" disabled> <span class="spinner-grow spinner-grow-sm"></span> Loading.. </button> </div> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html