Bootstrap5 按钮


按钮样式

Bootstrap 5 提供了不同样式的按钮:

示例

<button type="button" class="btn">Basic</button>
<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">Secondary</button>
<button type="button" class="btn btn-success">Success</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-danger">Danger</button>
<button type="button" class="btn btn-dark">Dark</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-link">Link</button>
亲自试一试 »

按钮类可用于<a>,<button>, 或者<input>要素:

示例

<a href="#" class="btn btn-success">Link Button</a>
<button type="button" class="btn btn-success">Button</button>
<input type="button" class="btn btn-success" value="Input Button">
<input type="submit" class="btn btn-success" value="Submit Button">
<input type="reset" class="btn btn-success" value="Reset Button">
亲自试一试 »

为什么我们在链接的 href 属性中放置#?

由于我们没有任何页面可以将其链接到,并且我们不想收到 "404" 消息,因此我们将 # 作为链接。在现实生活中,它当然应该是 "Search" 页面的真实 URL。


按钮轮廓

Bootstrap 5 还提供八个轮廓/边框按钮。

将鼠标移到它们上方可以查看额外的 "hover" 效果:

示例

<button type="button" class="btn btn-outline-primary">Primary</button>
<button type="button" class="btn btn-outline-secondary">Secondary</button>
<button type="button" class="btn btn-outline-success">Success</button>
<button type="button" class="btn btn-outline-info">Info</button>
<button type="button" class="btn btn-outline-warning">Warning</button>
<button type="button" class="btn btn-outline-danger">Danger</button>
<button type="button" class="btn btn-outline-dark">Dark</button>
<button type="button" class="btn btn-outline-light text-dark">Light</button>
亲自试一试 »


按钮尺寸

使用.btn-lg大按钮类或.btn-sm小按钮的类:

示例

<button type="button" class="btn btn-primary btn-lg">Large</button>
<button type="button" class="btn btn-primary">Default</button>
<button type="button" class="btn btn-primary btn-sm">Small</button>
亲自试一试 »

块级按钮

要创建跨越父元素整个宽度的块级按钮,请使用.d-grid父元素上的 "helper" 类:

示例

<div class="d-grid">
  <button type="button" class="btn btn-primary btn-block">Full-Width Button</button>
</div>
亲自试一试 »

如果您有许多块级按钮,您可以使用.gap-*类:

示例

<div class="d-grid gap-3">
  <button type="button" class="btn btn-primary btn-block">Full-Width Button</button>
  <button type="button" class="btn btn-primary btn-block">Full-Width Button</button>
  <button type="button" class="btn btn-primary btn-block">Full-Width Button</button>
</div>
亲自试一试 »

活动/禁用按钮

按钮可以设置为活动(显示为按下)或禁用(不可单击)状态:

班上.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>
亲自试一试 »

微调按钮

您还可以将 "spinners" 添加到按钮,您将在我们的BS5 加载器教程:

示例

<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>
亲自试一试 »