Skip to content
TryHired.
Interview prep · Technical · Google Chrome 157.0.8103.40 headless

CSS interview questions: answers with real browser output

The layout, cascade and animation questions front-end developers get, answered in plain words, with each example run in a real browser.

Updated Nov 15, 2026 · Examples run with Google Chrome 157.0.8103.40 headless · questions: TryHired
Quick answer

CSS interviews cover the cascade and specificity, the box model and box-sizing, margin collapsing, positioning and stacking contexts, Flexbox and Grid sizing, em and rem, custom properties, cascade layers, :is() and :where(), and animation: transitions versus keyframe animations, fill modes, animation events, what is cheap to animate and reduced motion. Below are 17 questions; 16 examples were loaded in headless Google Chrome and the output is what each page measured.

How the examples run

Each snippet is a small page measured in Chrome

Advice

Each example is a complete HTML snippet. The build loads it in headless Google Chrome at an 800 × 600 viewport with network access blocked, and prints whatever the snippet's script logs with console.log: computed styles, measured sizes and positions, and animation events.

Basic

Basic CSS interview questions

Advice

Basic1. How does CSS specificity work?

When rules conflict, the more specific selector wins, regardless of order. Specificity is compared as three counts: IDs, then classes, attributes and pseudo-classes, then element types. #title is (1,0,0), so it beats .page h1.headline at (0,2,1) even though that selector is longer. Only when specificity ties does the later rule win. Inline styles and !important sit above this comparison.

HTML and CSS
<style>
  #title { color: green; }
  .page h1.headline { color: red; }
  h1 { color: blue; }
</style>
<div class="page"><h1 id="title" class="headline">Hello</h1></div>
<script>
  console.log(getComputedStyle(document.getElementById('title')).color);
</script>
Output (Google Chrome 157.0.8103.40 headless)
rgb(0, 128, 0)

Checked against: W3C Selectors Level 4

Basic2. What does box-sizing: border-box change?

With the default content-box, width sets the content area only, and padding and border are added on top: 200px + 2 × 20px + 2 × 5px = 250px wide. With border-box, width includes padding and border, so the box is exactly 200px. Many projects set border-box on every element because it makes layouts easier to reason about.

HTML and CSS
<style>
  .box { width: 200px; padding: 20px; border: 5px solid; }
  .border-box { box-sizing: border-box; }
</style>
<div class="box" id="a"></div>
<div class="box border-box" id="b"></div>
<script>
  for (const id of ['a', 'b']) console.log(id, document.getElementById(id).getBoundingClientRect().width);
</script>
Output (Google Chrome 157.0.8103.40 headless)
a 250
b 200

Checked against: W3C CSS Box Sizing Level 3

Basic3. What is margin collapsing?

In normal block layout, adjoining vertical margins combine into one margin, the larger of the two (for positive margins): a 20px bottom margin above a 30px top margin leaves a 30px gap, not 50px. Margins do not collapse between flex or grid items, which is why the same two paragraphs in a flex column are 50px apart.

HTML and CSS
<style>
  p { margin: 0; }
  .one { margin-bottom: 20px; }
  .two { margin-top: 30px; }
  .flex { display: flex; flex-direction: column; }
</style>
<div id="block"><p class="one">One</p><p class="two">Two</p></div>
<div id="flex" class="flex"><p class="one">One</p><p class="two">Two</p></div>
<script>
  for (const id of ['block', 'flex']) {
    const [a, b] = document.querySelectorAll('#' + id + ' p');
    console.log(id, 'gap:', b.getBoundingClientRect().top - a.getBoundingClientRect().bottom);
  }
</script>
Output (Google Chrome 157.0.8103.40 headless)
block gap: 30
flex gap: 50

Checked against: W3C CSS 2.2: box model

Basic4. What is position: absolute positioned relative to?

To its containing block. That is the nearest ancestor that establishes one: any positioned ancestor (position other than static), and also an ancestor with a transform, a filter or layout or paint containment, even if it is not positioned. If there is none, it is the initial containing block. That is why a badge with top and right offsets lands in the corner of a card with position: relative or a transform, but at the corner of the viewport when the card has neither.

