Topics
v6.0.0
On this page · sections

Top 20 HTML & CSS Interview Questions — Concise Q&A

Module: JavaScript · Category: HTML & CSS

Frontend panels still open with HTML/CSS fundamentals. Use these as tight panel answers — clear definition, why it matters, one concrete example.

01. Semantic HTML — why does it matter?

Q: What is semantic HTML, and why do interviewers care?

A: Semantic tags describe meaning (header, nav, main, article, section, aside, footer) instead of only presentation (div/span). Benefits: better accessibility (screen readers get landmarks), SEO, maintainability, and default browser/CSS hooks. Prefer semantics first; style with CSS.

Tip: div is fine for layout wrappers with no meaning — don’t force a section everywhere.

02. Block vs inline vs inline-block

Q: Difference between block, inline, and inline-block?

A:

DisplayWidth/heightLine breaksMargin/padding
blockFull available width by defaultStarts on a new lineAll sides apply
inlineContent-sizedFlows in textHorizontal yes; vertical padding/margin don’t affect layout the same way
inline-blockContent-sized (width/height honored)Flows inlineAll sides apply

Example: Icons + text next to each other → inline-block or flex; page sections → block.

03. CSS box model (content-box vs border-box)

Q: Explain the CSS box model. What does box-sizing: border-box do?

A: Every element’s box has content → padding → border → margin. With content-box (default), width/height apply only to content — padding/border grow the total size. With border-box, width/height include content + padding + border (margin still outside). Most teams set:

*, *::before, *::after { box-sizing: border-box; }

04. CSS specificity

Q: How is CSS specificity calculated? Who wins when rules conflict?

A: Specificity is a 4-tuple: inline style > IDs > classes/attributes/pseudo-classes > elements/pseudo-elements. Equal specificity → later rule wins (source order). !important overrides normal rules (use sparingly). Inline styles beat IDs unless the other rule is !important.

Example: #nav .link beats .link. .link:hover beats .link.

05. Positioning: static, relative, absolute, fixed, sticky

Q: Explain CSS position values.

A:

  • static — default flow; top/left ignored
  • relative — offset from its normal position; still occupies original space; creates containing block for absolute children
  • absolute — removed from flow; positioned relative to nearest positioned ancestor (relative/absolute/fixed/sticky)
  • fixed — relative to the viewport (or transform-containing ancestor)
  • sticky — acts like relative until a scroll threshold, then sticks within its containing block

06. display: none vs visibility: hidden vs opacity: 0

Q: What’s the difference?

A:

Layout spaceEvents / a11yAnimatable
display: noneRemovedNot in accessibility treeNo (discrete)
visibility: hiddenKeptHidden but may still affect some a11y casesLimited
opacity: 0KeptStill focusable/clickable unless also pointer-events: noneYes

Use display: none to unmount visually from layout; opacity/visibility for transitions and placeholders.

07. Flexbox vs CSS Grid

Q: When do you use Flexbox vs Grid?

A: Flexbox = one-dimensional alignment (row or column) — toolbars, navs, card footers, centering. Grid = two-dimensional layout — page shells, dashboards, overlapping regions. You often combine them: Grid for page structure, Flex inside components.

One-liner: Flex for content direction; Grid for overall structure.

08. Centering an element (classic)

Q: How do you center a div horizontally and vertically?

A: Modern default:

.parent { display: flex; justify-content: center; align-items: center; min-height: 100vh; }

Grid alternative: place-items: center on the parent. Older absolute trick: top/left: 50% + transform: translate(-50%, -50%). Prefer flex/grid in interviews unless they ask for the classic absolute approach.

09. em vs rem vs px vs %

Q: Difference between these units?

A:

  • px — absolute device pixels (roughly); predictable, less scalable for a11y fonts
  • em — relative to parent’s font-size (compounds)
  • rem — relative to root (html) font-size (preferred for spacing/typography scale)
  • % — relative to the containing block (width/height rules differ)

Panel answer: Use rem for type and spacing scales; %/fr for fluid layout; px for borders/hairlines.

10. Pseudo-classes vs pseudo-elements

Q: Difference between :hover and ::before?

