Advanced Interview Questions

What is a stacking context?

A group of elements stacked together in the z-axis. It is created by the root, positioned elements with a z-index, and properties like opacity < 1, transform, filter, isolation: isolate. Children can never be painted above elements outside their parent's stacking context, which explains many "z-index does not work" bugs.

What are CSS custom properties and how do they differ from Sass variables?

Custom properties (--brand: #04AA6D; color: var(--brand)) live in the browser: they inherit, can be changed per element or with media queries, and can be updated by JavaScript at runtime. Sass variables are replaced at build time and do not exist in the final CSS.

Which properties are cheap to animate, and why?

transform and opacity: they only affect the composite step and can run on the GPU. Animating width, top or margin triggers layout and paint on every frame and can cause jank.

What is BFC (Block Formatting Context)?

An isolated layout region where floats are contained and margins of children do not collapse with the outside. It is created by overflow other than visible, display: flow-root, flex/grid items, floats and absolutely positioned elements. display: flow-root is the modern way to contain floats.

Explain :is(), :where() and :has().

  • :is() groups selectors and takes the highest specificity of its arguments.
  • :where() groups selectors with zero specificity (easy to override).
  • :has() selects an element based on its descendants or following siblings: the long-awaited "parent selector".

How would you structure CSS for a large project?

  1. Design tokens as custom properties (colors, spacing, fonts).
  2. A small reset and base typography.
  3. Component styles with a naming convention (BEM) or CSS Modules.
  4. Utilities last.
  5. Linting (stylelint), and removing unused CSS in the build.