HTML and CSS
<style>
  body { margin: 0; }
  .card { position: relative; margin: 50px; width: 200px; height: 100px; }
  .card.static { position: static; }
  .card.moved { position: static; transform: translateX(0); }
  .badge { position: absolute; top: 10px; right: 10px; width: 20px; height: 20px; }
</style>
<div class="card" id="rel"><span class="badge"></span></div>
<div class="card static" id="st"><span class="badge"></span></div>
<div class="card moved" id="tr"><span class="badge"></span></div>
<script>
  for (const id of ['rel', 'st', 'tr']) {
    const r = document.querySelector('#' + id + ' .badge').getBoundingClientRect();
    console.log(id, 'badge at x =', r.left, 'y =', r.top);
  }
</script>
Output (Google Chrome 157.0.8103.40 headless)
rel badge at x = 220 y = 60
st badge at x = 770 y = 10
tr badge at x = 220 y = 360

Checked against: W3C CSS Containment Level 2, W3C Filter Effects Level 1, W3C CSS Positioned Layout Level 3, W3C CSS Transforms Level 1

Basic5. What is the difference between display: none, visibility: hidden and opacity: 0?

display: none removes the element from layout entirely: it generates no box, so the next element moves up. visibility: hidden and opacity: 0 both keep the space. They differ in interaction: the CSS Color specification says opacity does not affect hit testing, so a fully transparent button still receives clicks, and in the example it can also take keyboard focus, while the hidden one does neither. That is why opacity: 0 on its own is a poor way to hide controls.

HTML and CSS
<style>
  p { margin: 0; }
  button { display: block; width: 120px; height: 40px; }
</style>
<div id="w1"><button style="display: none">A</button><p>next</p></div>
<div id="w2"><button style="visibility: hidden">B</button><p>next</p></div>
<div id="w3"><button style="opacity: 0">C</button><p>next</p></div>
<script>
  for (const [i, label] of [[1, 'display: none'], [2, 'visibility: hidden'], [3, 'opacity: 0']]) {
    const w = document.getElementById('w' + i), b = w.querySelector('button'), p = w.querySelector('p');
    const r = b.getBoundingClientRect();
    const hit = r.height > 0 && document.elementFromPoint(r.left + 10, r.top + 10) === b;
    b.focus();
    console.log(label.padEnd(18), '| next starts', p.offsetTop - w.offsetTop, 'px down | click lands on it:', hit, '| takes focus:', document.activeElement === b);
  }
</script>
Output (Google Chrome 157.0.8103.40 headless)
display: none      | next starts 0 px down | click lands on it: false | takes focus: false
visibility: hidden | next starts 40 px down | click lands on it: false | takes focus: false
opacity: 0         | next starts 40 px down | click lands on it: true | takes focus: true

Checked against: W3C CSS Color Level 4, W3C CSS Display Level 3

Intermediate

Intermediate CSS interview questions

Advice

Intermediate6. How does flex-grow share space?

The container first gives fixed items their size, then divides the leftover space in proportion to the flex-grow values. In a 600px row with a 150px fixed item, 450px is left; flex: 1 and flex: 2 split it 150px and 300px. Related questions: justify-content aligns items along the main axis, align-items along the cross axis.

HTML and CSS
<style>
  .row { display: flex; width: 600px; }
  .fixed { width: 150px; flex: none; }
  .a { flex: 1; }
  .b { flex: 2; }
</style>
<div class="row"><div class="fixed"></div><div class="a"></div><div class="b"></div></div>
<script>
  for (const el of document.querySelectorAll('.row > div')) console.log(el.className.padEnd(5), el.getBoundingClientRect().width);
</script>
Output (Google Chrome 157.0.8103.40 headless)
fixed 150
a     150
b     300

Checked against: W3C CSS Flexible Box Layout Level 1

Intermediate7. What does the fr unit do in CSS Grid?

fr is a share of the leftover space in the grid container after fixed tracks and gaps are subtracted. Here 700px minus a 100px column and two 30px gaps leaves 540px, split 1:2 into 180px and 360px. Use Grid for two-dimensional layouts (rows and columns together) and Flexbox for one direction.

HTML and CSS
<style>
  .grid { display: grid; width: 700px; grid-template-columns: 100px 1fr 2fr; column-gap: 30px; }
</style>
<div class="grid"><div></div><div></div><div></div></div>
<script>
  console.log(getComputedStyle(document.querySelector('.grid')).gridTemplateColumns);
