| English | 释义 | 拼音 |
|---|---|---|
| padding/ˈpædɪŋ/ | 内边距 | nèi biān jù |
| border/ˈbɔːdə/ | 边框 | biān kuāng |
| CSS/ˌsiː es ˈes/ | 层叠样式表 | céng dié yàng shì biǎo |
| selector/sɪˈlektə/ | 选择器 | xuǎn zé qì |
| declaration/ˌdekləˈreɪʃn/ | 声明 | shēng míng |
| the box model/ðə bɒks ˈmɒdl/ | 盒模型 | hé mó xíng |
| margin/ˈmɑːdʒɪn/ | 外边距 | wài biān jù |
| responsive design/rɪˈspɒnsɪv dɪˈzaɪn/ | 响应式设计 | xiǎng yìng shì shè jì |
| media query/ˈmiːdiːə ˈkwɪərɪ/ | 媒体查询 | méi tǐ chá xún |
A 320-pixel card still overflows a 320-pixel screen
- The card's declared width is not always its total width. Padding and borders can add space outside the content width.
- CSS 层叠样式表 applies presentation rules. A selector 选择器 chooses elements; a declaration 声明 gives a property and value, such as
color: navy.
请给出一个保留可复用样式表实用规则的实际理由。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
示例:“共享样式表允许我们一致地更新匹配的卡片,而无需编辑每个HTML元素。”
Account for every part of the box
- The box model 盒模型 has content, padding 内边距, border 边框 and margin 外边距. Padding and border are inside the painted box; margin separates it from neighbours.
- With
content-box, width 320 px, padding 16 px on both sides and borders 2 px on both sides give 356 px before margins. Withborder-box, the declared 320 px includes that padding and border.

同一宽度,两种盒模型
$W_{content-box}=W+2P+2B$
用本课的320像素卡片比较内容宽度与外部宽度。
从内容向外排列盒模型区域。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
内边距和边框包围内容;外边距将盒子与相邻元素分隔开。
使用 content-box 时,内容宽度为 320 px,左右内边距各为 16 px,左右边框各为 2 px。在应用外边距之前,其总宽度是多少像素?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
320 + 16 + 16 + 2 + 2 = 356 px。
Make the width flexible
- Responsive design 响应式设计 adapts content and layout to available space. This card fills its container up to 400 px, with padding and border included.
- Use the rule on an element with
class="card". Long unbroken content, parent margins and other rules still need checking; one declaration cannot guarantee the whole page fits.
.card {
box-sizing: border-box;
width: 100%;
max-width: 400px;
padding: 16px;
border: 2px solid currentColor;
overflow-wrap: anywhere;
}
Inside a 280 px container, this card is 280 px wide including its padding and border. Inside a 700 px container, it stops at 400 px. Its outer margins are still additional space.
一个带有内边距的卡片应填满其容器直至400像素(包括其边框)。哪组规则最适用?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
border-box将内边距和边框包含在声明的宽度中。父级布局和内容的测试仍需要进行。
Change layout only when it needs to change
- A media query 媒体查询 applies rules under a condition. For example, change two columns to one when they become too narrow to read.
- Choose the breakpoint from the content, then test several widths and larger text. Do not assume that all phones or all laptops have the same usable width.
Inspect the rule that actually wins
- The cascade considers factors including origin, importance and layers before selector specificity. For otherwise comparable rules, an ID is more specific than a class; a class is more specific than an element selector.
- When comparable rules have equal specificity, later source order can decide. Use browser developer tools to check matched rules, the loaded stylesheet and computed values.
!important participates in the cascade; it does not break CSS. Avoid using it as an unexplained patch. Check the cause first: a missing stylesheet, invalid declaration or overriding rule may all explain an unexpected result.
同一源域和层中的两条普通规则使用相同的选择器 .card 并设置了冲突的颜色。没有其他决定因素不同。哪种颜色会胜出?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
相同的选择性并不意味着顺序无关紧要。对于可比较且并列的规则,后出现的源顺序决定结果。
!important是CSS级联规则的一部分,但不应作为未加解释的补丁使用。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
重要性影响优先级。检查冲突并故意使用它,而不是声称它破坏了CSS。
将观察结果与下一步有效的检查项进行匹配。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
检查实际原因,而不是假设所有问题都源于选择器特异性。
Test the box and the reading experience
- Try narrow and wide containers, keyboard focus and enlarged text. Check that text is readable in both colour themes and that content can wrap.
- Explain one overflow repair using the box calculation and one style conflict using the actual matching rules.
Calculate the whole box, prefer flexible sizes, then inspect the cascade. Specificity matters, but it is not the only deciding factor.
Source: W3C CSS cascade.
卡片在正常字号下可以正常显示,但放大文字后会出现溢出。你应该怎么做?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
响应式行为包括调整文本和内容的约束条件,而不仅仅是一屏截图的适配。