CareerKit

HTML and CSS Interview Questions and Answers

HTML and CSS questions asked in frontend interviews: semantic HTML and accessibility, the box model and positioning, Flexbox and Grid, responsive design and specificity.

10 free answers from our HTML & CSS Interview Questions ebook, which has all 90 questions.

Semantic HTML and accessibility

What is semantic HTML and why does it matter?

Semantic HTML means choosing elements by their meaning, not their default look: <nav> for navigation, <button> for actions, <h1> to <h6> for headings, <article> for a self-contained piece of content.

It matters because:

  • Accessibility: screen readers announce landmarks, headings and controls, so users can navigate the page.
  • SEO: search engines understand the structure and importance of your content.
  • Maintainability: other developers can read the structure at a glance.
  • Free behaviour: a real <button> is focusable and works with Enter and Space without any JavaScript.
<!-- Non-semantic -->
<div class="header"><div class="nav">...</div></div>
<div class="btn" onclick="buy()">Buy now</div>

<!-- Semantic -->
<header><nav aria-label="Main">...</nav></header>
<button type="button" onclick="buy()">Buy now</button>
Say it in the interview

Semantic HTML uses elements for their meaning, like nav, main, article and button, instead of generic divs. It makes pages accessible to screen readers and keyboard users, helps search engines understand the content, and gives built-in behaviour such as keyboard support for buttons.

What is the difference between div, section and article?

  • <div> has no meaning. Use it purely for styling or layout.
  • <section> is a thematic group of content, usually with its own heading, like "Pricing" or "FAQ".
  • <article> is self-contained content that would make sense on its own or if shared elsewhere: a blog post, a product card, a comment.

A useful test: if you'd give it a heading, it's probably a <section>; if it could be published on its own, it's an <article>; if it's just a wrapper for CSS, it's a <div>.

<section aria-labelledby="ebooks-title">
  <h2 id="ebooks-title">All ebooks</h2>
  <article class="card">
    <h3>React Interview Questions</h3>
    <p>₹299</p>
  </article>
</section>
Say it in the interview

div has no meaning and is only for layout. section groups related content under a heading, like a page section. article is self-contained content that could stand alone, like a blog post or product card. I choose by meaning and use div only when nothing else fits.

Box model and positioning

What is the CSS box model?

Every element is drawn as a rectangular box made of four layers, from the inside out:

  • content: the text, image or child elements
  • padding: space inside the box, between content and border (it shows the background)
  • border: the line around the padding
  • margin: space outside the border, separating it from other elements (always transparent)
.card {
  width: 300px;
  padding: 16px;
  border: 2px solid #dce1ea;
  margin: 24px;
}

With the default box-sizing: content-box, width sets only the content width, so this card actually takes 300 + 16×2 + 2×2 = 336px of width, plus 24px of margin on each side. Browser DevTools show this as a coloured box diagram, which is the fastest way to debug sizing.

Say it in the interview

Each element is a box of content, then padding, then border, then margin. Padding is inside the border and shows the background, while margin is outside and transparent. By default, width only covers the content, so padding and border are added on top.

What is the difference between content-box and border-box?

box-sizing decides what width and height include:

  • content-box (default): width is the content only; padding and border are added on top.
  • border-box: width includes padding and border; the content shrinks to fit.
*,
*::before,
*::after {
  box-sizing: border-box;
}

.sidebar {
  width: 25%;
  padding: 20px;
}

With border-box, a 25% sidebar with padding is still exactly 25% wide, which makes layouts far more predictable. That's why almost every modern project starts with the global rule above.

Say it in the interview

With content-box, the width covers only the content and padding and border are added on top. With border-box, the width includes padding and border. I apply border-box globally so declared sizes are the real sizes, especially for percentage layouts.

Flexbox and Grid

What is Flexbox, and what are the main and cross axes?

Flexbox lays out children in one direction, a row or a column, and controls how they grow, shrink, align and space out.

  • The main axis runs in the flex-direction: horizontal for row (the default), vertical for column.
  • The cross axis is perpendicular to it.