</script>
Output (Google Chrome 157.0.8103.40 headless)
100px 180px 360px

Checked against: W3C CSS Grid Layout Level 1

Intermediate8. What is the difference between em and rem?

rem is relative to the root element's font size; em is relative to the element's own font size, or to the parent's when em is used for font-size itself. Inside a 20px card, 1.5em gives a 30px font and its 1em padding is then 30px, while 1.5rem stays 24px with 16px padding wherever it is used. em compounds when nested; rem does not.

HTML and CSS
<style>
  html { font-size: 16px; }
  .card { font-size: 20px; }
  .em { font-size: 1.5em; padding: 1em; }
  .rem { font-size: 1.5rem; padding: 1rem; }
</style>
<div class="card"><p class="em">em</p><p class="rem">rem</p></div>
<script>
  for (const c of ['em', 'rem']) {
    const s = getComputedStyle(document.querySelector('.' + c));
    console.log(c.padEnd(3), 'font-size', s.fontSize, '| padding', s.paddingTop);
  }
</script>
Output (Google Chrome 157.0.8103.40 headless)
em  font-size 30px | padding 30px
rem font-size 24px | padding 16px

Checked against: W3C CSS Values and Units Level 4

Intermediate9. How do CSS custom properties (variables) work?

Custom properties such as --brand are ordinary properties: they cascade and inherit, so redefining one on a container changes it for everything inside. var(--name, fallback) uses the fallback when the variable is not set. Unlike Sass variables, they are live in the browser, so you can change them with a class or media query, or from JavaScript.

HTML and CSS
<style>
  :root { --brand: rgb(0, 90, 200); }
  .dark { --brand: rgb(255, 200, 0); }
  .btn { color: var(--brand); background-color: var(--accent, rgb(240, 240, 240)); }
</style>
<button class="btn" id="a">Light</button>
<div class="dark"><button class="btn" id="b">Dark</button></div>
<script>
  for (const id of ['a', 'b']) {
    const s = getComputedStyle(document.getElementById(id));
    console.log(id, s.color, '|', s.backgroundColor);
  }
</script>
Output (Google Chrome 157.0.8103.40 headless)
a rgb(0, 90, 200) | rgb(240, 240, 240)
b rgb(255, 200, 0) | rgb(240, 240, 240)

Checked against: W3C CSS Custom Properties Level 1

Intermediate10. What are cascade layers (@layer)?

Layers let you order groups of styles explicitly. Between layers, order decides before specificity does: a later layer beats an earlier one even with a weaker selector, and styles outside any layer beat all layered styles (for normal declarations). Teams use this to keep resets and third-party CSS from fighting component styles.

HTML and CSS
<style>
  @layer reset, components;
  @layer components { .btn.primary { color: blue; } }
  @layer reset { span.btn.primary { color: gray; } }
  button { color: green; }
</style>
<button class="btn primary">Save</button> <span class="btn primary">Link</span>
<script>
  for (const sel of ['button', 'span']) console.log(sel, getComputedStyle(document.querySelector(sel)).color);
</script>
Output (Google Chrome 157.0.8103.40 headless)
button rgb(0, 128, 0)
span rgb(0, 0, 255)

Checked against: W3C CSS Cascading and Inheritance Level 5

Advanced

Advanced CSS interview questions

Advice

Advanced11. How do :is() and :where() differ?

