DrillLab
第 01 / 25 节LESSON 01 / 25约 14 分钟~14 min

HTML 五问5 questions on HTML

块级与行内、事件冒泡与捕获、meta、语义化、无障碍。Block and inline elements, event bubbling and capturing, meta tags, semantic elements, accessibility.

面试 · 第 1 部分Interview · Part 1
这一页有什么On this page6
学完这节你会After this lesson you can
  • 说清块级和行内元素的三处实际差别Explain three real differences between block and inline elements
  • 画出事件从 window 到目标再回到 window 的完整路径Draw the full path of an event, from window down to the target and back up to window
  • 说明语义化标签除了「好看」之外的两个真实收益Name two real benefits of semantic elements beyond tidier markup
  • 举出无障碍(a11y)的具体做法,而不是空谈概念Give concrete accessibility (a11y) practices instead of talking about the idea
这在考试里考什么What the exam does with this

HTML 题是筛人题:答不上来直接出局,答得好也拿不到加分。所以目标不是讲深,而是每道都能在 30 秒内说清楚,并且举得出一个例子。事件冒泡/捕获那道除外 —— 它常被追问到事件委托和 React 的合成事件,值得往深里准备。HTML questions are filters: missing one takes you out, and answering one well earns no extra credit. So the goal is not depth. The goal is to answer each one clearly in 30 seconds and give one example. Bubbling and capturing is the exception — it often leads on to event delegation and React synthetic events, so prepare that one in depth.

§01

块级元素 vs 行内元素Block elements vs inline elements

#269 Block element vs Inline element

一句话:块级元素独占一行、宽高可控; 行内元素跟着文字流走、宽高由内容决定。

具体差别就三处,记住这三条就够答:

  • 换行—— 块级前后自动断行(divph1ul); 行内不断行(spanastrongimg)。
  • 宽高—— 块级可以设 width /height;行内设了不生效
  • 内外边距—— 块级四个方向都生效; 行内的上下 margin 不生效, 上下 padding 视觉上会溢出但不撑开行高。

会追问:「那 inline-block 呢?」 —— 它是折中:不换行(像行内),但宽高和上下 margin 都生效(像块级)。 导航按钮常用它。

还会追问:img 是行内元素, 为什么能设宽高?」—— 因为它是替换元素(replaced element), 内容由外部资源决定,浏览器对它网开一面。inputvideo 同理。 这个点答出来会加分。

In one line: a block element takes the whole line and you can set its width and height; an inline element flows with the text and is sized by its content.

There are exactly three differences worth remembering:

  • Line breaks — block elements break before and after (div, p, h1, ul); inline ones do not (span, a, strong, img).
  • Width and height — settable on block elements, ignored on inline ones.
  • Margin and padding — all four sides work on block elements; on inline elements vertical margin does nothing, and vertical padding overflows visually without pushing the line apart.

Follow-up: “What about inline-block?” — it is the compromise: no line break (like inline) but width, height and vertical margin all work (like block). Nav buttons use it constantly.

Also asked:img is inline, so why can you set its size?” — because it is a replaced element: its content comes from an external resource, so the browser makes an exception. Same for input and video. Getting this one right scores points.

§02

事件冒泡 vs 事件捕获Event bubbling vs event capturing

#380 Event bubbling vs Event capturing

一句话:一次点击在 DOM 里走三段 —— 先从 window 往下到目标(捕获), 在目标上触发(目标阶段),再从目标往上回到window(冒泡)。捕获从外到内,冒泡从内到外。

addEventListener 第三个参数决定你在哪一段听: 默认 false 听冒泡,true(或 { capture: true })听捕获。

