100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
HTML & CSS

Common HTML & CSS Interview Questions

A practical rundown of frequently asked HTML and CSS interview questions with clear, technically accurate answers.

Interview PrepIntermediate14 min readJul 8, 2026
Analogies

Overview

HTML and CSS interviews rarely ask you to memorize trivia; they test whether you understand how the browser actually renders and lays out a page. Interviewers repeatedly probe the same handful of concepts — the box model, specificity, layout systems, and semantic markup — because these come up daily in real front-end work. This topic collects the questions you are most likely to be asked, along with concise, accurate answers you can adapt in your own words.

🏏

Cricket analogy: Just as coaches don't quiz players on trivia but on reading the field and adjusting the line, interviewers don't ask HTML/CSS trivia but probe whether you understand how the browser actually lays out and renders a page.

Frequently Asked Questions

Q: What is the CSS box model, and what does box-sizing: border-box change about it?

The box model describes how every element's rendered size is calculated from four layers: content, padding, border, and margin. By default (content-box), width and height apply only to the content area, so padding and border are added on top, making the element visually larger than the declared width. With box-sizing: border-box, the declared width and height include padding and border, so the content area shrinks to accommodate them instead of growing the box — this makes sizing far more predictable, which is why most reset stylesheets set border-box globally.

🏏

Cricket analogy: The box model is like a batsman's protective gear: the content is the player, padding is the inner padding of the pads, the border is the outer shell, and margin is the space kept from the stumps; box-sizing: border-box keeps the total stance width fixed.

Q: How does CSS specificity work, and how is it calculated?

