Answering CSS Interview Questions with Simple English

58 阅读3分钟

前言

这只是一个正在学习英语的我记录的一些关于 CSS 问题以及答复的参考范文,只是用于个人记录。

学语言不是死记硬背,而是学会语言本身。

Can you describe the CSS box model briefly?

Yes. The CSS box model means every element is like a box: it has content in the middle, then padding around the content, a border around the padding, and margin outside the border.

Can you explain the difference between the box-sizing: content-box and box-sizing: border-box properties in CSS?

Sure! So, with box-sizing: content-box—which is actually the default—the width and height you set only apply to the content area. That means if you add padding or borders, the total size of the element gets bigger. But with box-sizing: border-box, the width and height include the content, padding, and border all together. So the total size stays exactly what you set, which is usually easier to work with.

Can you explain the different CSS position values?

By default, the position property is set to static, meaning the element follows the normal document flow, and properties like top, right, bottom, left, and z-index do not affect it. When we use relative positioning, the element is still part of the normal flow, but it can be offset from its original position. With absolute positioning, the element is removed from the normal flow and positioned relative to its nearest positioned ancestor. For fixed positioning, the element is removed from the flow as well, but it’s positioned relative to the viewport, meaning it stays in the same spot even when scrolling. Finally, sticky positioning is a combination of relative and fixed, where the element is treated as relative until it reaches a certain scroll point, after which it behaves like fixed.

Can you explain some common display values in CSS?

Sure! The most common ones are block, inline, inline-block, flex, grid, and none.
So, block elements take the full width and start on a new line — like paragraphs.
Inline elements flow with text and don’t break the line — like links.
Inline-block is a mix: it stays in the line, but you can set width and height.
Then there’s flex — that’s for one-dimensional layouts, like navigation bars.
Grid is for two-dimensional layouts, like full page designs.
And display: none just removes the element entirely from the page.

Most elements are either block or inline by default, depending on their tag.

Can you explain what a CSS selector is?

CSS selectors are patterns that we use to target HTML elements so we can style them. The most common ones include element, class, and ID selectors. There are also attribute selectors, pseudo-classes like :hover, and pseudo-elements like ::before. We can even combine selectors using combinators such as > or +. Together, they allow very fine control over how we apply styles to elements on a web page.

Can you explain how CSS specificity works?

First, !important is a special keyword that overrides all other rules, no matter their specificity. But it’s best to avoid it when possible.

For normal rules, the browser uses specificity to decide which one wins. Specificity is calculated based on the types of selectors used:

  • ID selectors count the most,
  • then classes, attributes, and pseudo-classes (like .btn, [type="text"], or :hover),
  • and finally element selectors and pseudo-elements (like div or ::before).

The browser compares these values from highest to lowest weight—so IDs are checked first, then classes, then elements.
If two rules have the same specificity, the one that appears later in the CSS file wins.

注意这里有个认知错误,我一直以为权重是4位数,第一位是内联样式。结果 AI 告诉我:只有三个,如上面回答所示。

例子:

<div id="box" style="color: red">
#box {
  color: black;
}

✅ 答案:color: red 生效(文字是红色)


🔍 为什么?

因为 内联样式(style="...")的 specificity 比 ID 选择器更高

具体对比:

style="color: red"内联样式1,0,0
#box { color: black; }ID 选择器1,0,0

⚠️ 等等!它们的 specificity 一样?那为什么红色生效?

👉 关键在于:当 specificity 相同时,CSS 的层叠规则(cascade)会看“来源顺序”

但更重要的是:内联样式在层叠顺序中天然优先级更高,即使 specificity 数值相同。


📚 更准确的解释(来自 CSS 规范):

CSS 的最终样式由 层叠(Cascade) 决定,它考虑多个因素,按优先级从高到低:

  1. !important

  2. 来源类型(Origin)

    • 内联样式(style 属性)
    • 嵌入/外部样式表(<style><link>
  3. Specificity

  4. 源代码顺序(后写的覆盖先写的)

✅ 所以,即使内联样式和 ID 选择器的 specificity 都是 1,0,0,内联样式仍然优先,因为它属于更高优先级的“来源类型”。