入门基础
引入 Bootstrap
通过 CDN 引入 Bootstrap 可以快速开始——CSS 放在 <head> 中,JS 包(包含 Popper)放在 </body> 之前。bundle.min.js 包含 Popper,用于工具提示/下拉菜单/弹出框。使用 npm 时,在入口文件中导入 CSS 和 JS。Bootstrap 5 移除了 jQuery 依赖——所有组件都使用原生 JavaScript 工作。
<!-- CSS only (in <head>) -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- JS bundle (before </body>) -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- or install via npm -->
<!-- npm install [email protected] -->
<!-- import in JS -->
// import 'bootstrap/dist/css/bootstrap.min.css'
// import 'bootstrap' // imports all JS容器
容器是最基本的布局元素——它们居中内容并添加水平内边距。.container 在每个断点处为固定宽度(max-width 变化);.container-fluid 始终为 100% 宽度。响应式容器(container-sm/md/lg/xl/xxl)在指定断点之前为 100% 宽度,之后变为固定宽度。大多数布局使用 .container;全宽应用使用 .container-fluid。
<!-- fixed-width container (responsive breakpoints) -->
<div class="container">
<!-- content centered with max-width -->
</div>
<!-- full-width container -->
<div class="container-fluid">
<!-- spans entire viewport width -->
</div>
<!-- responsive containers (max-width at breakpoint) -->
<div class="container-sm"> <!-- 100% until sm, then fixed -->
<div class="container-md">
<div class="container-xl">
<!-- 100% wide until the breakpoint, then max-width applied -->
<div class="container-xxl">响应式断点
Bootstrap 使用移动优先的 min-width 媒体查询系统。共有 6 个断点:xs(默认)、sm(576px)、md(768px)、lg(992px)、xl(1200px)、xxl(1400px)。col-md-6 等类在 md 及以上断点生效。要在断点处显示/隐藏元素,使用 d-none(display:none)和 d-{breakpoint}-block。这种移动优先方法意味着先为小屏幕设计,再为大屏幕添加复杂性。
/* Bootstrap 5 breakpoints (min-width, mobile-first) */
/* xs: <576px (default, no media query needed) */
/* sm: >=576px */
/* md: >=768px */
/* lg: >=992px */
/* xl: >=1200px */
/* xxl: >=1400px */
<!-- classes apply AT and ABOVE the breakpoint -->
<div class="col-12 col-md-6 col-lg-4">
<!-- full width on mobile, half on md, third on lg -->
</div>
<!-- hidden/visible at breakpoints -->
<div class="d-none d-md-block"> <!-- hidden below md -->
<div class="d-block d-md-none"> <!-- visible below md only -->Reboot 与基础样式
Reboot 是 Bootstrap 的 CSS 重置——它使用 rem 单位(根 em,基于 html 的 16px 字体大小)规范化浏览器样式。所有元素都设置了 box-sizing: border-box。标题、段落、列表、表格和链接都有合理的默认值。Reboot 移除了标题和列表的上边距,将其设为 0。这在 Bootstrap 组件层叠之前提供了跨浏览器的一致基线。
<!-- Reboot normalizes styles: box-sizing, margins, etc. -->
<!-- body gets: font-family, font-size (1rem), line-height (1.5), color -->
<!-- headings use rem units, margin-top removed -->
<h1>Heading 1 (2.5rem)</h1>
<h2>Heading 2 (2rem)</h2>
<!-- links get color and underline on hover -->
<a href="#">Default link</a>
<!-- tables get basic styling -->
<table class="table">...</table>
<!-- <ul>/<ol> have padding-left removed -->
<ul>
<li>Item</li>
</ul>使用 CSS 变量自定义
Bootstrap 5 暴露了数百个以 --bs- 为前缀的 CSS 自定义属性(变量)。在 :root 中覆盖它们可以全局主题化你的网站。深色模式通过 <html> 元素上的 data-bs-theme='dark' 内置支持,它会交换一组变量。这使得主题化比旧的 Sass 变量方法容易得多——你可以用纯 CSS 更改颜色、字体、间距和圆角,无需构建步骤。
:root {
/* override Bootstrap's CSS variables */
--bs-primary: #0d6efd;
--bs-body-bg: #ffffff;
--bs-body-color: #212529;
--bs-font-sans-serif: 'Inter', system-ui, sans-serif;
--bs-border-radius: 0.375rem;
}
/* dark mode via data attribute */
[data-bs-theme="dark"] {
--bs-body-bg: #212529;
--bs-body-color: #f8f9fa;
}
<!-- use in HTML -->
<div style="background: var(--bs-primary); color: var(--bs-white);">
Custom styled
</div>布局基础
行与列
.row 是一个 flex 容器(display: flex),带有负水平边距以抵消列内边距。直接的 .col 子元素等分填充行。指定 col-{1-12} 获得固定比例(共 12 列)。不带数字时,.col 取相等的剩余空间。12 列网格是 Bootstrap 布局的核心——组合 col-{n} 和自动 .col 实现灵活布局。
<!-- .row creates a flex container with negative margins -->
<!-- .col children flex equally -->
<div class="container">
<div class="row">
<div class="col">Column 1</div>
<div class="col">Column 2</div>
<div class="col">Column 3</div>
</div>
<!-- 3 equal columns -->
</div>
<!-- sized columns -->
<div class="row">
<div class="col-8">8/12 width</div>
<div class="col-4">4/12 width</div>
</div>
<!-- auto + sized -->
<div class="row">
<div class="col">auto width (content-based)</div>
<div class="col-6">exactly half</div>
<div class="col">auto width</div>
</div>间距(列间距)
间距(Gutters)是列之间的内边距,由 g-* 类控制(g-0 到 g-5)。g-0 移除所有间距(边缘到边缘的列)。g-3 是默认值。间距在水平(列之间)和垂直(列换行时行之间)都起作用。使用响应式变体(g-2 g-md-4)在不同断点获得不同间距。间距系统取代了旧的仅水平内边距方法。
<!-- default gutters (1.5rem padding on each side of columns) -->
<div class="row">
<div class="col-6">A</div>
<div class="col-6">B</div>
</div>
<!-- remove gutters -->
<div class="row g-0">
<div class="col-6">A</div>
<div class="col-6">B</div>
</div>
<!-- custom gutter sizes: g-0 to g-5 -->
<div class="row g-3">
<div class="col-6">A</div>
<div class="col-6">B</div>
</div>
<!-- responsive gutters -->
<div class="row g-2 g-md-4">
<div class="col-6">A</div>
<div class="col-6">B</div>
</div>
<!-- vertical gutters (row spacing) -->
<div class="row g-3">
<div class="col-12">Row 1</div>
<div class="col-12">Row 2 (g-3 gap above)</div>
</div>列换行与对齐
当列总数超过 12 时,列会换行到新行。使用行上的 align-items-* 控制垂直对齐(start/center/end)。使用 justify-content-* 控制水平分布(start/center/end/between/around/evenly)。这些底层使用 flexbox。换行是自动的——无需显式的行分隔。这使得仅用 col-* 类就能轻松实现类似瀑布流的布局。
<!-- columns wrap when they exceed 12 total -->
<div class="row">
<div class="col-9">9 cols</div>
<div class="col-4">4 cols → wraps to next line (9+4=13)</div>
<div class="col-6">6 cols (new line)</div>
</div>
<!-- vertical alignment -->
<div class="row align-items-center">
<div class="col">Vertically centered</div>
</div>
<!-- options: align-items-start, -center, -end -->
<div class="row align-items-end">...</div>
<!-- horizontal alignment -->
<div class="row justify-content-center">
<div class="col-6">Centered horizontally</div>
</div>
<!-- options: justify-content-start, -center, -end, -between, -around, -evenly -->列排序
排序类在不改变 HTML 的情况下视觉重排列。order-first、order-{1-5}、order-last 提供 7 个级别。使用响应式变体(order-md-first)在断点处改变顺序——对 SEO 很有用(重要内容在 HTML 中靠前,但在移动端显示不同)。偏移量(offset-md-4)添加左边距将列向右推——用于居中或缩进。两者都依赖 flexbox 的 order 和 margin 工具类。
<!-- order classes: order-first, order-{1-5}, order-last -->
<div class="row">
<div class="col order-2">First in HTML, second visually</div>
<div class="col order-1">Second in HTML, first visually</div>
<div class="col order-3">Third in both</div>
</div>
<!-- responsive ordering -->
<div class="row">
<div class="col order-last order-md-first">
Last on mobile, first on desktop
</div>
<div class="col">Normal</div>
</div>
<!-- column offset (push right) -->
<div class="row">
<div class="col-md-4 offset-md-4">Centered (offset 4)</div>
</div>
<!-- offset responsive -->
<div class="col-md-4 offset-md-2 offset-lg-4">...</div>Z-Index 与层叠
Bootstrap 使用 z-index 比例进行层叠:z-1、z-2、z-3(以及负值 z-n0、z-n1、z-n2)。模态框、下拉菜单和工具提示等组件具有更高的内置 z-index 值(1000-1080)。要让 z-index 生效,元素需要 position(relative、absolute、fixed 或 sticky)。避免使用任意 z-index 值——使用 Bootstrap 的比例保持一致性。层叠顺序设计为模态框始终在下拉菜单之上,下拉菜单在导航栏之上。
/* Bootstrap's z-index scale */
/* z-1 to z-3, plus z-n0, z-n1, z-n2 for negatives */
<div class="z-3 position-relative">High z-index</div>
<div class="z-1 position-relative">Low z-index</div>
<!-- components have built-in z-index: -->
/* dropdown: 1000 */
/* sticky: 1020 */
/* fixed: 1030 */
/* modal-backdrop: 1050 */
/* modal: 1055 */
/* popover: 1070 */
/* tooltip: 1080 */
<!-- use position utilities for stacking context -->
<div class="position-relative z-3">
Needs position for z-index to apply
</div>网格系统
基础网格
Bootstrap 的网格是一个 12 列系统。col-{1-12} 指定元素跨越的 12 列中的多少列。col-12 = 全宽,col-6 = 一半,col-4 = 三分之一,col-3 = 四分之一。行内列的总和应为 12(或更少——剩余空间会被分配)。不带数字时,.col 等分剩余空间。网格使用 flexbox——行是 flex 容器,列是 flex 项目。
<!-- 12-column grid -->
<div class="container">
<div class="row">
<div class="col-12">Full width (12/12)</div>
</div>
<div class="row">
<div class="col-6">Half (6/12)</div>
<div class="col-6">Half (6/12)</div>
</div>
<div class="row">
<div class="col-4">Third (4/12)</div>
<div class="col-4">Third (4/12)</div>
<div class="col-4">Third (4/12)</div>
</div>
<div class="row">
<div class="col-3">Quarter (3/12)</div>
<div class="col-9">Three-quarters (9/12)</div>
</div>
</div>响应式列
响应式列类(col-sm-*、col-md-*、col-lg-*、col-xl-*、col-xxl-*)在该断点及之上生效(移动优先)。最小的适用类优先。col-12 col-md-6 col-lg-4 表示:移动端全宽,平板端一半,桌面端三分之一。row-cols-* 设置每个断点每行等宽列的数量——当所有列宽度相同时,比在每个子元素上指定 col-* 更简洁。
<!-- different widths at different breakpoints -->
<div class="row">
<!-- full on mobile, half on md, third on lg -->
<div class="col-12 col-md-6 col-lg-4">Card 1</div>
<div class="col-12 col-md-6 col-lg-4">Card 2</div>
<div class="col-12 col-md-6 col-lg-4">Card 3</div>
</div>
<!-- mobile-first: base class applies to all, -->
<!-- larger breakpoints override -->
<div class="col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">
1 per row on xs, 2 on sm, 3 on md, 4 on lg, 6 on xl
</div>
<!-- equal-width columns at breakpoint -->
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3">
<div class="col">Auto-width card</div>
<div class="col">Auto-width card</div>
<div class="col">Auto-width card</div>
</div>嵌套列
要嵌套网格,在 .col 内放置 .row——嵌套的行在父列宽度内创建新的 12 列上下文。嵌套列(col-4、col-8)分割父元素宽度,而非整个页面。嵌套对于带子部分的复杂布局(如侧边栏)必不可少。保持嵌套较浅(最多 2-3 层)以避免列过窄并保持可读性。
<!-- nest a row inside a column -->
<div class="row">
<div class="col-6">
<!-- outer column -->
<h3>Left half</h3>
<!-- nested row: new 12-column context -->
<div class="row">
<div class="col-4">Nested 1/3 of parent</div>
<div class="col-8">Nested 2/3 of parent</div>
</div>
</div>
<div class="col-6">
<h3>Right half</h3>
</div>
</div>
<!-- the nested row creates a new 12-col grid -->
<!-- within the parent column's width -->列偏移与边距
offset-{breakpoint}-{n} 添加 n 列的左边距,用于居中或缩进。col-md-4 上的 offset-md-4 使其居中(4 + 4 + 4 = 12)。更简单的居中方式是使用 mx-auto(margin: 0 auto),无论列数多少都有效。响应式偏移(offset-sm-2 offset-md-0)允许在移动端缩进但在桌面端不缩进。偏移比空占位列更简洁。
<!-- offset: add left margin (empty columns) -->
<div class="row">
<div class="col-md-4 offset-md-4">
Centered (4 + 4 offset each side = 12)
</div>
</div>
<!-- offset only on one side -->
<div class="row">
<div class="col-md-6 offset-md-3">
Centered with more left space
</div>
</div>
<!-- responsive offset -->
<div class="row">
<div class="col-sm-8 offset-sm-2 offset-md-0 col-md-12">
Offset on mobile, none on desktop
</div>
</div>
<!-- margin utilities for auto centering -->
<div class="row">
<div class="col-md-6 mx-auto">
Auto-centered (margin: 0 auto)
</div>
</div>行列与自动布局
row-cols-{n} 设置每行等宽列的数量——当所有列宽度相同时,比在每个子元素上添加 col-* 更简洁。row-cols-2 表示每行 2 列;项目自动换行。响应式变体(row-cols-1 row-cols-md-3)在断点处改变数量。普通 .col(无数字)使列等分空间。卡片网格使用 row-cols;需要特定比例时使用 col-{n}。
<!-- row-cols: set number of columns per row -->
<div class="row row-cols-2">
<div class="col">Item 1</div>
<div class="col">Item 2</div>
<div class="col">Item 3</div> <!-- wraps to new row -->
<div class="col">Item 4</div>
</div>
<!-- 2 per row automatically -->
<!-- responsive row-cols -->
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4">
<div class="col">Card</div>
<div class="col">Card</div>
<div class="col">Card</div>
<div class="col">Card</div>
</div>
<!-- auto-fit columns: .col without number -->
<div class="row">
<div class="col">Equal</div>
<div class="col">Equal</div>
<div class="col">Equal</div>
</div>
<!-- each takes 1/3, regardless of content -->排版
标题与展示标题
Bootstrap 使用基于 rem 的尺寸样式化 h1-h6(h1 = 2.5rem,h6 = 1rem)。.h1-.h6 类将标题样式应用于任何元素。展示标题(display-1 到 display-6)更大更细——用于英雄区域。.lead 使段落稍大更轻——通常用于第一段。标题默认没有上边距(Reboot 移除了它)以避免布局间隙。
<!-- standard headings (h1-h6) -->
<h1>h1 heading</h1>
<h2>h2 heading</h2>
<h3>h3 heading</h3>
<!-- heading classes for matching styles -->
<p class="h1">Looks like h1</p>
<p class="h2">Looks like h2</p>
<!-- display headings (larger, thinner) -->
<h1 class="display-1">Display 1</h1>
<h1 class="display-2">Display 2</h1>
<h1 class="display-3">Display 3</h1>
<h1 class="display-4">Display 4</h1>
<h1 class="display-5">Display 5</h1>
<h1 class="display-6">Display 6</h1>
<!-- lead paragraph -->
<p class="lead">This is a lead paragraph.</p>行内文本元素
Bootstrap 样式化语义 HTML 元素:<mark>(高亮)、<del>/<s>(删除线)、<ins>/<u>(下划线)、<small>(小字)。文本工具类提供样式:text-uppercase/lowercase/capitalize、fw-bold/light/normal、fst-italic、text-muted(灰色)。text-decoration-underline/line-through 控制下划线。这些工具类优于内联样式以保持一致性。
<!-- text styling -->
<p>You can use <mark>highlight</mark> text.</p>
<p><del>Deleted</del> and <s>strikethrough</s> text.</p>
<p><ins>Inserted</ins> and <u>underline</u> text.</p>
<p><small>Small text</small> for fine print.</p>
<p><strong>Bold</strong> and <em>italic</em> text.</p>
<!-- text utilities -->
<p class="text-decoration-underline">Underlined</p>
<p class="text-decoration-line-through">Line through</p>
<p class="text-lowercase">lowercase text</p>
<p class="text-uppercase">UPPERCASE TEXT</p>
<p class="text-capitalize">Capitalized Text</p>
<p class="fw-bold">Font weight bold</p>
<p class="fw-light">Font weight light</p>
<p class="fst-italic">Font style italic</p>
<p class="text-muted">Muted/gray text</p>列表
list-unstyled 移除默认列表样式(项目符号和左内边距)——用于导航菜单。list-inline 使列表项内联显示(用于标签云、面包屑)。描述列表(dl/dt/dd)通过 row 类获得水平布局——dt 在左,dd 在右,通过网格对齐。Bootstrap 中的列表有 margin-top: 0 和 margin-bottom: 1rem 以保持一致的间距。
<!-- unordered list -->
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<!-- unstyled list (no bullets, no padding) -->
<ul class="list-unstyled">
<li>No bullets</li>
<li>No left padding</li>
</ul>
<!-- inline list -->
<ul class="list-inline">
<li class="list-inline-item">Inline 1</li>
<li class="list-inline-item">Inline 2</li>
<li class="list-inline-item">Inline 3</li>
</ul>
<!-- description list -->
<dl>
<dt>Term</dt>
<dd>Definition</dd>
</dl>
<!-- horizontal description list -->
<dl class="row">
<dt class="col-sm-3">Term</dt>
<dd class="col-sm-9">Definition</dd>
</dl>引用与代码
引用使用 <figure>/<blockquote>/<figcaption> 结构以获得语义。blockquote-footer 样式化引用来源。代码样式:<code> 用于行内代码,<pre><code> 用于代码块,<kbd> 用于键盘按键,<var> 用于变量。代码中的尖括号始终要 HTML 转义(< 用 <)。引用可以用 text-center/text-end 对齐。kbd 样式使按键具有键盘式外观。
<!-- blockquote with source -->
<figure>
<blockquote class="blockquote">
<p>A well-known quote.</p>
</blockquote>
<figcaption class="blockquote-footer">
Someone famous in <cite title="Source Title">Source Title</cite>
</figcaption>
</figure>
<!-- blockquote alignment -->
<blockquote class="blockquote text-center">Centered</blockquote>
<blockquote class="blockquote text-end">Right-aligned</blockquote>
<!-- inline code -->
<p>Use <code><section></code> tag.</p>
<!-- code block -->
<pre><code><div class="container">
Hello
</div>
</code></pre>
<!-- variables -->
<var>y</var> = <var>mx</var> + <var>b</var>
<!-- keyboard input -->
<kbd>Ctrl</kbd> + <kbd>C</kbd>文本对齐与换行
文本对齐:text-start/center/end(Bootstrap 5 将 left/right 重命名为 start/end 以支持 RTL)。响应式变体(text-md-start)在断点处改变对齐。text-nowrap 防止换行;text-truncate 为溢出添加省略号(需要 max-width 或受限容器)。text-break 断开长单词。lh-* 控制行高。这些工具类无需自定义 CSS 即可处理文本流。
<!-- text alignment -->
<p class="text-start">Left aligned (default)</p>
<p class="text-center">Center aligned</p>
<p class="text-end">Right aligned</p>
<!-- responsive alignment -->
<p class="text-center text-md-start">
Center on mobile, left on md+
</p>
<!-- text wrapping -->
<div class="text-wrap">This text wraps.</div>
<div class="text-nowrap">This text doesn't wrap.</div>
<!-- truncate with ellipsis -->
<div class="text-truncate" style="max-width: 200px;">
This long text will be truncated with an ellipsis...
</div>
<!-- word break -->
<p class="text-break">verylongwordwithoutspaces</p>
<!-- line height -->
<p class="lh-1">Line height 1</p>
<p class="lh-sm">Line height small</p>
<p class="lh-base">Line height base (default)</p>
<p class="lh-lg">Line height large</p>表格
基础表格
.table 类添加内边距、边框和水平分隔线。表头使用 <thead>,数据使用 <tbody>。表头 <th> 上的 scope='col' 和行 <th> 上的 scope='row' 提高了屏幕阅读器的可访问性。表格是可选的——没有 .table,你会得到无样式的 HTML 表格。始终使用语义表格元素(thead、tbody、th、tr、td)以确保可访问性。
<!-- basic table with styling -->
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Alice</td>
<td>[email protected]</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Bob</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>表格变体与条纹
表格修饰符:table-striped(交替行颜色)、table-bordered(所有边框)、table-borderless(无边框)、table-hover(悬停行高亮)、table-sm(紧凑内边距)、table-dark(深色主题)。这些可以组合使用:table table-striped table-hover。深色变体反转颜色用于深色背景。table-sm 将单元格内边距减半以获得更密集的数据显示。
<!-- striped rows -->
<table class="table table-striped">
<!-- alternating background colors -->
</table>
<!-- bordered table -->
<table class="table table-bordered">
<!-- borders on all sides -->
</table>
<!-- borderless table -->
<table class="table table-borderless">
<!-- no borders -->
</table>
<!-- hoverable rows -->
<table class="table table-hover">
<!-- row highlights on hover -->
</table>
<!-- small table -->
<table class="table table-sm">
<!-- condensed padding -->
</table>
<!-- dark table -->
<table class="table table-dark">
<!-- dark background -->
</table>表格颜色
将上下文类应用于 <tr> 或 <td>:table-primary、table-success、table-danger、table-warning、table-info、table-secondary、table-light、table-dark、table-active。这些添加了细微的背景色调用于状态指示。在深色表格上,相同的类提供较浅的变体。少量使用以表示有意义的状态(success 表示完成,danger 表示错误)而非装饰。
<!-- contextual row/cell colors -->
<table class="table">
<thead>
<tr class="table-primary"><th>Primary header</th></tr>
</thead>
<tbody>
<tr class="table-success">
<td>Success row (green)</td>
</tr>
<tr class="table-danger">
<td>Danger row (red)</td>
</tr>
<tr class="table-warning">
<td>Warning row (yellow)</td>
</tr>
<tr class="table-info">
<td>Info row (cyan)</td>
</tr>
<!-- individual cells -->
<tr>
<td class="table-active">Active cell</td>
<td>Normal cell</td>
</tr>
</tbody>
</table>响应式表格
将表格包装在 .table-responsive 中以在小屏幕上启用水平滚动(防止布局破坏)。table-responsive-sm/md/lg/xl/xxl 仅在该断点以下滚动——表格在大屏幕上正常显示。没有响应式包装,宽表格会溢出其容器。包装 div 是必需的,因为 overflow 不能直接应用于 <table>。对于多列表格始终使用此功能。
<!-- responsive table (horizontal scroll on small screens) -->
<div class="table-responsive">
<table class="table">
<!-- scrolls horizontally on small screens -->
<thead>...</thead>
<tbody>...</tbody>
</table>
</div>
<!-- responsive at specific breakpoints -->
<div class="table-responsive-sm">
<!-- scrolls below sm -->
</div>
<div class="table-responsive-md">
<!-- scrolls below md -->
</div>
<div class="table-responsive-lg">
<!-- scrolls below lg -->
</div>
<!-- always scrollable -->
<div class="table-responsive">...</div>表格组与标题
caption-top 将标题移到表格上方(默认在下方)。标题提高了屏幕阅读器的可访问性。用 table-light/table-dark 分别样式化 thead/tbody/tfoot。垂直对齐:align-top、align-middle、align-bottom 应用于单元格或行——当单元格内容高度不同时很有用。tfoot 元素默认样式类似 thead 但可自定义。
<!-- caption (accessibility, shown below table by default) -->
<table class="table caption-top">
<caption>List of users</caption>
<thead>
<tr><th>#</th><th>Name</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>Alice</td></tr>
</tbody>
</table>
<!-- table groups for styling -->
<table class="table">
<thead class="table-light">
<tr><th>Light header</th></tr>
</thead>
<tbody>
<tr><td>Body</td></tr>
</tbody>
<tfoot class="table-dark">
<tr><td>Dark footer</td></tr>
</tfoot>
</table>
<!-- vertically aligned cells -->
<tr class="align-middle">
<td class="align-top">Top</td>
<td class="align-middle">Middle</td>
<td class="align-bottom">Bottom</td>
</tr>表单
表单控件
form-control 样式化文本输入框、文本域和文件输入。form-select 样式化下拉菜单。始终将输入框与 <label> 元素配对(for/id 匹配)以确保可访问性。form-label 添加适当的间距。disabled 防止交互;readonly 显示值但不可编辑。mb-3(margin-bottom)在表单组之间添加间距。.form-control 类应用一致的高度、内边距和焦点样式。
<!-- text input -->
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter email">
</div>
<!-- textarea -->
<div class="mb-3">
<label for="bio" class="form-label">Bio</label>
<textarea class="form-control" id="bio" rows="3"></textarea>
</div>
<!-- select -->
<select class="form-select" aria-label="Default select">
<option selected>Open this select menu</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<!-- disabled -->
<input class="form-control" type="text" placeholder="Disabled" disabled>
<input class="form-control" type="text" value="Readonly" readonly>表单网格与布局
使用网格系统(row/col)布局表单。对于水平表单,将 col-form-label 与 col-* 配合使用以对齐标签/输入。row-cols-lg-auto 使表单元素在大屏幕上按内容尺寸调整(内联表单)。align-items-center 垂直对齐标签和输入。网格在不同断点精确控制表单布局,无需自定义 CSS。
<!-- form using grid -->
<div class="row mb-3">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name">
</div>
</div>
<!-- inline form -->
<form class="row row-cols-lg-auto g-3 align-items-center">
<div class="col">
<input class="form-control" placeholder="Email">
</div>
<div class="col">
<input class="form-control" placeholder="Password">
</div>
<div class="col">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</form>
<!-- horizontal form with column sizing -->
<form>
<div class="row mb-3">
<label class="col-2 col-form-label">Email</label>
<div class="col-10">
<input class="form-control" type="email">
</div>
</div>
</form>输入组
输入组使用 input-group-text 在输入框前后添加文本/按钮。常用于 @ 用户名、$ 货币、.00 小数或搜索按钮。input-group 包装器使附加内容和输入框显示为一个连接的控件。多个输入框可以共享一个附加内容。输入组内的按钮使用 btn-outline-* 获得连接的外观。不要将输入组与 form-floating 混用。
<!-- prepend text -->
<div class="input-group mb-3">
<span class="input-group-text">@</span>
<input type="text" class="form-control" placeholder="Username">
</div>
<!-- append text -->
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Recipient's username">
<span class="input-group-text">@example.com</span>
</div>
<!-- prepend + append -->
<div class="input-group mb-3">
<span class="input-group-text">$</span>
<input type="text" class="form-control">
<span class="input-group-text">.00</span>
</div>
<!-- with button -->
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Search">
<button class="btn btn-outline-secondary" type="button">Go</button>
</div>
<!-- multiple inputs -->
<div class="input-group">
<span class="input-group-text">First and last name</span>
<input type="text" class="form-control">
<input type="text" class="form-control">
</div>浮动标签
浮动标签在字段获得焦点或有内容时,将标签从输入框内部动画移动到上方。HTML 中标签必须在输入框之后。placeholder 属性是必需的(即使为空)才能实现浮动行为。此模式节省空间且看起来现代。不要与 input-group 组合使用。浮动标签适用于输入框、文本域和选择框。它们是传统标签的简洁替代方案,适合紧凑表单。
<!-- floating label (label floats up when focused/filled) -->
<div class="form-floating mb-3">
<input type="email" class="form-control" id="floatingInput" placeholder="[email protected]">
<label for="floatingInput">Email address</label>
</div>
<!-- floating textarea -->
<div class="form-floating">
<textarea class="form-control" id="floatingTextarea" placeholder="Leave a comment"></textarea>
<label for="floatingTextarea">Comments</label>
</div>
<!-- floating select -->
<div class="form-floating mb-3">
<select class="form-select" id="floatingSelect">
<option selected>Open this select menu</option>
<option value="1">One</option>
</select>
<label for="floatingSelect">Works with selects</label>
</div>
<!-- note: placeholder is REQUIRED for floating labels -->
<!-- the placeholder="..." must be present -->表单验证
Bootstrap 提供 is-valid 和 is-invalid 类用于验证样式,valid-feedback 和 invalid-feedback 用于消息。表单上的 needs-validation 类结合 novalidate(禁用浏览器工具提示),允许你使用 Bootstrap 的验证 UI 和自定义 JS。提交时,添加 was-validated 显示所有验证状态。这适用于 HTML5 约束(required、pattern、type)或自定义服务端验证。
<!-- server-side validation classes -->
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control is-valid" id="username" value="valid_user">
<div class="valid-feedback">Looks good!</div>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control is-invalid" id="email">
<div class="invalid-feedback">Please enter a valid email.</div>
</div>
<!-- no validate on form to disable browser validation -->
<form class="needs-validation" novalidate>
<div class="mb-3">
<input type="text" class="form-control" required>
<div class="invalid-feedback">This field is required.</div>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
<!-- JS: form.classList.add('was-validated') on submit -->卡片
基础卡片
卡片是灵活的容器,可选页眉、主体和页脚。card-img-top 在顶部放置图片。card-title、card-subtitle、card-text 样式化内容。宽度由父网格或 style 属性控制——卡片默认为全宽。card-header 和 card-footer 用于结构化内容。卡片用统一组件取代了 Bootstrap 3 的面板、井和缩略图。
<div class="card" style="width: 18rem;">
<!-- optional image -->
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Some quick example text to build on the card title.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
<!-- card sections -->
<div class="card">
<div class="card-header">Header</div>
<div class="card-body">Body content</div>
<div class="card-footer text-muted">Footer</div>
</div>卡片内容类型
卡片可以包含各种内容类型:图片、文本、列表组(使用 list-group-flush 移除边框并匹配卡片边缘)和链接(card-link 添加间距)。混合搭配部分——card-body、list-group、card-header、card-footer——构建你需要的确切卡片。list-group-flush 将列表无缝集成到卡片中,没有双边框。这种灵活性使卡片成为 Bootstrap 最通用的组件。
<!-- list group inside card -->
<div class="card" style="width: 18rem;">
<div class="card-header">Featured</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Item 1</li>
<li class="list-group-item">Item 2</li>
<li class="list-group-item">Item 3</li>
</ul>
<div class="card-body">
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</div>
</div>
<!-- kitchen sink (all content types) -->
<div class="card">
<img src="..." class="card-img-top">
<div class="card-body">
<h5 class="card-title">Title</h5>
<p class="card-text">Text</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">List item</li>
</ul>
<div class="card-body">
<a href="#" class="card-link">Link</a>
</div>
</div>卡片网格与组
card-group 连接卡片,共享边框和等高。对于响应式卡片网格,使用网格系统:row row-cols-1 row-cols-md-3 g-4 在移动端 1 列,桌面端 3 列,gap-4 间距。每张卡片放在 .col 中。网格方法比 card-group 更灵活——卡片可以有独立高度,你控制换行。仅在需要连接的等高卡片时使用 card-group。
<!-- card group (cards share border, equal height) -->
<div class="card-group">
<div class="card">
<img src="..." class="card-img-top">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
</div>
</div>
</div>
<!-- cards have equal height, connected borders -->
<!-- using grid for responsive card layout -->
<div class="row row-cols-1 row-cols-md-3 g-4">
<div class="col">
<div class="card">...</div>
</div>
<div class="col">
<div class="card">...</div>
</div>
</div>卡片导航
在 card-header 中添加 nav-tabs 或 nav-pills 实现卡片导航。card-header-tabs 和 card-header-pills 将导航与页眉边缘齐平集成。使用 nav-link active 标记当前标签,nav-link disabled 标记非活动。要使标签功能化(切换内容),添加 data-bs-toggle='tab' 和标签面板——Bootstrap 的 JS 处理切换。此模式常见于仪表板和带多视图的内容卡片。
<!-- card with nav tabs -->
<div class="card text-center">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
</div>
<div class="card-body">
<h5 class="card-title">Special title</h5>
<p class="card-text">Content for active tab.</p>
<a href="#" class="btn btn-primary">Go</a>
</div>
</div>
<!-- card with pills -->
<div class="card-header">
<ul class="nav nav-pills card-header-pills">
<li class="nav-item"><a class="nav-link active" href="#">Active</a></li>
</ul>
</div>卡片变体与叠加
卡片颜色变体:bg-{color} 搭配 text-white/text-dark 用于实心背景;border-{color} 搭配 text-{color} 用于彩色边框。card-img-overlay 将文本放置在图片上(card-img 填充卡片,叠加内容在顶部)——使用 text-bg-dark 确保图片上的文本可读。text-center/text-start/text-end 控制文本对齐。对于深色图片叠加,确保文本和图片之间有足够的对比度。
<!-- colored card -->
<div class="card text-white bg-primary">
<div class="card-header">Header</div>
<div class="card-body">
<h5 class="card-title">Primary card</h5>
<p class="card-text">Text on colored background.</p>
</div>
</div>
<!-- border colored card -->
<div class="card border-primary">
<div class="card-header border-primary">Header</div>
<div class="card-body text-primary">
<h5 class="card-title">Primary border</h5>
</div>
</div>
<!-- image overlay (text over image) -->
<div class="card text-bg-dark">
<img src="..." class="card-img">
<div class="card-img-overlay">
<h5 class="card-title">Overlay title</h5>
<p class="card-text">Text over the image.</p>
</div>
</div>
<!-- text alignment -->
<div class="card text-center">Centered text</div>
<div class="card text-end">Right-aligned</div>模态框
基础模态框
模态框由 data-bs-toggle='modal' 和匹配模态框 id 的 data-bs-target 触发。结构:modal > modal-dialog > modal-content > modal-header/body/footer。btn-close 是 X 按钮(data-bs-dismiss='modal')。fade 添加过渡。tabindex='-1' 和 aria-hidden 用于可访问性。模态框默认隐藏,使用固定定位。Bootstrap JS 处理显示、隐藏、背景和焦点管理。
<!-- Button to trigger -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal"
data-bs-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
Modal body text goes here.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>模态框尺寸与滚动
模态框尺寸:modal-sm、默认、modal-lg、modal-xl、modal-fullscreen。modal-fullscreen-{breakpoint}-down 仅在该断点以下全屏。modal-dialog-scrollable 使主体滚动(页眉/页脚固定)——对长内容必不可少。modal-dialog-centered 垂直居中模态框。这些类放在 modal-dialog 上。组合使用:modal-dialog modal-dialog-centered modal-dialog-scrollable 实现居中、可滚动的模态框。
<!-- sizes -->
<div class="modal-dialog modal-sm">Small</div>
<div class="modal-dialog">Default</div>
<div class="modal-dialog modal-lg">Large</div>
<div class="modal-dialog modal-xl">Extra large</div>
<div class="modal-dialog modal-fullscreen">Fullscreen</div>
<!-- responsive fullscreen -->
<div class="modal-dialog modal-fullscreen-sm-down">
<!-- fullscreen below sm, normal above -->
</div>
<!-- scrollable long content -->
<div class="modal-dialog modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-body">
<!-- long content scrolls within modal -->
</div>
</div>
</div>
<!-- vertically centered -->
<div class="modal-dialog modal-dialog-centered">
<!-- centered vertically in viewport -->
</div>通过 JavaScript 控制模态框
通过 JS 控制模态框:用 new bootstrap.Modal(el) 创建实例,然后 show()/hide()/toggle()。backdrop: 'static' 防止点击背景关闭;keyboard: false 禁用 Esc。事件:show.bs.modal、shown.bs.modal、hide.bs.modal、hidden.bs.modal(show = 开始,shown = 完成)。使用 shown.bs.modal 聚焦输入框或加载动态内容。静态背景适用于意外关闭会丢失数据的表单。
<!-- HTML modal (hidden) -->
<div class="modal fade" id="myModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">Dynamic content here</div>
</div>
</div>
</div>
<script>
// create modal instance
const modalEl = document.getElementById('myModal')
const modal = new bootstrap.Modal(modalEl)
// show
modal.show()
// hide
modal.hide()
// toggle
modal.toggle()
// handle events
modalEl.addEventListener('shown.bs.modal', () => {
console.log('modal fully shown')
})
modalEl.addEventListener('hidden.bs.modal', () => {
console.log('modal fully hidden')
})
// pass options
const modal = new bootstrap.Modal(modalEl, {
backdrop: 'static', // don't close on backdrop click
keyboard: false // don't close on Esc
})
</script>工具提示与弹出框
工具提示在悬停/聚焦时显示;弹出框在点击时显示。两者都需要 JS 初始化(Bootstrap 不为性能自动初始化)。data-bs-placement 设置位置(top/bottom/left/right)。弹出框有标题 + 内容;data-bs-html='true' 允许 HTML。data-bs-trigger='focus' 使弹出框可关闭(点击外部关闭)。工具提示和弹出框使用 Popper.js(包含在包中)进行智能定位。它们是提示、定义和上下文信息的理想选择。
<!-- tooltip (hover/focus) -->
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip"
data-bs-placement="top" title="Tooltip on top">
Hover me
</button>
<!-- positions: top, bottom, left, right -->
<button data-bs-toggle="tooltip" data-bs-placement="right"
title="Right tooltip">Right</button>
<!-- popover (click) -->
<button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover"
title="Popover title" data-bs-content="Content here">
Click to toggle popover
</button>
<!-- popover with HTML content -->
<button data-bs-toggle="popover" data-bs-html="true"
data-bs-content="<b>Bold</b> content">HTML popover</button>
<!-- dismissible popover -->
<button data-bs-toggle="popover" data-bs-trigger="focus"
title="Dismissible">Click outside to dismiss</button>
<script>
// MUST initialize tooltips and popovers
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
tooltipTriggerList.forEach(t => new bootstrap.Tooltip(t))
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
popoverTriggerList.forEach(p => new bootstrap.Popover(p))
</script>Offcanvas(抽屉)
Offcanvas 是滑入式抽屉(类似模态框但从边缘)。offcanvas-start/end/top/bottom 设置滑动方向。结构镜像模态框:offcanvas-header/body 带 btn-close。data-bs-backdrop='static' 防止点击背景关闭。text-bg-dark 用于深色变体。Offcanvas 是移动导航、过滤器或侧边面板的理想选择。与模态框不同,它不覆盖全屏——它是叠加抽屉。用 offcanvas 替代 navbar-collapse 实现现代移动菜单。
<!-- trigger button -->
<button class="btn btn-primary" data-bs-toggle="offcanvas"
data-bs-target="#demoOffcanvas">
Open offcanvas
</button>
<!-- offcanvas drawer -->
<div class="offcanvas offcanvas-start" tabindex="-1" id="demoOffcanvas">
<div class="offcanvas-header">
<h5 class="offcanvas-title">Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"></button>
</div>
<div class="offcanvas-body">
<p>Content for the offcanvas.</p>
</div>
</div>
<!-- positions: start (left), end (right), top, bottom -->
<div class="offcanvas offcanvas-end" id="rightDrawer">Right side</div>
<div class="offcanvas offcanvas-top" id="topDrawer">Top</div>
<!-- dark variant -->
<div class="offcanvas offcanvas-start text-bg-dark" id="darkDrawer">
Dark offcanvas
</div>
<!-- static backdrop (no close on click outside) -->
<div class="offcanvas" data-bs-backdrop="static">...</div>警告框
基础警告框
警告框显示上下文反馈消息。alert-{variant} 设置颜色(primary、success、danger、warning、info、light、dark)。role='alert' 用于屏幕阅读器可访问性。alert-link 样式化链接以匹配警告颜色。警告框用于:表单验证反馈、操作结果(已保存!已删除!)或重要通知。语义化选择颜色:success 表示积极,danger 表示错误,warning 表示警告,info 表示中性信息。