Specificity determines which rule wins when multiple selectors target the same element. It is calculated as a tuple of (inline styles, ID selectors, class/attribute/pseudo-class selectors, element/pseudo-element selectors). Inline styles beat any selector; an ID (#id) beats any number of classes; a class (.class), attribute ([type]), or pseudo-class (:hover) beats an element selector (div); and !important overrides normal specificity entirely (and should be avoided). When specificity ties, the rule declared later in the source wins.

🏏

Cricket analogy: Specificity is like team selection hierarchy: an ID selector is the captain's decision that overrides the coach's suggestion (class), which overrides a generic team policy (element), and !important is like a board directive that trumps everyone, best avoided.

Q: What is the difference between Flexbox and CSS Grid, and when would you use each?

Flexbox is a one-dimensional layout model — it lays items out along a single row or column and excels at distributing space, aligning items, and handling content that can wrap or reflow, such as navbars or button groups. Grid is a two-dimensional layout model that lets you define rows and columns simultaneously, making it better for full page layouts, card grids, or any design where items need to align on both axes at once. In practice, many UIs use Grid for the overall page skeleton and Flexbox for aligning content within individual components.

🏏

Cricket analogy: Flexbox arranging items in one row is like setting a batting order along a single line-up card, while Grid is like laying out the entire fielding positions on the ground in both rows and columns simultaneously.

Q: Why use semantic HTML instead of generic divs and spans?

Semantic elements (header, nav, main, article, section, aside, footer, button) describe the meaning of content, not just its appearance. This improves accessibility because screen readers use these landmarks to let users navigate a page quickly; it improves SEO because search engines weigh semantic structure when indexing content; and it improves maintainability because developers can understand a document's purpose at a glance instead of parsing a wall of unlabeled divs.

🏏

Cricket analogy: Semantic elements are like an official scorecard using labeled fields for batsman, bowler, and overs instead of a blank notebook, so anyone reading it, including a commentator relying on structure, instantly understands what each part means.

Q: What is the difference between block, inline, and inline-block elements?

Block elements (div, p, section) start on a new line, stretch to fill the available width by default, and accept width, height, and vertical margin/padding. Inline elements (span, a, strong) flow within a line of text, do not start a new line, and ignore width/height and vertical margin. Inline-block elements flow inline like text but, like block elements, accept width, height, and vertical margin/padding — useful for things like navigation links that need padding without breaking onto their own line.

🏏

Cricket analogy: Block elements are like full overs that must complete before the next begins, inline elements are like individual deliveries that flow within an over without breaking it, and inline-block is like a delivery that can be padded with extra review time yet still sits within the over.

Q: What are the possible values of the CSS position property, and how do they differ?

static is the default; the element follows normal document flow and top/left/etc. have no effect. relative positions the element relative to its own normal position, without removing it from flow. absolute removes the element from flow and positions it relative to its nearest positioned ancestor (any ancestor with a position other than static), or the viewport if none exists. fixed positions relative to the viewport and stays put during scrolling. sticky behaves like relative until a scroll threshold is crossed, then behaves like fixed within its containing block.

🏏

Cricket analogy: static is like a fielder in their default position, relative is like a fielder shifted slightly for a batsman without leaving the field, absolute is like a substitute fielder placed anywhere relative to the boundary rope (nearest positioned ancestor), fixed is like the umpire staying put regardless of play, and sticky is like a fielder who holds position until the ball crosses a line, then chases it.

Q: What is the difference between em, rem, %, px, and viewport units?

px is an absolute unit that does not scale with user font-size preferences. em is relative to the font-size of the element itself (or its parent for non-font properties), which means em values can compound unpredictably when nested. rem is relative to the root (html) element's font-size, giving consistent scaling regardless of nesting. Percentages are relative to a parent property (often width or font-size). Viewport units (vw, vh) are relative to the viewport's width or height, useful for full-screen sections or fluid typography.

🏏

Cricket analogy: px is like a fixed boundary distance marked in meters regardless of ground size, em is like a batsman's stride length relative to their own height that compounds oddly when copying a teammate's stance, rem is like a distance measured against the pitch length (root) for consistency, and vw/vh are like sizing a boundary rope relative to the whole ground's dimensions.

Q: What is the CSS cascade, and how does the browser decide which style wins?

The cascade is the algorithm the browser uses to resolve conflicting rules. It considers, in order: origin and importance (user !important, author !important, author normal, user-agent default), then specificity, then source order (later rules win ties). Understanding the cascade explains why a highly specific rule declared earlier can still lose to a less specific !important rule, and why simply reordering CSS can 'fix' a style that specificity alone can't.

🏏

Cricket analogy: The cascade is like match rules precedence: ICC playing conditions (origin/importance) are checked first, then which team's specific ground rule applies (specificity), then whichever instruction was given most recently by the umpire (source order) settles any tie.

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

display: none removes the element from the layout entirely — it takes up no space and is not accessible to screen readers or keyboard focus. visibility: hidden keeps the element's space in the layout but makes it invisible and unfocusable, though it can be restored on children with visibility: visible. opacity: 0 makes the element fully transparent but keeps it in the layout, fully interactive, and focusable, which can cause invisible-but-clickable elements if not handled carefully.

🏏

Cricket analogy: display: none is like a player being dropped from the squad entirely, taking no space on the team sheet; visibility: hidden is like a player named as twelfth man, still on the sheet but not playing; opacity: 0 is like a player on the field who's been told to stand invisible but can still be tackled or interact.

Q: How would you center a div both horizontally and vertically?

The modern, most robust approach is Flexbox: set display: flex, justify-content: center, and align-items: center on the parent. Grid offers an equally concise alternative with display: grid; place-items: center. An older technique uses position: absolute with top: 50%; left: 50%; transform: translate(-50%, -50%) when the parent is positioned. Interviewers often want you to name multiple approaches and explain their trade-offs.

🏏

Cricket analogy: Centering with Flexbox's justify-content and align-items is like a fielding captain placing a fielder dead center using two simple directives, while the older absolute-positioning-with-transform trick is like manually pacing out fifty percent of the pitch and adjusting by half a fielder's width.

Q: What is a stacking context, and how does z-index actually work?

z-index only has an effect on elements with a position value other than static, and it only compares elements within the same stacking context. A new stacking context is created by properties like position with a z-index, opacity less than 1, transform, filter, or will-change. Because stacking contexts nest, a child with a very high z-index can still be visually buried beneath a sibling if its parent's stacking context is layered below that sibling's — a common source of 'z-index isn't working' bugs.

🏏

Cricket analogy: z-index only mattering within the same stacking context is like a fielder's positioning priority only applying within their own team's formation; a star fielder positioned deep in a weak formation can still be effectively 'buried' behind a rival team's stronger overall setup.

Quick Reference

  • Box model order: content → padding → border → margin.
  • box-sizing: border-box includes padding and border inside the declared width/height.
  • Specificity order: inline style > ID > class/attribute/pseudo-class > element.
  • Flexbox = one-dimensional layout; Grid = two-dimensional layout.
  • Semantic HTML improves accessibility, SEO, and maintainability.
  • Block elements start new lines and accept full box sizing; inline elements do not.
  • position: absolute is relative to the nearest positioned ancestor, not always the viewport.
  • rem scales from the root font-size; em can compound through nested elements.
  • The cascade resolves ties using origin/importance, then specificity, then source order.
  • display: none removes layout space; visibility: hidden and opacity: 0 do not.
  • z-index only compares elements within the same stacking context.
  • Flexbox or Grid with center alignment is the modern way to center content.

Key Takeaways

  • Most HTML/CSS interview questions test conceptual understanding of rendering and layout, not memorized syntax.
  • The box model and box-sizing are foundational — get comfortable explaining them precisely.
  • Know when to reach for Flexbox versus Grid and be able to justify the choice.
  • Semantic HTML and accessibility are treated as core skills, not optional extras.
  • Understanding specificity, the cascade, and stacking contexts prevents most 'CSS isn't working' debugging sessions.

Practice what you learned

Was this page helpful?

Topics covered

#HTMLCSS#HTMLCSSStudyNotes#WebDevelopment#CommonHTMLCSSInterviewQuestions#Common#HTML#CSS#Interview#StudyNotes#SkillVeris