方法
¥Approach
Reboot 建立在 Normalize 的基础上,仅使用元素选择器即可提供许多具有某种有态度的样式的 HTML 元素。附加样式仅在类中完成。例如,我们重新启动一些 <table>
样式以获得更简单的基线,然后提供 .table
、.table-bordered
等。
¥Reboot builds upon Normalize, providing many HTML elements with somewhat opinionated styles using only element selectors. Additional styling is done only with classes. For example, we reboot some <table>
styles for a simpler baseline and later provide .table
, .table-bordered
, and more.
以下是我们选择在重新启动时覆盖哪些内容的指南和原因:
¥Here are our guidelines and reasons for choosing what to override in Reboot:
-
更新一些浏览器默认值以使用
rem
而不是em
来实现可缩放的组件间距。¥Update some browser default values to use
rem
s instead ofem
s for scalable component spacing. -
避免
margin-top
。垂直边距可能会折叠,从而产生意想不到的结果。但更重要的是,margin
的单一方向是一个更简单的心智模型。¥Avoid
margin-top
. Vertical margins can collapse, yielding unexpected results. More importantly though, a single direction ofmargin
is a simpler mental model. -
为了更轻松地跨设备尺寸进行缩放,块元素应使用
rem
作为margin
。¥For easier scaling across device sizes, block elements should use
rem
s formargin
s. -
将
font
相关属性的声明保持在最低限度,并尽可能使用inherit
。¥Keep declarations of
font
-related properties to a minimum, usinginherit
whenever possible.
CSS 变量
¥CSS variables
Added in v5.2.0在 v5.1.1 中,我们对所有 CSS 包(包括 bootstrap.css
、bootstrap-reboot.css
和 bootstrap-grid.css
)所需的 @import
进行了标准化,以包含 _root.scss
。这会将 :root
级别的 CSS 变量添加到所有包中,无论该包中使用了多少个变量。最终,随着时间的推移,Bootstrap 5 将继续看到更多的 CSS 变量 添加,以便提供更多实时定制,而无需总是重新编译 Sass。我们的方法是获取源 Sass 变量并将它们转换为 CSS 变量。这样,即使你不使用 CSS 变量,你仍然拥有 Sass 的所有功能。该计划仍在进行中,需要时间才能完全实现。
¥With v5.1.1, we standardized our required @import
s across all our CSS bundles (including bootstrap.css
, bootstrap-reboot.css
, and bootstrap-grid.css
) to include _root.scss
. This adds :root
level CSS variables to all bundles, regardless of how many of them are used in that bundle. Ultimately Bootstrap 5 will continue to see more CSS variables added over time, in order to provide more real-time customization without the need to always recompile Sass. Our approach is to take our source Sass variables and transform them into CSS variables. That way, even if you don’t use CSS variables, you still have all the power of Sass. This is still in-progress and will take time to fully implement.
例如,考虑常见 <body>
样式的这些 :root
CSS 变量:
¥For example, consider these :root
CSS variables for common <body>
styles:
@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)};
在实践中,这些变量然后在重新启动中应用,如下所示:
¥In practice, those variables are then applied in Reboot like so:
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
}
它允许你根据自己的喜好进行实时定制:
¥Which allows you to make real-time customizations however you like:
<body style="--bs-body-color: #333;">
<!-- ... -->
</body>
页面默认值
¥Page defaults
<html>
和 <body>
元素已更新,以提供更好的页面范围默认值。进一步来说:
¥The <html>
and <body>
elements are updated to provide better page-wide defaults. More specifically:
-
box-sizing
在每个元素上全局设置 - 包括*::before
和*::after
,到border-box
。这可确保元素的声明宽度不会因填充或边框而超出。¥The
box-sizing
is globally set on every element—including*::before
and*::after
, toborder-box
. This ensures that the declared width of element is never exceeded due to padding or border.-
<html>
上没有声明基font-size
,但假定为16px
(浏览器默认值)。font-size: 1rem
应用在<body>
上,以便通过媒体查询轻松响应类型缩放,同时尊重用户偏好并确保更易于访问的方法。可以通过修改$font-size-root
变量来覆盖此浏览器默认值。¥No base
font-size
is declared on the<html>
, but16px
is assumed (the browser default).font-size: 1rem
is applied on the<body>
for easy responsive type-scaling via media queries while respecting user preferences and ensuring a more accessible approach. This browser default can be overridden by modifying the$font-size-root
variable.
-
-
<body>
还设置了全局font-family
、font-weight
、line-height
和color
。稍后一些表单元素继承了这一点,以防止字体不一致。¥The
<body>
also sets a globalfont-family
,font-weight
,line-height
, andcolor
. This is inherited later by some form elements to prevent font inconsistencies. -
为了安全起见,
<body>
有一个声明的background-color
,默认为#fff
。¥For safety, the
<body>
has a declaredbackground-color
, defaulting to#fff
.
原生字体堆栈
¥Native font stack
Bootstrap 利用 “原生字体堆栈” 或 “系统字体堆栈” 在每个设备和操作系统上实现最佳文本渲染。这些系统字体是专门针对当今的设备而设计的,具有改进的屏幕渲染、可变字体支持等。了解有关 这篇 Smashing Magazine 文章中的原生字体堆栈 的更多信息。
¥Bootstrap utilizes a “native font stack” or “system font stack” for optimum text rendering on every device and OS. These system fonts have been designed specifically with today’s devices in mind, with improved rendering on screens, variable font support, and more. Read more about native font stacks in this Smashing Magazine article.
$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;
请注意,由于字体堆栈包含表情符号字体,因此许多常见的符号/dingbat Unicode 字符将渲染为彩色象形文本。它们的外观会有所不同,具体取决于浏览器/平台的原生表情符号字体中使用的样式,并且它们不会受到任何 CSS color
样式的影响。
¥Note that because the font stack includes emoji fonts, many common symbol/dingbat Unicode characters will be rendered as multicolored pictographs. Their appearance will vary, depending on the style used in the browser/platform’s native emoji font, and they won’t be affected by any CSS color
styles.
此 font-family
应用于 <body>
并在整个 Bootstrap 中自动全局继承。要切换全局 font-family
,请更新 $font-family-base
并重新编译 Bootstrap。
¥This font-family
is applied to the <body>
and automatically inherited globally throughout Bootstrap. To switch the global font-family
, update $font-family-base
and recompile Bootstrap.
标题
¥Headings
所有标题元素 — <h1>
— <h6>
均已删除 margin-top
、设置 margin-bottom: .5rem
和收紧 line-height
。虽然标题默认继承 color
,但你也可以通过可选的 CSS 变量 --bs-heading-color
覆盖它。
¥All heading elements—<h1>
—<h6>
have their margin-top
removed, margin-bottom: .5rem
set, and line-height
tightened. While headings inherit their color
by default, you can also override it via optional CSS variable, --bs-heading-color
.
Heading | Example |
---|---|
<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 |
段落
¥Paragraphs
所有 <p>
元素均已删除 margin-top
,并设置 margin-bottom: 1rem
以方便间距。
¥All <p>
elements have their margin-top
removed and margin-bottom: 1rem
set for easy spacing.
This is an example paragraph.
<p>This is an example paragraph.</p>
链接
¥Links
链接有默认的 color
和下划线。虽然链接在 :hover
上更改,但它们不会根据某人是否 :visited
链接而更改。他们也没有收到特殊的 :focus
款式。
¥Links have a default color
and underline applied. While links change on :hover
, they don’t change based on whether someone :visited
the link. They also receive no special :focus
styles.
<a href="#">This is an example link</a>
从 v5.3.x 开始,链接 color
使用 rgba()
和新的 -rgb
CSS 变量设置,允许轻松自定义链接颜色不透明度。使用 --bs-link-opacity
CSS 变量更改链接颜色不透明度:
¥As of v5.3.x, link color
is set using rgba()
and new -rgb
CSS variables, allowing for easy customization of link color opacity. Change the link color opacity with the --bs-link-opacity
CSS variable:
<a href="#" style="--bs-link-opacity: .5">This is an example link</a>
占位符链接(没有 href
的链接)以更具体的选择器为目标,并将其 color
和 text-decoration
重置为其默认值。
¥Placeholder links—those without an href
—are targeted with a more specific selector and have their color
and text-decoration
reset to their default values.
<a>This is a placeholder link</a>
水平规则
¥Horizontal rules
<hr>
元素已被简化。与浏览器默认值类似,<hr>
通过 border-top
进行样式设置,具有默认的 opacity: .25
,并通过 color
自动继承其 border-color
,包括通过父级设置 color
时。可以使用文本、边框和不透明度工具修改它们。
¥The <hr>
element has been simplified. Similar to browser defaults, <hr>
s are styled via border-top
, have a default opacity: .25
, and automatically inherit their border-color
via color
, including when color
is set via the parent. They can be modified with text, border, and opacity utilities.
<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">
列表
¥Lists
所有列表(<ul>
、<ol>
和 <dl>
)均删除了 margin-top
并删除了 margin-bottom: 1rem
。嵌套列表没有 margin-bottom
。我们还重置了 <ul>
和 <ol>
元素上的 padding-left
。
¥All lists—<ul>
, <ol>
, and <dl>
—have their margin-top
removed and a margin-bottom: 1rem
. Nested lists have no margin-bottom
. We’ve also reset the padding-left
on <ul>
and <ol>
elements.
-
All lists have their top margin removed
-
And their bottom margin normalized
-
Nested lists have no bottom margin
-
This way they have a more even appearance
-
Particularly when followed by more list items
-
-
The left padding has also been reset
- Here’s an ordered list
- With a few list items
- It has the same overall look
- As the previous unordered list
为了更简单的样式、清晰的层次结构和更好的间距,描述列表已更新 margin
。<dd>
将 margin-left
重置为 0
并添加 margin-bottom: .5rem
。<dt>
为粗体。
¥For simpler styling, clear hierarchy, and better spacing, description lists have updated margin
s. <dd>
s reset margin-left
to 0
and add margin-bottom: .5rem
. <dt>
s are bolded.
- 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.
内联代码
¥Inline code
用 <code>
封装内联代码片段。请务必转义 HTML 尖括号。
¥Wrap inline snippets of code with <code>
. Be sure to escape HTML angle brackets.
<section>
should be wrapped as inline.
For example, <code><section></code> should be wrapped as inline.
代码块
¥Code blocks
对多行代码使用 <pre>
。再次强调,请确保转义代码中的任何尖括号,以便正确渲染。<pre>
元素被重置以删除其 margin-top
并使用 rem
单位作为其 margin-bottom
。
¥Use <pre>
s for multiple lines of code. Once again, be sure to escape any angle brackets in the code for proper rendering. The <pre>
element is reset to remove its margin-top
and use rem
units for its 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>
变量
¥Variables
使用 <var>
标签来指示变量。
¥For indicating variables use the <var>
tag.
<var>y</var> = <var>m</var><var>x</var> + <var>b</var>
用户输入
¥User input
使用 <kbd>
表示通常通过键盘输入的输入。
¥Use the <kbd>
to indicate input that is typically entered via keyboard.
To edit settings, press 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>
样本输出
¥Sample output
要指示程序的示例输出,请使用 <samp>
标签。
¥For indicating sample output from a program use the <samp>
tag.
<samp>This text is meant to be treated as sample output from a computer program.</samp>
表格
¥Tables
表格稍微调整为样式 <caption>
,折叠边框,并确保始终一致的 text-align
。.table
类 对边框、填充等进行了其他更改。
¥Tables are slightly adjusted to style <caption>
s, collapse borders, and ensure consistent text-align
throughout. Additional changes for borders, padding, and more come with the .table
class.
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>
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>
表单
¥Forms
各种表单元素已重新启动以获得更简单的基本样式。以下是一些最显着的变化:
¥Various form elements have been rebooted for simpler base styles. Here are some of the most notable changes:
-
<fieldset>
没有边框、填充或边距,因此它们可以轻松用作单个输入或输入组的封装器。¥
<fieldset>
s have no borders, padding, or margin so they can be easily used as wrappers for individual inputs or groups of inputs. -
<legend>
与字段集一样,也经过重新设计以显示为某种标题。¥
<legend>
s, like fieldsets, have also been restyled to be displayed as a heading of sorts. -
<label>
设置为display: inline-block
以允许应用margin
。¥
<label>
s are set todisplay: inline-block
to allowmargin
to be applied. -
<input>
、<select>
、<textarea>
和<button>
主要由规范化处理,但重新启动会删除它们的margin
并设置line-height: inherit
。¥
<input>
s,<select>
s,<textarea>
s, and<button>
s are mostly addressed by Normalize, but Reboot removes theirmargin
and setsline-height: inherit
, too. -
<textarea>
被修改为只能垂直调整大小,因为水平调整大小通常是 “breaks” 页面布局。¥
<textarea>
s are modified to only be resizable vertically as horizontal resizing often “breaks” page layout. -
<button>
和<input>
按钮元素有cursor: pointer
,有:not(:disabled)
。¥
<button>
s and<input>
button elements havecursor: pointer
when:not(:disabled)
.
下面演示了这些变化以及更多变化。
¥These changes, and more, are demonstrated below.
按钮上的指针
¥Pointers on buttons
重新启动包括对 role="button"
的增强,可将默认光标更改为 pointer
。将此属性添加到元素中以帮助指示元素是交互式的。对于 <button>
元素来说,这个角色不是必需的,它们有自己的 cursor
更改。
¥Reboot includes an enhancement for role="button"
to change the default cursor to pointer
. Add this attribute to elements to help indicate elements are interactive. This role isn’t necessary for <button>
elements, which get their own cursor
change.
<span role="button" tabindex="0">Non-button element button</span>
杂项元素
¥Misc elements
地址
¥Address
<address>
元素已更新,以将浏览器默认 font-style
从 italic
重置为 normal
。现在也继承了 line-height
,并添加了 margin-bottom: 1rem
。<address>
用于显示最近祖级(或整个作品)的联系信息。通过以 <br>
结束行来保留格式。
¥The <address>
element is updated to reset the browser default font-style
from italic
to normal
. line-height
is also now inherited, and margin-bottom: 1rem
has been added. <address>
s are for presenting contact information for the nearest ancestor (or an entire body of work). Preserve formatting by ending lines with <br>
.
1123 Fictional St,
San Francisco, CA 94103
P: (123) 456-7890 Full Name
first.last@example.com
块引用
¥Blockquote
块引用上的默认 margin
是 1em 40px
,因此我们将其重置为 0 0 1rem
,以便与其他元素更加一致。
¥The default margin
on blockquotes is 1em 40px
, so we reset that to 0 0 1rem
for something more consistent with other elements.
A well-known quote, contained in a blockquote element.
Someone famous in Source Title
行内元素
¥Inline elements
<abbr>
元素接受基本样式,使其在段落文本中脱颖而出。
¥The <abbr>
element receives basic styling to make it stand out amongst paragraph text.
概括
¥Summary
摘要中的默认 cursor
是 text
,因此我们将其重置为 pointer
,以表明可以通过单击该元素进行交互。
¥The default cursor
on summary is text
, so we reset that to pointer
to convey that the element can be interacted with by clicking on it.
Some details
More info about the details.
Even more details
Here are even more details about the details.
HTML5 [hidden]
属性
¥HTML5 [hidden]
attribute
HTML5 添加了 名为 [hidden]
的新全局属性,默认样式为 display: none
。借鉴 PureCSS 的想法,我们通过设置 [hidden] { display: none !important; }
来帮助防止其 display
被意外覆盖,从而改进了此默认设置。
¥HTML5 adds a new global attribute named [hidden]
, which is styled as display: none
by default. Borrowing an idea from PureCSS, we improve upon this default by making [hidden] { display: none !important; }
to help prevent its display
from getting accidentally overridden.
<input type="text" hidden>
[hidden]
is not compatible with jQuery’s $(...).hide()
and $(...).show()
methods, we don’t specifically endorse [hidden]
over other techniques for managing the display
of elements.
要仅切换元素的可见性(意味着其 display
未修改并且该元素仍然可以影响文档的流程),请改用 .invisible
类。
¥To merely toggle the visibility of an element, meaning its display
is not modified and the element can still affect the flow of the document, use the .invisible
class instead.