Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

Top HTML and CSS Interview Questions and Answers

The HTML and CSS interview questions asked most often, covering semantics, script loading, the box model, specificity, positioning, Flexbox, Grid and responsive design.

jiocoder TeamUpdated Oct 7, 20263 min readCSS
Top HTML and CSS Interview Questions and Answers

Frontend interviews nearly always include HTML and CSS questions, and they're easy to underestimate. These are the topics interviewers ask about most, from semantics and accessibility to layout and specificity.

1. What is semantic HTML and why does it matter?#

Semantic HTML uses elements that describe their meaning, like <header>, <nav>, <main>, <article> and <button>, instead of generic <div>s. It helps screen readers, improves SEO, and makes code easier to read.

Tip

Mention that a <button> works with the keyboard by default, while a clickable <div> needs extra code to be accessible. Interviewers love this example.

2. What is the difference between async and defer on script tags?#

  • Normal script: HTML parsing stops while the script downloads and runs.
  • async: downloads in parallel and runs as soon as it's ready, in any order. Good for independent scripts like analytics.
  • defer: downloads in parallel and runs after HTML parsing finishes, in order. Good for most app scripts.

3. Explain the CSS box model#

Every element is a box made of content, padding, border and margin. By default (content-box), width sets only the content width. With border-box, width includes padding and border, which makes sizing far easier.

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

4. How does specificity work?#

When rules conflict, the more specific selector wins. Specificity is compared in this order: inline styles, then IDs, then classes, attributes and pseudo-classes, then elements and pseudo-elements. If specificity is equal, the rule that comes later wins. !important overrides normal rules and should be a last resort.

CSS
#nav a       { color: red; }   /* 1 ID, 1 element */
.menu .link  { color: blue; }  /* 2 classes */
/* The link is red: one ID beats any number of classes */

5. What are the position values?#

ValueBehaviour
staticDefault. Follows normal flow.
relativeStays in flow, can be offset. Becomes the reference for absolute children.
absoluteRemoved from flow, positioned relative to the nearest positioned ancestor.
fixedPositioned relative to the viewport. Stays put when scrolling.
stickyActs relative until a scroll threshold, then sticks.

6. Flexbox or Grid: when do you use each?#

Flexbox is one-dimensional, arranging items in a row or a column. It's great for navbars, button groups and aligning items. Grid is two-dimensional, controlling rows and columns together. It's great for page layouts and card grids. They work well together.

7. How do you center a div?#

CSS
/* Flexbox */
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Grid */
.parent {
  display: grid;
  place-items: center;
}

8. What is the difference between em, rem, %, vw and px?#

  • px: a fixed size.
  • em: relative to the element's own font size (or its parent's, for font-size itself).
  • rem: relative to the root (html) font size. Best for consistent, scalable typography.
  • %: relative to the parent element.
  • vw / vh: 1% of the viewport width or height.

9. How do you make a website responsive?#

Add the viewport meta tag, use flexible layouts (Flexbox and Grid), relative units, and responsive images, then adjust layouts with media queries. A mobile-first approach writes base styles for small screens and adds min-width queries for larger ones.

CSS
.cards { display: grid; gap: 1rem; }

@media (min-width: 768px) {
  .cards { grid-template-columns: repeat(3, 1fr); }
}

Key takeaways#

  • Use semantic HTML and explain how it helps accessibility.
  • Know the box model, specificity and positioning thoroughly.
  • Be able to explain Flexbox vs Grid and center an element both ways.