HTML & CSS Middle interview questions and answers
75 questions for a HTML & CSS (Middle) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.What is the specificity of the selector
nav#main .item:hover > a::before? - 2.How do you override a third-party library's
!importantrule without touching its code? - 3.What does the shorthand
flex: 1expand to? - 4.How does
flex-basisdiffer fromwidthon a flex item in a row (flex-direction: row)? - 5.Inside a flex item, long text with
text-overflow: ellipsisdoesn't truncate and stretches the column instead. How do you fix it? - 6.How does
repeat(auto-fill, minmax(200px, 1fr))differ fromrepeat(auto-fit, minmax(200px, 1fr))? - 7.How do you define a "header on top, sidebar and content in the middle, footer at the bottom" layout in Grid with named areas?
- 8.What is an implicit grid?
- 9.What does
place-items: centerdo on a grid container? - 10.When is Grid more appropriate, and when Flexbox?
- 11.What does
font-size: clamp(1rem, 2.5vw, 2rem)resolve to on a 1000px-wide screen (1rem = 16px)? - 12.Why use logical properties like
margin-inline-startinstead ofmargin-left? - 13.How does
transitiondiffer fromanimation? - 14.Which properties are best to animate for smooth 60 FPS?
- 15.Why is
transform: translate()often better than changinglefton aposition: absoluteelement for movement animations? - 16.How should
will-changebe used? - 17.What is Critical CSS?
- 18.What does the CLS (Cumulative Layout Shift) metric measure?
- 19.Which technique best prevents layout shifts caused by loading images?
- 20.What does
font-display: swapdo in@font-face? - 21.What are the
srcsetandsizesattributes on<img>for? - 22.When do you need
<picture>rather than just<img srcset>? - 23.What does
loading="lazy"on<img>do, and when should you NOT use it? - 24.What does
object-fit: coverdo on an<img>with setwidthandheight? - 25.What is the specificity of
:where(.card, #hero) p? - 26.What does the selector
.card:has(img)match? - 27.How do CSS variables (
--x) fundamentally differ from Sass variables ($x)? - 28.What is a stacking context, and why might
z-index: 9999"not work"? - 29.
position: stickyisn't working even thoughtop: 0is set. What's the most likely cause? - 30.What is a block formatting context (BFC), and how do you create one?
- 31.What's new about the
@media (width >= 768px)syntax? - 32.What are
@containercontainer queries for? - 33.Why can
height: 100vhbe taller than the visible area on mobile, and how do you fix it? - 34.What does the
currentColorvalue mean? - 35.How do the
initial,inheritandunsetvalues differ? - 36.In BEM, how do you correctly name the title class inside a
cardblock in its "accent" variant? - 37.How do CSS Modules solve class-name collisions?
- 38.Why is
:focus-visiblebetter than:focusfor styling the focus outline? - 39.What does
tabindex="-1"do? - 40.When is
aria-labeljustified? - 41.How do you apply an arbitrary value in Tailwind that isn't in the theme?
- 42.Why can't you build a Tailwind class name like `
bg-${color}-500`? - 43.How do you add a custom
brandcolor in Tailwind v4 so thatbg-brand,text-brand, etc. work? - 44.What are
groupandpeerfor in Tailwind? - 45.A React component merges external classes:
className={"p-2 " + props.className}, and receivesp-4. What padding results? - 46.What is Tailwind's
@applydirective for, and why shouldn't you overuse it? - 47.How do you accommodate users for whom animations cause discomfort?
- 48.How do you build a two-column layout with a fixed-width sidebar and content filling the rest?
- 49.Which image format usually gives the best compression for photos in modern browsers?
- 50.How do you properly include print-only styles?
- 51.What is the specificity of
:is(#hero, .card) p? - 52.What does
aspect-ratio: 16 / 9give you on a video container? - 53.What does
inset: 0mean on an absolutely positioned overlay? - 54.What does
grid-auto-flow: densedo? - 55.Why add
flex-shrink: 0to an icon inside a flex row with long text? - 56.How do you push the last item of a flex navbar to the far right?
- 57.What does
object-position: topdo withobject-fit: cover? - 58.What is
@supportsfor? - 59.How do you follow the user's system dark theme in plain CSS?
- 60.What does
color-scheme: light darkon:rootdo? - 61.What is
accent-colorfor? - 62.What does
.field:focus-withinmatch? - 63.How do you change the color of list bullets without touching the text?
- 64.How can CSS number headings like "Step 1", "Step 2" automatically?
- 65.How do you truncate text to three lines with an ellipsis?
- 66.Why is there a small gap under an
<img>inside a<div>? - 67.What does
transform-originchange? - 68.Why is
transition: all 0.3sconsidered a bad habit? - 69.Why add
aria-hidden="true"to a decorative icon next to a button label? - 70.How do you make a screen reader announce "Saved" after an async action without moving focus?
- 71.What is needed to make
<div role="button">behave like a real button? - 72.How do you hide a text label visually but keep it for screen readers?
- 73.What
altshould an icon-only link to the home page have? - 74.How do you style an element based on a
data-stateattribute in Tailwind? - 75.What does
srcset="logo.png 1x, logo@2x.png 2x"do?