为什么默认是冒泡?因为绝大多数时候你想知道的是 「用户点了什么」,从内往外传最自然 —— 而且这才让事件委托成为可能(见 #289)。

会追问:「怎么阻止?」

  • e.stopPropagation()—— 别再往上(或往下)传了。
  • e.stopImmediatePropagation()—— 连同一个元素上的其他监听器都别跑了。
  • e.preventDefault()—— 阻止的是默认行为(链接跳转、表单提交), 和传播完全无关。这两个最容易混,别答错。

还会追问:「有哪些事件不冒泡?」——focusblurloadmouseentermouseleave。 需要委托时用它们的冒泡版:focusin /focusout / mouseover /mouseout

In one line: a click travels the DOM in three phases — down from window to the target (capture), fires on the target, then back up to window (bubble). Capture goes outside-in, bubbling goes inside-out.

The third argument to addEventListener picks the phase you listen in: false (the default) is bubbling, true (or { capture: true }) is capture.

Why is bubbling the default? Because what you almost always want to know is “what did the user click”, and inside-out is the natural direction for that — it is also what makes event delegation possible (see #289).

Follow-up: “How do you stop it?”

  • e.stopPropagation() — stop travelling further up (or down).
  • e.stopImmediatePropagation() — also skip other listeners on the same element.
  • e.preventDefault() — cancels the default behaviour (link navigation, form submit) and has nothing to do with propagation. These two get mixed up most often; do not confuse them.

Also asked: “Which events do not bubble?” —focus, blur, load, mouseenter, mouseleave. When you need to delegate, use their bubbling counterparts: focusin / focusout / mouseover / mouseout.

JavaScript三个阶段的完整顺序The full order of the three phases示意Illustrative
1// <div id="outer"><div id="inner"><button id="btn">点我</button></div></div>
2
3outer.addEventListener("click", () => console.log("outer 捕获"), true);
4inner.addEventListener("click", () => console.log("inner 捕获"), true);
5btn .addEventListener("click", () => console.log("btn 目标"));
6inner.addEventListener("click", () => console.log("inner 冒泡"));
7outer.addEventListener("click", () => console.log("outer 冒泡"));
8
9// 点击 btn 的输出顺序:
10// outer 捕获 -> inner 捕获 -> btn 目标 -> inner 冒泡 -> outer 冒泡
1// <div id="outer"><div id="inner"><button id="btn">Click me</button></div></div>
2
3outer.addEventListener("click", () => console.log("outer capture"), true);
4inner.addEventListener("click", () => console.log("inner capture"), true);
5btn .addEventListener("click", () => console.log("btn target"));
6inner.addEventListener("click", () => console.log("inner bubble"));
7outer.addEventListener("click", () => console.log("outer bubble"));
8
9// Output order when btn is clicked:
10// outer capture -> inner capture -> btn target -> inner bubble -> outer bubble
面试里画得出这个顺序,基本就过了。注意目标元素上的监听器不分捕获/冒泡,按注册顺序执行。If you can draw this order in an interview, that question is handled. One detail: on the target element itself there is no capture or bubble distinction, so those listeners run in the order they were registered.
§03

meta 标签有什么用What is the meta tag for?

#381 What is the importance of the meta tag?

一句话:meta 放的是 「关于这个页面的信息」—— 浏览器、搜索引擎、社交平台读它,用户看不到它。

真正天天用到的就四个,答这四个就够:

  • <meta charset="UTF-8">——必须放在 head 最前面。 没有它中文会乱码,因为浏览器得先知道用什么编码去解析后面的字节。
  • viewport——移动端适配的前提。 不写这一行,手机浏览器会假装自己有 980px 宽然后整页缩小, 你写的所有响应式 CSS 全白费(见 #274)。
  • description—— 搜索结果里那段摘要。 它不影响排名,但影响点击率。
  • Open Graphog:titleog:image)—— 链接分享到微信 / Slack / Twitter 时显示的卡片。

会追问:keywords 还有用吗?」 —— 没用了,Google 早就不看,因为被滥用成了堆关键词。 这个点能答出来说明你不是背的旧教程。

In one line: meta carries information about the page — browsers, search engines and social platforms read it; users never see it.

Only four of them actually matter day to day:

  • <meta charset="UTF-8"> must be first in the head. Without it non-ASCII text turns to mojibake, because the browser has to know the encoding before it can parse the bytes that follow.
  • viewport the precondition for mobile. Without this line a phone browser pretends it is 980px wide and scales the whole page down, which throws away every responsive rule you wrote (see #274).
  • description — the snippet in search results. It does not affect ranking, but it affects click-through.
  • Open Graph (og:title, og:image) — the card shown when the link is shared into Slack, Twitter or a chat app.

Follow-up: “Does keywords still do anything?” — no. Google stopped reading it long ago because it was abused into keyword stuffing. Knowing this shows you are not reciting an old tutorial.

Text实际会写的那几行示意Illustrative
1<head>
2 <meta charset="UTF-8"> <!-- 必须最先 -->
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <meta name="description" content="一句话说明这个页面是干什么的">
5 <meta property="og:title" content="分享出去显示的标题">
6 <meta property="og:image" content="https://…/cover.png">
7 <title>页面标题</title>
8</head>
1<head>
2 <meta charset="UTF-8"> <!-- must come first -->
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <meta name="description" content="one sentence on what this page is for">
5 <meta property="og:title" content="the title shown when shared">
6 <meta property="og:image" content="https://…/cover.png">
7 <title>Page title</title>
8</head>
§04

什么是语义化标签What are semantic elements?

#382 What are Semantic Elements?

一句话:标签名本身说明了内容的角色 ——header / nav / main /article / section /aside / footer, 而 divspan 什么都没说。

关键是别答成「代码更好看」。语义化有两个可以量化的真实收益:

  • 屏幕阅读器能导航。视障用户可以按「跳到主内容」「列出所有标题」来浏览 —— 这些功能依赖标签语义。 一整页 div,读屏软件只能从头念到尾。
  • 搜索引擎知道哪块是正文。main 里的内容权重高于 aside

会追问:sectiondiv 到底怎么选?」—— 判断标准是「这块内容有没有自己的标题」。 有(能配一个 h2)就用 section; 纯粹为了布局套一层就用 div为了样式而套的容器就该是 div, 硬换成 section 反而污染了大纲。

In one line: the tag name itself states the role of the content — header / nav / main /article / section / aside /footer — whereas div and span say nothing at all.

Do not answer “the code looks nicer”. Semantics buys you two things you can actually measure:

  • Screen readers can navigate. A blind user can jump to the main content or list every heading — and those features depend on tag semantics. On a page of nothing but divs, a screen reader can only read from the top.
  • Search engines know which part is the article. Content in main weighs more than content in aside.

Follow-up: “So how do you choose between section and div?” — the test is “does this block have its own heading?” If it does (you could give it an h2), use section. If it is a wrapper you added purely for layout, use div. A container that exists for styling should be a div — forcing it to be a section just pollutes the document outline.

§05

无障碍、可用性、包容性Accessibility, usability and inclusion

#385 Could you explain accessibility, usability, and inclusion? Give some examples of each one in terms of web design.

一句话区分:

  • 无障碍(accessibility, a11y)——有障碍的人能不能用。视障、听障、 运动障碍、认知障碍。
  • 可用性(usability)——能用的人用得顺不顺。找得到、看得懂、不容易点错。
  • 包容性(inclusion)——范围够不够宽。老年人、网速慢的、 用小屏手机的、非母语用户、临时单手抱着孩子操作的。

三者是包含关系:无障碍是包容性的一部分, 可用性差的东西对所有人都差。

每个给一个例子(面试要的就是例子):

  • a11y:给 imgalt;表单 labelhtmlFor 关联到 input保证键盘能 Tab 到所有交互元素, 并且焦点环别用 outline: none 删掉。
  • usability:按钮文字写「保存草稿」 而不是「提交」;表单报错指向出错的那个字段, 而不是页面顶部一句「表单有误」。
  • inclusion:正文对比度至少 4.5:1; 不用颜色作为唯一的信息载体 (红绿色盲看不出「红色是错的」,要同时给图标或文字); 首屏在 3G 下也能出内容。

会追问:「怎么测?」—— 键盘走一遍(不碰鼠标能不能完成主流程)、 Chrome DevTools 的 Lighthouse 跑一次 a11y 评分、axe 插件扫一遍。说得出工具名比说概念更可信。

The distinction in one line each:

  • Accessibility (a11y) can people with disabilities use it: vision, hearing, motor, cognitive.
  • Usability how smoothly can people who can use it, use it: can they find things, understand them, avoid mis-taps.
  • Inclusion how wide is the net: older users, slow connections, small screens, non-native speakers, someone operating one-handed while holding a child.

The three nest: accessibility is part of inclusion, and anything with poor usability is worse for everyone.

One example each — examples are what the interviewer wants:

  • a11y: write alt on images; tie a label to its input with htmlFor; make sure Tab reaches every interactive element and do not delete the focus ring with outline: none.
  • usability: label the button “Save draft” rather than “Submit”; put the validation error on the field that failed, not a generic banner at the top of the page.
  • inclusion: at least 4.5:1 contrast for body text; never use colour as the only carrier of meaning (someone with red-green colour blindness cannot see that “red means wrong”, so add an icon or a word); make the first screen usable on 3G.

Follow-up: “How do you test it?” — walk the main flow with the keyboard only; run Lighthouse’s a11y audit in Chrome DevTools; scan with the axe extension. Naming the tools is more convincing than naming the concepts.

迁移Transfer

换一道题也能用Works on other problems too

考试不会原题重考。真正能带走的是「看到这种信号 → 伸手去拿这个解法」。The exam will not reuse the same question. What you take away is the reflex: see this signal, reach for that solution.

问「为什么行内元素设不了高」Asked why you cannot set the height of an inline element
行内跟文字流走;替换元素例外Inline elements follow the text flow; replaced elements are the exception
问事件顺序Asked about the order events fire in
捕获从外到内 → 目标 → 冒泡从内到外Capture goes outside in, then the target, then bubbling goes inside out
混淆 stopPropagation / preventDefaultstopPropagation and preventDefault get mixed up
一个管传播,一个管默认行为One stops the event from travelling, the other stops the default action
问语义化的好处Asked what semantic elements are good for
读屏能导航 + 搜索引擎分得清正文,别答「好看」Screen readers can navigate and search engines can tell where the main text is; do not answer that it looks tidier
问无障碍Asked about accessibility
举具体例子:alt、label、键盘焦点、对比度Give concrete examples: alt text, label, keyboard focus, contrast
这节的要点What to take away
  1. 块级 vs 行内看三处:换行、宽高、上下 margin;img/input 是替换元素所以能设宽高。Block and inline differ in three places: starting a new line, width and height, and top and bottom margin; img and input are replaced elements, so they do take a width and height.
  2. 事件三阶段:捕获(外→内)→ 目标 → 冒泡(内→外);默认监听冒泡。An event has three phases: capture (outside in), target, then bubbling (inside out); a listener runs in the bubbling phase by default.
  3. stopPropagation 管传播,preventDefault 管默认行为,两回事。stopPropagation stops the event from travelling, preventDefault stops the default action; they are two different things.
  4. meta 里真正重要的是 charset(防乱码)和 viewport(移动端前提)。The two meta tags that matter are charset, which keeps text from rendering as garbled characters, and viewport, without which mobile layout does not work.
  5. 语义化的收益是读屏导航和搜索权重,不是「代码好看」。Semantic elements pay off in screen reader navigation and search ranking, not in tidier code.
  6. 无障碍 ⊂ 包容性;答题一定要给具体例子和测试工具。Accessibility is one part of inclusion; always answer with concrete examples and the tools you test with.

接下来What next

  1. 接着看下一节Continue to the next lessonCSS 八问8 questions on CSS
    下一节Next lesson
  2. 可选:再巩固一下Optional: reinforce it这一节的 5 道八股5 questions from this lesson
读完并且做过上面的练习了吗?Read it and worked through the exercises above?