Autocoder

HTML and CSS Interview Questions and Answers

Last updated 2026-11-19

HTML and CSS questions look easy until the interviewer asks why z-index does not work, why a margin collapsed or how to centre something without guessing. This page covers the questions that separate people who have read about CSS from people who have debugged it: stacking contexts, the cascade, layout with flexbox and grid, responsive images and the modern features now supported in all major browsers.

It goes deeper on layout and the cascade than the general frontend developer interview questions page, and leaves JavaScript for the JavaScript guide. Treat each code snippet as something to try in a browser, not just read.

TopicsSemantics, cascade, layout, positioning, responsive design, modern CSS
Often paired withA layout build task and an accessibility check
Questions here18

Layout tasks that come up in practice

Typical tasks are a holy-grail page layout, a card grid that reflows without media queries, a sticky header with a scrolling body, and a modal centred over a backdrop. Interviewers watch whether you reach for grid and flexbox, use logical sizing with rem and gap, and test narrower widths before declaring the layout done.

Modern CSS you can mention

HTML and CSS Interview Questions and Answers: questions with worked answers

  1. 1

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

    easy

    display none removes the element from layout and the accessibility tree, so it takes no space. visibility hidden hides it but keeps its space and is also skipped by screen readers. opacity 0 makes it fully transparent but it still takes space, still receives clicks and is still announced. For animation, opacity and visibility can transition, while display cannot without newer features.

  2. 2

    What is the difference between inline, block and inline-block?

    easy

    Block elements start on a new line and fill the available width, and respect width, height and vertical margins. Inline elements flow within text, ignore width and height and ignore vertical margin. Inline-block flows like inline but accepts width, height and margins like block. Flex and grid change how children behave, making most of this distinction matter only inside normal flow.

  3. 3

    When do you use em, rem, px, % and vw?

    easy

    rem is relative to the root font size and is the best choice for font sizes and spacing, since it respects user preferences. em is relative to the element's own font size, which compounds in nested elements and suits components that scale with their text. px is for hairlines like borders. % is relative to the parent, and vw and dvh are relative to the viewport. Use clamp() for fluid type.

    css
    h1 { font-size: clamp(1.75rem, 1.2rem + 2vw, 3rem); }
  4. 4

    What is a stacking context and why does z-index sometimes fail?

    medium

    A stacking context is an isolated layer group. z-index only compares elements within the same context, so a child with z-index 9999 cannot rise above an element outside its parent context. A new context is created by the root, by positioned elements with a z-index value, and by opacity below 1, transform, filter, isolation: isolate and others. The fix is usually to find the ancestor creating the context, or to move the element in the DOM.

  5. 5

    Explain margin collapsing.

    medium

    Adjacent vertical margins of block elements combine into one equal to the larger, and a parent's margin can collapse with its first or last child when nothing separates them. Padding, a border, an overflow other than visible, a flex or grid container or display flow-root stops it. Horizontal margins and margins inside flex and grid never collapse.

    css
    .card { display: flow-root; } /* stops child margins collapsing through */
  6. 6

    How does CSS specificity and the cascade decide which rule wins?

    medium

    The cascade order is origin and importance, then cascade layer, then specificity, then source order. Specificity counts inline styles, IDs, classes and attributes and pseudo-classes, then elements and pseudo-elements. Layers declared later win over earlier ones regardless of specificity for normal rules. Keep selectors flat, avoid IDs for styling and avoid !important except for utilities.

  7. 7

    How do you centre an element horizontally and vertically?

    medium

    With grid, display grid with place-items center on the parent is the shortest. With flexbox, use justify-content and align-items center. For an absolutely positioned element, inset 0 with margin auto and a fixed size works, or top and left 50% with translate(-50%, -50%). For text in a line, text-align centre. Pick the method that suits the context.

    css
    .parent { display: grid; place-items: center; min-height: 100dvh; }
  8. 8

    Flexbox: what do flex-grow, flex-shrink and flex-basis do?

    medium

    flex-basis is the starting size before free space is distributed. flex-grow shares leftover space in proportion, and flex-shrink shrinks items in proportion when there is not enough space. The shorthand flex: 1 means 1 1 0%, so items share space equally ignoring content. min-width: auto on flex items can stop shrinking, which is why long text overflows until you set min-width: 0.

  9. 9

    How does CSS grid differ, and what do auto-fit and minmax do?

    medium

    Grid defines rows and columns, and items can span tracks and be placed explicitly. repeat(auto-fit, minmax(16rem, 1fr)) creates as many columns as fit, each at least 16rem and sharing leftover space, giving a responsive gallery with no media queries. auto-fill keeps empty tracks, while auto-fit collapses them so items stretch.

    css
    .gallery { display: grid; gap: 1rem;
      grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr)); }
  10. 10

    What does position: sticky do and what stops it working?

    medium

    A sticky element behaves as relative until it crosses a threshold such as top: 0 within its scroll container, then sticks, but only within its parent box. It fails when an ancestor has overflow hidden, auto or scroll, when no threshold is set, or when the parent is no taller than the element.

  11. 11

    How do you build a responsive layout?

    medium

    Use the viewport meta tag, design mobile first and add min-width media queries at points where the content breaks, not at device sizes. Prefer fluid units, flexible grids and clamp for type. Use container queries for components, responsive images and touch-friendly sizes of at least 44 CSS pixels, and test with real devices and zoom.

    html
    <meta name="viewport" content="width=device-width, initial-scale=1">
  12. 12

    How do responsive images work with srcset and picture?

    medium

    srcset with width descriptors and a sizes attribute lets the browser pick the best file for the layout width and screen density. The picture element with source elements lets you switch format, such as AVIF or WebP with a JPEG fallback, or crop for art direction. Always set width and height or aspect-ratio to prevent layout shift, and use loading="lazy" below the fold.

    html
    <img src="hero-800.jpg"
         srcset="hero-480.jpg 480w, hero-800.jpg 800w, hero-1600.jpg 1600w"
         sizes="(min-width: 60rem) 50vw, 100vw"
         width="800" height="450" alt="Team at work" loading="lazy">
  13. 13

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

    medium

    Custom properties like --brand: #0a7 are resolved at runtime, inherit through the DOM, can be changed per element or with JavaScript, and respond to media queries and themes. Sass variables are replaced at build time and cannot change in the browser. Read them with var(--brand, fallback). Theming dark mode is a classic use.

    css
    :root { --bg: #fff; --fg: #111; }
    @media (prefers-color-scheme: dark) { :root { --bg: #111; --fg: #eee; } }
    body { background: var(--bg); color: var(--fg); }
  14. 14

    What are container queries and why are they useful?

    hard

    A container query styles an element based on the size of its container, not the viewport. Mark the container with container-type: inline-size, then use @container. It makes components reusable in a sidebar and in a main column without knowing where they are placed, which media queries cannot do. They are supported in all current major browsers.

    css
    .card-wrap { container-type: inline-size; }
    @container (min-width: 30rem) {
      .card { display: grid; grid-template-columns: 8rem 1fr; }
    }
  15. 15

    What does the :has() selector allow?

    hard

    :has() selects an element based on its descendants or following siblings, which CSS could not do before, for example form:has(:invalid) to style a form with an invalid field, or a card that has an image. It is supported in all major browsers. Because it can be expensive, avoid very broad uses like :has(*) on large pages.

    css
    .card:has(img) { grid-template-columns: 10rem 1fr; }
    label:has(+ input:required)::after { content: " *"; }
  16. 16

    How do you make HTML accessible beyond adding alt text?

    hard

    Use semantic landmarks and one h1 with a logical heading order, associate every input with a label, make everything operable by keyboard with visible focus, and keep colour contrast at least 4.5:1 for body text. Use ARIA only to fill gaps, such as aria-expanded on a toggle button. Respect prefers-reduced-motion and test with a screen reader. Add lang to the html element.

  17. 17

    How do script async and defer differ, and where does render blocking come from?

    hard

    A plain script in the head blocks HTML parsing while it downloads and runs. async downloads in parallel and runs as soon as ready, in any order, so it suits independent scripts like analytics. defer downloads in parallel and runs after parsing, in document order, so it suits application code. Stylesheets block rendering, so keep critical CSS small and load the rest without blocking. type="module" scripts are deferred by default.

    html
    <link rel="stylesheet" href="app.css">
    <script src="app.js" defer></script>
    <script src="analytics.js" async></script>
  18. 18

    What is the CSS containing block for percentage and absolute positioning?

    hard

    For position absolute, the containing block is the nearest ancestor that is positioned, meaning anything other than static, or that has a transform, filter or similar property. Percentage widths refer to the containing block width, while percentage top and bottom heights refer to its height, which must be definite. Fixed positioning is relative to the viewport unless an ancestor has a transform, which then becomes the reference.

Frequently asked questions

What HTML and CSS topics matter most in interviews?

The box model, specificity, flexbox and grid, positioning and stacking, responsive design, semantic HTML and accessibility.

Should I know CSS preprocessors like Sass?

Know what they do, but native nesting and custom properties now cover many of their uses. Tailwind and CSS Modules are more commonly asked about.

How do I practise layout questions?

Rebuild a few real pages from screenshots using only HTML and CSS, resize the window as you go and check keyboard navigation.

Are CSS animations asked?

Sometimes: transitions versus keyframe animations, which properties are cheap to animate such as transform and opacity, and respecting reduced motion.

Related guides

More in Role-wise Interview Questions by Job and Tech Stack

See all in this section