重置样式(Reboot)
Reboot,是一个将特定元素的 CSS 更改汇集到单个文件中的集合,它为启动 Bootstrap 提供了一个优雅、一致且简单的基线,以便在此基础上构建。
方法
Reboot 建立在 Normalize 之上,为许多 HTML 元素提供了一些带有一定主观意见的样式,仅使用元素选择器。额外的样式仅通过类完成。例如,我们重置了一些 <table> 样式以获得更简单的基线,随后提供 .table、.table-bordered 等更多样式。
以下是我们在 Reboot 中选择覆盖内容的指南和理由:
- 更新一些浏览器默认值以使用
rem秒而不是em秒来进行可扩展组件间距。 - 避免使用
margin-top。垂直外边距可能会合并,产生意想不到的结果。更重要的是,单方向的margin是一个更简单的思维模型。 - 为了更容易在不同设备尺寸下进行缩放,块级元素应使用
rem进行margin。 - 将与
font相关的属性声明保持在最低限度,并尽可能使用inherit。
CSS 变量
在 v5.2.0 中添加在 v5.1.1 中,我们标准化了我们的要求 @import贯穿我们所有的 CSS 包(包括 bootstrap.css, bootstrap-reboot.css,和 bootstrap-grid.css) 包括 _root.scss. 这增加了 :root 将 CSS 变量应用到所有捆绑包,无论该捆绑包中使用了多少变量。最终,Bootstrap 5 将继续看到更多 CSS 变量 随着时间的推移添加,以便提供更多实时的自定义,而不需要每次都重新编译 Sass。我们的方法是将我们的源 Sass 变量转换为 CSS 变量。这样,即使你不使用 CSS 变量,你仍然可以拥有 Sass 的全部功能。 这仍在进行中,完全实施将需要时间。
例如,考虑这些 :root 常用的 CSS 变量 <body> 样式:
@if $font-size-root != null {
--#{$prefix}root-font-size: #{$font-size-root};
}
--#{$prefix}body-font-family: #{inspect($font-family-base)};
@include rfs($font-size-base, --#{$prefix}body-font-size);
--#{$prefix}body-font-weight: #{$font-weight-base};
--#{$prefix}body-line-height: #{$line-height-base};
@if $body-text-align != null {
--#{$prefix}body-text-align: #{$body-text-align};
}
--#{$prefix}body-color: #{$body-color};
--#{$prefix}body-color-rgb: #{to-rgb($body-color)};
--#{$prefix}body-bg: #{$body-bg};
--#{$prefix}body-bg-rgb: #{to-rgb($body-bg)};
--#{$prefix}emphasis-color: #{$body-emphasis-color};
--#{$prefix}emphasis-color-rgb: #{to-rgb($body-emphasis-color)};
--#{$prefix}secondary-color: #{$body-secondary-color};
--#{$prefix}secondary-color-rgb: #{to-rgb($body-secondary-color)};
--#{$prefix}secondary-bg: #{$body-secondary-bg};
--#{$prefix}secondary-bg-rgb: #{to-rgb($body-secondary-bg)};
--#{$prefix}tertiary-color: #{$body-tertiary-color};
--#{$prefix}tertiary-color-rgb: #{to-rgb($body-tertiary-color)};
--#{$prefix}tertiary-bg: #{$body-tertiary-bg};
--#{$prefix}tertiary-bg-rgb: #{to-rgb($body-tertiary-bg)};
在实践中,这些变量随后在 Reboot 中这样应用:
body {
margin: 0; // 1
font-family: var(--#{$prefix}body-font-family);
@include font-size(var(--#{$prefix}body-font-size));
font-weight: var(--#{$prefix}body-font-weight);
line-height: var(--#{$prefix}body-line-height);
color: var(--#{$prefix}body-color);
text-align: var(--#{$prefix}body-text-align);
background-color: var(--#{$prefix}body-bg); // 2
-webkit-text-size-adjust: 100%; // 3
-webkit-tap-highlight-color: rgba($black, 0); // 4
}
这允许你随心所欲地进行实时自定义:
<body style="--bs-body-color: #333;">
<!-- ... -->
</body>
页面默认值
<html> 和 <body> 元素已更新,以提供更好的全页默认设置。更具体地说:
- 这
box-sizing在每个元素上全局设置——包括*::before和*::after,到border-box这确保了元素声明的宽度不会因内边距或边框而被超出。- 无基地
font-size在…上声明<html>,但是16px假设为(浏览器默认值)。font-size: 1rem应用于<body>通过媒体查询轻松实现响应式字体缩放,同时尊重用户偏好并确保更易于访问的方法。可以通过修改来覆盖此浏览器默认设置$font-size-root变量。
- 无基地
- 这
<body>还设置了一个全局font-family,font-weight,line-height,和color这会被某些表单元素继承,以防止字体不一致。 - 为了安全,
<body>有一个声明background-color,默认为#fff.
本地字体堆栈
Bootstrap 使用“本地字体堆栈”或“系统字体堆栈”,以在每个设备和操作系统上实现最佳文本渲染。这些系统字体是专为现代设备设计的,在屏幕上具有改进的渲染效果,支持可变字体等等。阅读更多关于 本地字体堆栈的内容,请参见这篇 Smashing Magazine 文章。
$font-family-sans-serif:
// Cross-platform generic font family (default user interface font)
system-ui,
// Safari for macOS and iOS (San Francisco)
-apple-system,
// Windows
"Segoe UI",
// Android
Roboto,
// older macOS and iOS
"Helvetica Neue",
// Linux
"Noto Sans",
"Liberation Sans",
// Basic web fallback
Arial,
// Sans serif fallback
sans-serif,
// Emoji fonts
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji" !default;
请注意,因为字体堆栈包含表情符号字体,许多常见的符号/装饰字符 Unicode 字符将以多彩象形符号显示。它们的外观会有所不同,取决于浏览器/平台本地表情符号字体使用的样式,并且不会受到任何 CSS color 样式的影响。
这个 font-family 应用于 <body> 并在整个 Bootstrap 中自动全局继承。要切换全局 font-family,请更新 $font-family-base 并重新编译 Bootstrap。
标题
所有标题元素——<h1>——<h6> 都已移除其 margin-top,设置了 margin-bottom: .5rem,并收紧了 line-height。虽然标题默认继承其 color,但你也可以通过可选的 CSS 变量 --bs-heading-color 来覆盖它。
| 标题 | 示例 |
|---|---|
<h1></h1> | h1. Bootstrap 标题 |
<h2></h2> | h2. Bootstrap 标题 |
<h3></h3> | h3. Bootstrap 标题 |
<h4></h4> | h4. Bootstrap 标题 |
<h5></h5> | h5. Bootstrap 标题 |
<h6></h6> | h6. Bootstrap 标题 |
段落
所有 <p> 元素都已移除它们的 margin-top 并设置了 margin-bottom: 1rem 以便于间距。
这是一个示例段落。
<p>This is an example paragraph.</p>链接
链接默认应用 color 和下划线。虽然链接在 :hover 时会变化,但它们不会根据某人是否 :visited 链接而变化。它们也没有收到特殊的 :focus 样式。
<a href="#">This is an example link</a>从 v5.3.x 开始,链接 color 使用 rgba() 和新的 -rgb CSS 变量设置,允许轻松自定义链接颜色的不透明度。使用 --bs-link-opacity CSS 变量更改链接颜色不透明度:
<a href="#" style="--bs-link-opacity: .5">This is an example link</a>占位符链接——那些没有 href 的——会被更具体的选择器定位,并将它们的 color 和 text-decoration 重置为默认值。
<a>This is a placeholder link</a>水平线
这 <hr> 元素已被简化。类似于浏览器默认设置, <hr>s 的样式通过 border-top,有一个默认值 opacity: .25,并自动继承它们的 border-color 通过 color,包括当 color 由父元素设置。它们可以通过文本、边框和不透明度工具进行修改。
<hr>
<div class="text-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。我们也已重置了 padding-left 开 <ul> 和 <ol> 元素。
- 所有列表的顶部外边距已被移除
- 底部外边距已被标准化
- 嵌套列表没有底部外边距
- 这样它们看起来更加均衡
- 尤其是后面跟着更多列表项时
- 左侧内边距也已重置
- 这是一个有序列表
- 包含几个列表项
- 它的整体外观与之前的无序列表相同
- 和之前的无序列表一样
为了更简单的样式、清晰的层次结构和更好的间距,描述列表已更新margin。<dd>将margin-left重置为0并添加margin-bottom: .5rem。<dt>是加粗的。
- 描述列表
- 描述列表非常适合定义术语。
- 术语
- 该术语的定义。
- 同一术语的第二个定义。
- 另一个术语
- 该其他术语的定义。
行内代码
用<code>包裹内联代码片段。确保对 HTML 尖括号进行转义。
<section> 应该作为内联包装。For example, <code><section></code> should be wrapped as inline.代码块
使用 <pre>用于多行代码。再次确保在代码中转义任何尖括号以便正确渲染。 <pre> 元素被重置以移除其 margin-top 并使用 rem 它的单位 margin-bottom.
<p>Sample text here...</p>
<p>And another line of sample text here...</p>
<pre><code><p>Sample text here...</p>
<p>And another line of sample text here...</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>
To edit settings, press <kbd><kbd>Ctrl</kbd> + <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>
<caption>
This is an example table, and this is its caption to describe the contents.
</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>s 没有边框、内边距或外边距,因此它们可以轻松用作单个输入或输入组的容器。<legend>s,像字段集一样,也已被重新设计为以某种标题的形式显示。<label>被设置为display: inline-block允许margin适用。<input>s,<select>s,<textarea>s,以及<button>大多数问题由 Normalize 处理,但 Reboot 会移除它们margin和集合line-height: inherit,也一样。<textarea>s 被修改为仅可垂直调整大小,因为水平调整大小常常会“破坏”页面布局。<button>和<input>按钮元素有cursor: pointer什么时候:not(:disabled).
下面演示了这些变化以及更多内容。
某些日期输入类型在最新版本的 Safari 和 Firefox 中 尚未完全支持。
按钮上的指针
Reboot 包括对 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> 来保持格式。
引用块
块引用的默认 margin 是 1em 40px,所以我们将其重置为 0 0 1rem 以与其他元素更一致。
著名引用,包含在blockquote元素中。
行内元素
<abbr> 元素接收基本样式,以使其在段落文字中脱颖而出。
摘要
摘要上的默认 cursor 是 text,所以我们将其重置为 pointer 以表示该元素可以通过点击进行交互。
一些细节
关于这些细节的更多信息。
更多的细节
这里有关于这些细节的更多信息。
HTML5 [hidden] 属性
HTML5 添加了一个名为 [hidden] 的新的全局属性,其默认样式为 display: none。借鉴 PureCSS的想法,我们通过使 [hidden] { display: none !important; } 来改进这一默认设置,以帮助防止其 display 被意外覆盖。
<input type="text" hidden>
由于 [hidden] 与 jQuery 的 $(...).hide() 和 $(...).show() 方法不兼容,我们并不特别推荐使用 [hidden] 来管理元素的 display,而不是其他技术。
仅仅切换元素的可见性,也就是说其display没有被修改,并且元素仍然可以影响文档的布局,请使用.invisible类。