.toolbar {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

Most confusion comes from forgetting that the axes swap when you set flex-direction: column: then justify-content controls vertical placement and align-items controls horizontal.

Say it in the interview

Flexbox arranges children along one axis. The main axis follows flex-direction, row by default, and the cross axis is perpendicular. justify-content works on the main axis and align-items on the cross axis, and they swap meaning when the direction is column.

What is the difference between justify-content and align-items?

  • justify-content distributes items along the main axis: flex-start, center, flex-end, space-between, space-around, space-evenly.
  • align-items aligns items on the cross axis: stretch (the default, so items fill the container's height in a row), flex-start, center, flex-end, baseline.
  • align-content spaces out multiple rows of wrapped items on the cross axis (it does nothing for a single line).
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

align-items: baseline lines up text of different sizes along their text baseline, perfect for a big price next to a small crossed-out MRP.

Say it in the interview

justify-content distributes items along the main axis, like space-between for a header. align-items positions them on the cross axis and defaults to stretch. align-content only matters when items wrap onto several lines. Baseline alignment is great for mixing text sizes.

Responsive design and media queries

What is responsive design?

Responsive design means one website adapts its layout to any screen size, from a 320px phone to a wide desktop, instead of building separate mobile and desktop sites.

It's built on three ideas:

  • Fluid layouts: widths in %, fr and max-width instead of fixed pixels
  • Flexible media: images and videos that scale (max-width: 100%)
  • Media queries (and container queries) to change the layout at certain widths
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

img, video {
  max-width: 100%;
  height: auto;
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
}

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

It also needs the viewport meta tag, so phones don't zoom out to a fake desktop width.

Say it in the interview

Responsive design means a single site adapts to every screen size using fluid layouts, flexible images and media queries, plus the viewport meta tag. In India most traffic is mobile, so I design for small screens first and enhance for larger ones.

What is mobile-first, and why is it preferred?

Mobile-first CSS writes the base styles for small screens, then adds min-width media queries to enhance the layout on larger screens. Desktop-first does the opposite with max-width queries.

/* Mobile first: base styles are for phones */
.nav-links { display: none; }
.cards { grid-template-columns: 1fr; }

@media (min-width: 768px) {
  .nav-links { display: flex; }
  .cards { grid-template-columns: repeat(2, 1fr); }
}

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

Why it's preferred:

  • Phones get the simplest CSS without having to undo desktop styles.
  • It forces you to prioritise the essential content.
  • It matches reality: most users, especially in India, are on mobile.
Say it in the interview

Mobile-first means writing default styles for small screens and adding min-width media queries to enhance for bigger screens. It keeps mobile CSS simple, forces prioritisation of content, and suits an audience that's mostly on phones.

CSS specificity and common bugs

What is specificity, and how is it calculated?

When several rules target the same element and property, specificity decides which wins. It's counted as three numbers, compared left to right:

  • (a) ID selectors: #header
  • (b) classes, attribute selectors and pseudo-classes: .card, [type="email"], :hover
  • (c) element selectors and pseudo-elements: div, ::before
p                      /* (0, 0, 1) */
.price                 /* (0, 1, 0) */
.card .price           /* (0, 2, 0) */
#ebooks .card .price   /* (1, 2, 0) */
button:hover           /* (0, 1, 1) */

Any number of classes never beats a single ID: (0, 10, 0) loses to (1, 0, 0). Inline style attributes beat all selectors, and !important beats normal declarations. The universal selector * and :where() add zero.

Say it in the interview

Specificity is counted as IDs, then classes, attributes and pseudo-classes, then elements and pseudo-elements, compared from left to right, so one ID beats any number of classes. Inline styles beat selectors and important beats everything normal. If specificity ties, the later rule wins.

In what order does the cascade decide which style wins?

The cascade resolves conflicts in this order:

  1. Origin and importance: browser defaults, then your styles; !important declarations reverse the priority.
  2. Cascade layers (@layer): later layers win over earlier ones; unlayered styles win over layered ones.
  3. Specificity: the more specific selector wins.
  4. Order of appearance: if everything else is equal, the rule that comes last wins.
@layer reset, components, utilities;

@layer components {
  .button { background: #2f4bff; }
}

@layer utilities {
  .bg-dark { background: #18213a; }
}

Here .bg-dark wins over .button even though their specificity is equal, because its layer comes later. Layers are a modern, clean way to organise CSS without specificity wars.

Say it in the interview

The cascade looks at origin and importance first, then cascade layers, then specificity, and finally source order, where the last rule wins. Cascade layers let me control priority between groups of styles, like resets, components and utilities, without increasing specificity.

All 90 questions in the ebook

Linked questions are answered free on this page; the rest are in the ebook.

Semantic HTML and accessibility

  1. What is semantic HTML and why does it matter?
  2. What is the difference between div, section and article?
  3. When do you use header, nav, main, aside and footer?
  4. What does the doctype do?
  5. What do the meta charset and viewport tags do?
  6. What is the difference between block, inline and inline-block elements?
  7. What are data-* attributes?
  8. How do you write good alt text for images?
  9. What is ARIA and when should you use it?
  10. How do you make a form accessible?
  11. What is the difference between a button and a link?
  12. Why does heading structure matter?
  13. What is tabindex, and how do you manage focus?
  14. What is the difference between script, async and defer?
  15. What is the difference between localStorage, sessionStorage and cookies?
  16. What are HTML5 input types and built-in validation?
  17. What is the difference between id and class?
  18. How do you improve SEO with HTML?

Box model and positioning

  1. What is the CSS box model?
  2. What is the difference between content-box and border-box?
  3. What is margin collapsing?
  4. What is the difference between padding and margin?
  5. What is the difference between display: none, visibility: hidden and opacity: 0?
  6. What are the different position values?
  7. How does an absolutely positioned element decide where to go?
  8. What is z-index, and what is a stacking context?
  9. How do you centre an element horizontally and vertically?
  10. How do you handle content that overflows its box?
  11. What are the common CSS units?
  12. What is the difference between em and rem?
  13. What are floats, and how do you clear them?
  14. Why don't width, height and vertical margins work on a span?
  15. What is the difference between width: auto and width: 100%?
  16. What is the difference between pseudo-classes and pseudo-elements?
  17. What is the difference between width, min-width and max-width?
  18. How does line-height work, and why use a unitless value?

Flexbox and Grid

  1. What is Flexbox, and what are the main and cross axes?
  2. What is the difference between justify-content and align-items?
  3. What do flex-grow, flex-shrink and flex-basis do?
  4. How do flex-wrap and gap work?
  5. How do you push one flex item to the far end?
  6. Why does a flex item overflow instead of shrinking?
  7. What do align-self and order do?
  8. What is CSS Grid, and when do you use it instead of Flexbox?
  9. What do fr, repeat and minmax do in grid-template-columns?
  10. What is the difference between auto-fill and auto-fit?
  11. How do grid-template-areas work?
  12. How do you make a grid item span several columns or rows?
  13. Build a responsive card grid without media queries.
  14. Build the classic header, sidebar, content and footer layout.
  15. How do you make columns equal height?
  16. How does place-items work, and how else can Grid centre things?
  17. What is the difference between the explicit and implicit grid?
  18. How do you make a footer stay at the bottom on short pages?

Responsive design and media queries

  1. What is responsive design?
  2. What is mobile-first, and why is it preferred?
  3. How do media queries work, and which breakpoints do you use?
  4. How do you serve responsive images?
  5. How do you make font sizes scale smoothly between screen sizes?
  6. Why doesn't 100vh fit the screen on mobile?
  7. Which units help a layout stay responsive?
  8. What are container queries?
  9. How do you make a data table responsive?
  10. Is it fine to hide elements on mobile with display: none?
  11. How do you build a responsive navigation menu?
  12. How big should touch targets be?
  13. What are prefers-reduced-motion and prefers-color-scheme?
  14. What are CSS custom properties (variables)?
  15. What does the aspect-ratio property do?
  16. What does object-fit do?
  17. How do you test responsive design?
  18. What is layout shift, and how do you prevent it?

CSS specificity and common bugs

  1. What is specificity, and how is it calculated?
  2. In what order does the cascade decide which style wins?
  3. Why should you avoid !important?
  4. Which properties are inherited, and what do inherit, initial and unset do?
  5. What is the difference between the descendant, child and sibling combinators?
  6. What are attribute selectors?
  7. What is the difference between :nth-child and :nth-of-type?
  8. What do :is(), :where() and :has() do?
  9. Why isn't my z-index working?
  10. Why is there a small gap below my image?
  11. Why is there extra space between inline-block elements?
  12. Why doesn't height: 50% work?
  13. Why does my page scroll sideways on mobile?
  14. Why isn't my CSS transition working?
  15. What is the difference between a CSS reset and normalize.css?
  16. What is BEM?
  17. Why animate transform instead of top and left?
  18. How do you debug CSS?