方法
重置在 Normalize 的基础上构建,仅使用元素选择器为许多 HTML 元素提供带有一定主张的样式。额外的样式仅通过类完成。例如,我们重置了一些 <table> 样式以获得更简单的基础,随后提供 .table、.table-bordered 等。
以下是我们选择在重置中覆盖内容的原则和原因:
- 将一些浏览器默认值更新为使用
rem而不是em,以实现可缩放组件的间距。 - 避免
margin-top。垂直外边距可能折叠,产生意外结果。更重要的是,单一方向的margin是更简单的心智模型。 - 为便于跨设备尺寸缩放,块级元素应使用
rem作为margin。 - 尽量减少
font相关属性的声明,尽可能使用inherit。
根 CSS 变量
我们向所有 CSS 包添加 :root 级别的 CSS 变量,以实现实时自定义,而无需总是重新编译 Sass。例如,考虑以下用于常见 <body> 样式的 :root CSS 变量:
--border-width: #{$border-width},
--border-width-keyline: .5px,
--border-style: #{$border-style},
--border-color: light-dark(var(--gray-200), var(--gray-700)),
--border-color-translucent: color-mix(in oklch, var(--fg-body) 15%, transparent),
在实践中,这些变量随后在重置中如此应用:
body {
margin: 0; // 1
font-family: var(--body-font-family);
font-size: var(--body-font-size);
font-weight: var(--body-font-weight);
line-height: var(--body-line-height);
color: var(--fg-body);
text-align: var(--body-text-align);
background-color: var(--bg-body); // 2
-webkit-text-size-adjust: 100%; // 3
-webkit-tap-highlight-color: transparent; // 4
}
这允许你以任何喜欢的方式进行实时自定义:
<body style="--bs-body-color: #333;">
<!-- ... -->
</body>
页面默认值
<html> 和 <body> 元素经过更新,以提供更好的页面级默认值。具体来说:
box-sizing在每个元素上全局设置为border-box——包括*::before和*::after。这确保元素声明的宽度不会因内边距或边框而被超出。- 未在
<html>上声明基础font-size,但假定为16px(浏览器默认值)。在<body>上应用font-size: 1rem,以便通过媒体查询轻松实现响应式字体缩放,同时尊重用户偏好并确保更易访问的方式。可以通过修改$font-size-root变量来覆盖此浏览器默认值。
- 未在
<body>还设置全局font-family、font-weight、line-height和color。这些随后会被某些表单元素继承,以防止字体不一致。- 为安全起见,
<body>声明了background-color,默认为#fff。
原生字体栈
Bootstrap 使用“原生字体栈”或“系统字体栈”,以实现每个设备和操作系统上的最佳文本渲染。这些系统字体专为当今的设备而设计,在屏幕上具有更好的渲染效果、可变字体支持等。请阅读这篇 Smashing Magazine 文章中关于原生字体栈的内容。
该字体栈以适用于 Apple 设备的 -apple-system 和 BlinkMacSystemFont 开始,然后是适用于 Windows 的 Segoe UI、适用于 Android 的 Roboto,以及适用于 Linux 的 Noto Sans 或 Liberation Sans。Arial 和 sans-serif 是 Web 后备字体,最后以四种 emoji 字体结束。
该字体栈省略了 system-ui 关键字。在某些系统上,system-ui 会解析为区域设置的用户界面字体。该字体可能以错误的字形显示非拉丁文本——请参阅 issue #42703。
// The stack omits system-ui. It resolves to the user interface font of the
// locale, which shows non-Latin text incorrectly. See twbs/bootstrap#42703.
--body-font-family: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', 'Noto Sans', 'Liberation Sans', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'",
--body-font-size: 1rem,
--body-font-weight: var(--font-weight-normal),
--body-line-height: 1.5,
请注意,由于字体栈包含 emoji 字体,许多常见符号/装饰用 Unicode 字符将渲染为多彩的象形图。它们的外观会有所不同,取决于浏览器/平台原生 emoji 字体中使用的样式,并且它们不会受到任何 CSS color 样式的影响。
此 font-family 应用于 <body>,并在整个 Bootstrap 中全局自动继承。要切换全局 font-family,请更新 --body-font-family。
标题
所有标题元素——<h1> 到 <h6> 都移除了 margin-top,设置了 margin-bottom: .5rem,并收紧了 line-height。虽然标题默认继承其 color,但你也可以通过可选 CSS 变量 --bs-heading-color 覆盖它。
你也可以使用 .h1–.h6 类在不使用 HTML 元素的情况下为标题设置样式。
| 标题 | 示例 |
|---|---|
<h1></h1> | h1. Bootstrap heading |
<h2></h2> | h2. Bootstrap heading |
<h3></h3> | h3. Bootstrap heading |
<h4></h4> | h4. Bootstrap heading |
<h5></h5> | h5. Bootstrap heading |
<h6></h6> | h6. Bootstrap heading |
段落
所有 <p> 元素都移除了 margin-top,并设置了 margin-bottom: 1rem,以便轻松设置间距。
This is an example paragraph.
<p>This is an example paragraph.</p>链接
链接应用了默认 color 和下划线。虽然链接会在 :hover 时变化,但它们不会根据是否被 :visited 而改变。它们也不会获得特殊的 :focus 样式。
<a href="#">This is an example link</a>使用 .link-{%} 工具类更改链接颜色的不透明度。
<a href="#" class="link-50">This is an example link</a>使用主题工具类更改链接颜色。
<a href="#" class="theme-primary">Primary link</a>
<a href="#" class="theme-accent">Accent link</a>
<a href="#" class="theme-success">Success link</a>
<a href="#" class="theme-danger">Danger link</a>
<a href="#" class="theme-warning">Warning link</a>
<a href="#" class="theme-info">Info link</a>
<a href="#" class="theme-inverse">Inverse link</a>
<a href="#" class="theme-secondary">Secondary link</a>占位链接——即没有 href 的链接——通过更具体的选择器定位,其 color 和 text-decoration 被重置为默认值。
<a>This is a placeholder link</a>水平分隔线
<hr> 元素已被简化。与浏览器默认值类似,<hr> 通过 border-top 设置样式,默认 opacity: .25,并通过 color 自动继承其 border-color,包括当 color 通过父元素设置时。它们可以通过文本、边框和不透明度工具类进行修改。
<hr>
<div class="fg-success">
<hr>
</div>
<hr class="border border-danger border-2 opacity-50">
<hr class="border border-primary border-3 opacity-75">列表
所有列表——<ul>、<ol> 和 <dl>——都移除了 margin-top,并设置 margin-bottom: 1rem。嵌套列表没有 margin-bottom。我们还重置了 <ul> 和 <ol> 元素的 padding-left。
- 所有列表都移除了顶部外边距
- 并且底部外边距已规范化
- 嵌套列表没有底部外边距
- 这样它们的外观更均匀
- 特别是在后面还有其他列表项时
- 左侧内边距也已重置
- 这是一个有序列表
- 包含几个列表项
- 它具有相同的外观
- 与前面的无序列表一样
为了更简单的样式、清晰的层次和更好的间距,描述列表更新了 margin。<dd> 将 margin-left 重置为 0 并添加 margin-bottom: .5rem。<dt> 被加粗。
- Description lists
- A description list is perfect for defining terms.
- Term
- Definition for the term.
- A second definition for the same term.
- Another term
- Definition for this other term.
行内代码
使用 <code> 包裹行内代码片段。请务必转义 HTML 尖括号。
<section> should be wrapped as inline.For example, <code><section></code> should be wrapped as inline.代码块
对于多行代码使用 <pre>。再次强调,请务必转义代码中的任何尖括号以正确渲染。<pre> 元素被重置以移除其 margin-top,并对其 margin-bottom 使用 rem 单位。
<p>Sample text here...</p>
<p>这里是另一行示例文本……</p>
<pre><code><p>Sample text here...</p>
<p>这里是另一行示例文本……</p>
</code></pre>变量
要指示变量,使用 <var> 标签。
<var>y</var> = <var>m</var><var>x</var> + <var>b</var>用户输入
使用 <kbd> 指示通常通过键盘输入的输入。
要编辑设置,请按 Ctrl + ,
To switch directories, type <kbd>cd</kbd> followed by the name of the directory.<br>
要编辑设置,请按 <kbd>Ctrl</kbd> + <kbd>,</kbd>示例输出
要指示程序的示例输出,使用 <samp> 标签。
<samp>This text is meant to be treated as sample output from a computer program.</samp>表格
表格经过略微调整,以设置 <caption> 的样式、折叠边框并确保一致的 text-align。边框、内边距等的其他更改随.table 类提供。
| Table heading | Table heading | Table heading | Table heading |
|---|---|---|---|
| Table cell | Table cell | Table cell | Table cell |
| Table cell | Table cell | Table cell | Table cell |
| Table cell | Table cell | Table cell | Table cell |
<table>
<caption>
这是一个示例表格,这是用于描述其内容的标题。
</caption>
<thead>
<tr>
<th>Table heading</th>
<th>Table heading</th>
<th>Table heading</th>
<th>Table heading</th>
</tr>
</thead>
<tbody>
<tr>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
</tbody>
</table>表单
各种表单元素已被重置,以获得更简单的基础样式。以下是一些最显著的变化:
<fieldset>没有边框、内边距或外边距,因此可以轻松用作单个输入或输入组的包裹元素。<legend>与 fieldset 一样,也被重新设计样式,以显示为某种标题。<label>设置为display: inline-block,以允许应用margin。<input>、<select>、<textarea>和<button>主要由 Normalize 处理,但重置也会移除它们的margin并设置line-height: inherit。<textarea>被修改为只能垂直调整大小,因为水平调整大小通常会“破坏”页面布局。<button>和<input>按钮元素在:not(:disabled)时具有cursor: pointer。
这些变化以及更多内容在下面演示。
某些日期输入类型在最新版本的 Safari 和 Firefox 中尚未得到完全支持。
按钮的指针
重置包含对 role="button" 的增强,将默认光标更改为 pointer。将此属性添加到元素以帮助指示元素是可交互的。<button> 元素不需要此角色,它们有自己的 cursor 更改。
<span role="button" tabindex="0">Non-button element button</span>其他元素
地址
<address> 元素经过更新,将浏览器默认的 font-style 从 italic 重置为 normal。line-height 现在也被继承,并添加了 margin-bottom: 1rem。<address> 用于呈现最近祖先(或整个作品主体)的联系信息。通过在行末使用 <br> 来保留格式。
1123 Fictional St,
San Francisco, CA 94103
P: (123) 456-7890 Full Name
first.last@example.com
引用块
引用块的默认 margin 是 1em 40px,因此我们将其重置为 0 0 1rem,以与其他元素更一致。
A well-known quote, contained in a blockquote element.
行内元素
<abbr> 元素获得基本样式,使其在段落文本中脱颖而出。
摘要
摘要的默认 cursor 是 text,因此我们将其重置为 pointer,以传达该元素可以通过点击进行交互。
Some details
More info about the details.
Even more details
Here are even more details about the details.
HTML5 [hidden] 属性
HTML5 添加了一个名为 [hidden] 的新全局属性,其默认样式为 display: none。借鉴 PureCSS 的想法,我们通过使 [hidden] { display: none !important; } 来改进此默认值,以帮助防止其 display 被意外覆盖。
<input type="text" hidden>
由于 [hidden] 与 jQuery 的 $(...).hide() 和 $(...).show() 方法不兼容,我们不特别推荐 [hidden] 优先于其他管理元素 display 的技术。
如果仅切换元素的可见性,即不修改其 display 且元素仍可影响文档流,请改用.invisible 类。
iframe 颜色模式
<iframe> 元素经过更新,设置 color-scheme: light dark。当嵌入文档使用与 iframe 元素不同的颜色模式时,浏览器会在 iframe 后方绘制不透明的 background: canvas。在深色页面上,这会显示为白色方块。
嵌入文档遵循系统偏好。它无法看到你的 data-bs-theme 属性。因此我们也把每个 iframe 固定为系统偏好。这使嵌入内容保持透明,即使你使用 data-bs-theme 强制设置主题时也是如此。
<div class="bg-body fg-body p-3" data-bs-theme="dark">
<iframe class="w-100" style="color-scheme: unset" src="data:text/html,<!DOCTYPE html><html><meta name='color-scheme' content='light dark'><body style='background-color:transparent;color:gray;'><h4>Default iframe</h4></body></html>"></iframe>
<iframe class="w-100" src="data:text/html,<!DOCTYPE html><html><meta name='color-scheme' content='light dark'><body style='background-color:transparent;color:gray;'><h4>iframe with color-scheme: light-dark</h4></body></html>"></iframe>
<iframe class="w-100" src="data:text/html,<!DOCTYPE html><html><body style='background-color:transparent;color:gray;'><h4>Embed that declares no color scheme</h4></body></html>"></iframe>
</div>前两个嵌入内容声明了 <meta name="color-scheme" content="light dark">,因此它们遵循系统偏好并保持透明。第三个没有声明任何内容,与大多数第三方嵌入内容一样。将系统设置为深色模式,即可看到浏览器在其后方绘制白色方块。我们的规则无法阻止这一点——请阅读下一节。
没有深色模式的第三方嵌入内容
某些第三方嵌入内容没有深色模式。支付按钮、评论小部件和评价小部件是常见的例子。当系统处于深色模式时,浏览器会在此类嵌入内容后方绘制白色背景。这是浏览器的保护措施。它使嵌入内容的浅色内容保持可读。Bootstrap 无法为你移除它。
为该 iframe 添加 .color-scheme-light 颜色模式工具类。背景会再次变为透明。
<iframe class="color-scheme-light" src="..."></iframe>
仅对没有深色模式的嵌入内容使用此方法。Safari 和 Firefox 会将此值应用于嵌入文档。具有深色模式的嵌入内容随后会以浅色模式显示。