Both match any selector in their list. :is() takes the specificity of its highest-specificity argument, so :is(#main) counts like an ID; :where() always has zero specificity, which makes it ideal for defaults that are easy to override. In the example, :where(#main) p loses to a plain p rule that comes later, while :is(#main) span beats a class.

HTML and CSS
<style>
  :where(#main) p { color: red; }
  p { color: green; }
  :is(#main) span { color: red; }
  .note { color: green; }
</style>
<div id="main"><p>paragraph</p><span class="note">note</span></div>
<script>
  for (const sel of ['p', 'span']) console.log(sel, getComputedStyle(document.querySelector(sel)).color);
</script>
Output (Google Chrome 157.0.8103.40 headless)
p rgb(0, 128, 0)
span rgb(255, 0, 0)

Checked against: W3C Selectors Level 4

Advanced12. Why does z-index: 999 not bring an element to the front?

z-index compares elements only within the same stacking context. A positioned parent with a z-index creates a stacking context, and so does an element with opacity below 1 or a transform, among others; and everything inside it is stacked as one unit. Here the child's 999 only counts inside a parent at z-index 1, so a sibling at z-index 2 covers it.

HTML and CSS
<style>
  body { margin: 0; }
  .box { position: absolute; top: 0; left: 0; width: 100px; height: 100px; }
  .parent { position: relative; z-index: 1; }
  #child { z-index: 999; }
  #sibling { z-index: 2; }
</style>
<div class="parent"><div class="box" id="child"></div></div>
<div class="box" id="sibling"></div>
<script>
  console.log('element on top at (50, 50):', document.elementFromPoint(50, 50).id);
</script>
Output (Google Chrome 157.0.8103.40 headless)
element on top at (50, 50): sibling

Checked against: W3C CSS Color Level 4, W3C CSS Transforms Level 1, W3C CSS 2.2: visual formatting model

Advanced13. What is the difference between a CSS transition and a CSS animation?

A transition animates a property from its old value to its new value when that value changes, for example when a class is added; it needs a trigger and has just a start and an end. An animation runs @keyframes on its own, can have many steps, repeat and run backward. JavaScript can follow both through events such as transitionend.

HTML and CSS
<style>
  .panel { width: 100px; transition: width 120ms ease-out; }
  .panel.open { width: 300px; }
</style>
<div class="panel" id="p">menu</div>
<script>
  const p = document.getElementById('p');
  p.addEventListener('transitionend', e => console.log('transitionend:', e.propertyName, '| width now', getComputedStyle(p).width));
  requestAnimationFrame(() => requestAnimationFrame(() => p.classList.add('open')));
</script>
Output (Google Chrome 157.0.8103.40 headless)
transitionend: width | width now 300px

Checked against: W3C CSS Transitions Level 1

Advanced14. What does animation-fill-mode: forwards do?

By default an element snaps back to its normal styles when an animation ends. forwards keeps the values from the end of the last iteration (backwards applies the first keyframe during a delay, and both does both). The example moves two boxes with the same keyframes: after the end, only the forwards one is still shifted 200px.

HTML and CSS
<style>
  @keyframes slide { from { transform: translateX(0); } to { transform: translateX(200px); } }
  .a, .b { animation: slide 100ms 1; }
  .b { animation-fill-mode: forwards; }
</style>
<div class="a" id="a">a</div><div class="b" id="b">b</div>
<script>
  document.getElementById('b').addEventListener('animationend', () => setTimeout(() => {
    for (const id of ['a', 'b']) console.log(id, getComputedStyle(document.getElementById(id)).transform);
  }, 50));
</script>
Output (Google Chrome 157.0.8103.40 headless)
a none
b matrix(1, 0, 0, 1, 200, 0)

Checked against: W3C CSS Animations Level 1

Advanced15. Which events does a CSS animation fire?

animationstart when it begins, animationiteration at the end of each iteration except the last, and animationend when it finishes; each carries the animation name and the elapsed time. A three-iteration, 150ms animation therefore fires two iteration events, at 0.15s and 0.30s, and ends at 0.45s.

HTML and CSS
<style>
  @keyframes pulse { 50% { opacity: 0.4; } }
  .dot { width: 20px; height: 20px; animation: pulse 150ms 3; }
</style>
<div class="dot" id="d"></div>
<script>
  const d = document.getElementById('d');
  for (const t of ['animationstart', 'animationiteration', 'animationend'])
    d.addEventListener(t, e => console.log(e.type, e.animationName, e.elapsedTime.toFixed(2) + 's'));
</script>
Output (Google Chrome 157.0.8103.40 headless)
animationstart pulse 0.00s
animationiteration pulse 0.15s
animationiteration pulse 0.30s
animationend pulse 0.45s

Checked against: W3C CSS Animations Level 1

Advanced16. Why animate transform and opacity rather than width, top or margin?

Changing transform does not change the layout of the surrounding content, so the browser does not have to recalculate the positions of other elements on every frame; browsers can often animate transform and opacity on the compositor, which keeps the animation smooth. Animating width, top or margin moves other content, as the example shows: the neighbor of the translated box stays put, the neighbor of the box with a margin moves. Measure with the browser's performance tools rather than assuming.

HTML and CSS
<style>
  .row { display: flex; }
  .item { width: 100px; height: 50px; }
</style>
<div class="row"><div class="item" style="transform: translateX(100px)"></div><div class="item" id="n1"></div></div>
<div class="row"><div class="item" style="margin-left: 100px"></div><div class="item" id="n2"></div></div>
<script>
  console.log('neighbor of the translated box starts at', document.getElementById('n1').getBoundingClientRect().left);
  console.log('neighbor of the box with a margin starts at', document.getElementById('n2').getBoundingClientRect().left);
</script>
Output (Google Chrome 157.0.8103.40 headless)
neighbor of the translated box starts at 108
neighbor of the box with a margin starts at 208

Checked against: W3C CSS Transforms Level 1

Advanced17. How should animations respect prefers-reduced-motion?

Some users set their operating system to reduce motion, for example because animation can make them feel unwell. The prefers-reduced-motion media query exposes that setting: wrap large or decorative motion in @media (prefers-reduced-motion: no-preference), or switch it off or shorten it under reduce. Keep transitions that convey state, such as a quick fade, but remove parallax, zooms and auto-playing movement.

Checked against: W3C Media Queries Level 5

Practice

How to practice with this page

Advice
  • Predict each measurement before reading the output.
  • Open the snippet in your browser's developer tools and change one property at a time.
  • Explain the specificity of a selector out loud as three numbers.
  • Build a small responsive card with Flexbox or Grid without a framework.
Next

Related interview prep and tools

Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.

FAQ

Questions people ask

What CSS topics come up often in front-end interviews?
Specificity and the cascade, the box model and box-sizing, positioning and stacking contexts, Flexbox and Grid, responsive design with media queries and relative units, custom properties, and transitions and animations, often with a small live layout exercise.
Why were these examples run in Chrome only?
The page shows real browser output, and the build machine has Google Chrome. The answers describe the CSS specifications, which all current browsers implement for these features; for edge cases, check the layout in Firefox and Safari too.
How do I practice CSS for an interview?
Rebuild small components from real sites (a card, a navigation bar, a responsive grid) without a framework, then explain each property you used. Practice the questions here in the browser's developer tools by changing one property at a time.
Method

How this page is built

TryHired wrote these questions and answers. Every snippet on this page was loaded as a page in Google Chrome 157.0.8103.40 headless when the page was built (2026-11-15), at an 800 × 600 viewport with all network requests blocked; the output is exactly what the snippet's script printed with console.log. Facts about CSS were checked against the W3C specifications listed below. The build stops if any example fails. The question list is general practice: it does not predict what a particular employer will ask.

What this page can't tell you
  • Interviewers ask different questions for different roles and levels.
  • Answers are short explanations, not complete references.
  • Output comes from one browser, Google Chrome. The answers describe the W3C specifications, which current browsers follow for these features; check edge cases in Firefox and Safari too.
Sources
  1. W3C CSS Animations Level 1 (read 2026-11-15)
  2. W3C CSS Cascading and Inheritance Level 5 (read 2026-11-15)
  3. W3C CSS Color Level 4 (read 2026-11-15)
  4. W3C CSS Containment Level 2 (read 2026-11-15)
  5. W3C CSS Display Level 3 (read 2026-11-15)
  6. W3C Filter Effects Level 1 (read 2026-11-15)
  7. W3C CSS Flexible Box Layout Level 1 (read 2026-11-15)
  8. W3C CSS Grid Layout Level 1 (read 2026-11-15)
  9. W3C Media Queries Level 5 (read 2026-11-15)
  10. W3C CSS Positioned Layout Level 3 (read 2026-11-15)
  11. W3C Selectors Level 4 (read 2026-11-15)
  12. W3C CSS Box Sizing Level 3 (read 2026-11-15)
  13. W3C CSS Transforms Level 1 (read 2026-11-15)
  14. W3C CSS Transitions Level 1 (read 2026-11-15)
  15. W3C CSS Values and Units Level 4 (read 2026-11-15)
  16. W3C CSS Custom Properties Level 1 (read 2026-11-15)
  17. W3C CSS 2.2: box model (read 2026-11-15)
  18. W3C CSS 2.2: visual formatting model (read 2026-11-15)