A: Pseudo-classes (:hover, :focus, :nth-child) select elements in a state or structural position. Pseudo-elements (::before, ::after, ::placeholder) create/style a fragment of the element. Use double colon for pseudo-elements (::before); single colon still works for legacy.

11. Cascade, inheritance, and unset / revert

Q: How does the cascade decide the final style?

A: Order of winning: origin + importance → specificity → source order. Some properties inherit (color, font-family); others don’t (margin, display). Keywords: inherit (from parent), initial (spec default), unset (inherit if inheritable else initial), revert (roll back to user-agent/user styles).

12. Stacking context & z-index

Q: Why doesn’t my z-index work?

A: z-index only applies to positioned elements (or flex/grid items). A new stacking context is created by things like opacity < 1, transform, filter, isolation, position + z-index ≠ auto. Children cannot paint above siblings of a parent with a lower stacking context — fix by restructuring or adjusting the parent’s context, not only the child’s z-index.

13. Responsive design & mobile-first

Q: How do you approach responsive CSS?

A: Prefer mobile-first: base styles for small screens, then min-width media queries for larger breakpoints. Use fluid layouts (flex/grid, %/fr, clamp() for type), relative units, and avoid fixed widths. Test touch targets, overflow, and images (max-width: 100%, srcset).

.card { padding: 1rem; } @media (min-width: 768px) { .card { padding: 1.5rem; } }

14. Viewport units (vh, vw, dvh)

Q: Issues with 100vh on mobile?

A: Classic 100vh often includes the area under the browser chrome, so content can sit under the URL bar. Prefer 100dvh (dynamic viewport) or 100svh/100lvh where supported, with a 100vh fallback. Also watch nested scroll containers and keyboard open states on mobile.

15. Reflow vs repaint

Q: What’s the difference, and how do you avoid jank?

A: Reflow (layout) recalculates geometry — triggered by changing size/position/DOM structure. Repaint updates pixels without layout (e.g. color, background). Animating transform/opacity usually stays on the compositor (cheaper). Avoid reading layout (offsetHeight) then writing styles in a loop (layout thrashing). Batch DOM reads, then writes.

16. Critical rendering path (high level)

Q: What happens from HTML/CSS to pixels?

A: Browser builds DOM from HTML and CSSOM from CSS → render tree → layout → paint → composite. Blocking CSS/JS delays first paint. Optimize: minimize critical CSS, defer non-critical JS, avoid huge sync scripts in <head>, use async/defer appropriately, compress assets.

17. Accessibility basics (HTML)

Q: Name HTML a11y practices you always follow?

A:

  • Meaningful landmarks and heading hierarchy (h1–h6)
  • alt text for informative images; empty alt="" for decorative
  • Labels bound to inputs (<label for> / wrapping)
  • Keyboard focus order; visible :focus-visible
  • Don’t remove semantics with clickable divs — use <button>/<a>
  • Sufficient contrast; don’t rely on color alone

ARIA supplements semantics; it doesn’t replace them.

18. Forms: validation & HTML5 inputs

Q: How do you validate forms accessibly?

A: Prefer native constraints (required, type="email", pattern, min/max) plus clear error text associated via aria-describedby / aria-invalid. Don’t rely only on color. Client validation is UX; server validation is required. Use correct input types for mobile keyboards (tel, email, url).

19. CSS custom properties (variables)

Q: How do CSS variables differ from Sass variables?

A: CSS custom properties (--color-bg) are live in the browser, inherit, cascade, and can change at runtime (themes, dark mode). Sass/Less variables compile away at build time. Scope tokens on :root or a theme class; override per component when needed.

:root { --space: 1rem; } .card { padding: var(--space); }

20. BEM / naming & CSS architecture

Q: How do you keep CSS maintainable in a large app?

A: Common answer: component-scoped styles (CSS Modules, CSS-in-JS, or BEM naming) to avoid global collisions. BEM: .block__element--modifier. Modern stacks lean on Modules/Tailwind/utility layers, but the interview point is the same — limit global selectors, prefer composition, and keep specificity low so overrides stay predictable.

Quick revision checklist

ThemeQuestions
HTML semantics & a11y01, 17, 18
Layout & box model02, 03, 07, 08
Cascade & specificity04, 11, 12
Positioning & display05, 06
Units & responsive09, 13, 14
Performance15, 16
Architecture19, 20

Last updated on