Back to Interview PrepGet interview ready with AILearn each topic the way interviewers ask it, then answer like you would in the interview.

Chapter 14 of 14

Top 100 CSS Interview Questions

A rapid-fire reference covering the questions most likely to come up in a real interview - organized by topic and difficulty, with explanations that go beyond a one-line definition and a runnable code example for each one, so the concept sticks. Search or filter to find what you need, and share a direct link to any question.

Level:

Showing 100 of 100 questions

Questions reviewed0/100 · 0%

The first 15 of 100 questions are free to read. Sign up free to unlock every answer and track your progress.

Unlock all 100

CSS (Cascading Style Sheets) describes how HTML content should look - colors, spacing, layout, typography - separating presentation from structure. Without it, browsers would render every page with the same plain default stylesheet, and any visual customization would have to live inline on every element, which quickly becomes unmaintainable at scale.

Example

p {
  color: #333;
  font-size: 16px;
}

Inline styles (the style attribute) apply to a single element and are useful for quick one-offs or dynamically generated styles, but they’re hard to maintain and have the highest specificity. Internal styles (a <style> block in the head) suit a single page with no shared styling needs. External stylesheets (linked via <link>) are preferred for real projects because they’re cacheable, reusable across pages, and keep markup clean.

Example

<!-- external is preferred -->
<link rel="stylesheet" href="styles.css">

It refers to the algorithm the browser uses to resolve conflicting rules that target the same element: it weighs origin (browser vs author vs user styles), specificity, and source order, applying later rules over earlier ones when specificity ties. Understanding the cascade is the difference between guessing why a style “isn’t working” and knowing exactly which rule won and why.

Example

p { color: blue; }
p { color: red; } /* wins: same specificity, comes later */

Inheritance means some properties automatically pass their computed value from a parent element down to its children unless overridden - mostly text-related properties like color, font-family, and line-height. Box-model properties like margin, padding, border, and width do not inherit, because it would rarely make sense for every nested box to automatically copy its parent’s spacing.

Example

body {
  font-family: sans-serif; /* inherited by all text */
  color: #222;
}

Every element is rendered as a rectangular box made of content, padding, border, and margin, from the inside out. Padding adds space inside the border, margin adds space outside it, and the total rendered size is the sum of all four layers unless box-sizing changes how width and height are calculated. This model underlies virtually every layout calculation in CSS.

Example

.box {
  width: 200px;
  padding: 10px;
  border: 2px solid #000;
  margin: 20px;
}

Absolute units like px are fixed regardless of context, giving predictable but rigid sizing. Relative units like %, em, rem, vw, and vh scale based on something else - a parent’s size, the root font size, or the viewport - which is what makes responsive, accessible layouts possible, since they respond to user zoom and different screen sizes automatically.

Example

.title {
  font-size: 2rem;   /* relative to root */
  width: 50%;        /* relative to parent */
  height: 100vh;     /* relative to viewport */
}

CSS comments are wrapped in /* ... */ and can span multiple lines; there’s no single-line // syntax like in JavaScript. They’re stripped out before rendering and are useful for documenting why a particular hack or override exists, since CSS rules alone rarely explain their own intent.

Example

/* TODO: remove once the new nav ships */
.legacy-nav { display: none; }

!important overrides normal cascade and specificity rules, forcing that declaration to win regardless of what else targets the element - short of another !important with higher specificity or a later one at the same specificity. It’s discouraged because it breaks the predictable order of the cascade, making future overrides require yet more !important flags, a pattern that snowballs into unmaintainable stylesheets.

Example

.badge {
  color: red !important; /* wins over almost everything else */
}

Browsers ship their own default stylesheets (user-agent styles) that differ slightly between browsers - default margins on headings, list bullet styles, form control fonts, and so on. A reset aggressively zeroes out these defaults so you build up from a blank slate, while normalize.css instead makes defaults consistent across browsers without removing sensible ones, which is usually the gentler and more common choice today.

Example

/* minimal reset */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

User-agent styles are the baseline CSS every browser applies automatically before any author stylesheet loads - things like default margins on <p> and <h1>, bullet points on <ul>, and blue underlined links. They matter because unstyled elements are never truly “unstyled”; they inherit these built-in defaults, which is why a page can look reasonably formatted even before any custom CSS is written.

Example

/* browser default, roughly */
h1 { margin: 0.67em 0; font-size: 2em; }
a { color: blue; text-decoration: underline; }

The core types are type selectors (p, div), class selectors (.card), id selectors (#header), attribute selectors ([type="text"]), and pseudo-class/pseudo-element selectors (:hover, ::before). Each has a different specificity weight, and picking the right one is as much about maintainability - avoiding overly tight coupling to markup - as it is about matching the right elements.

Example

p { color: gray; }
.card { border: 1px solid #ddd; }
#logo { width: 120px; }
[type="text"] { padding: 4px; }

A descendant combinator (space) matches any nested element at any depth, while a child combinator (>) matches only direct children. An adjacent sibling combinator (+) matches the very next sibling, and a general sibling combinator (~) matches all siblings that follow, regardless of distance. Choosing the tightest combinator that expresses your intent avoids accidentally styling unrelated nested elements.

Example

.card p { }      /* any descendant paragraph */
.card > p { }    /* direct child paragraph only */
h2 + p { }       /* paragraph immediately after an h2 */
h2 ~ p { }       /* all paragraphs after an h2, same parent */

Specificity is commonly expressed as a tuple of (inline styles, ids, classes/attributes/pseudo-classes, type selectors/pseudo-elements), and a rule with a higher value in an earlier category always outweighs any number of matches in a later one - one id beats a thousand classes. When two selectors have identical specificity, the cascade falls back to source order, so whichever rule appears later in the stylesheet wins.

Example

#nav .item { }      /* specificity: 1 id, 1 class */
.nav .item.active { } /* specificity: 0 id, 3 classes - loses to above */

Attribute selectors can match presence ([disabled]), exact value ([type="text"]), substring at the start ([href^="https"]), substring at the end ([src$=".png"]), or substring anywhere ([class*="col-"]). This is powerful for targeting elements based on data or state without adding extra classes, though heavy reliance on them can make styles harder to trace back to markup intent.

Example

a[href^="https"] { color: green; }
img[src$=".png"] { border: none; }

:not() excludes elements matching its argument, while :is() and :where() let you group multiple selectors into one to avoid repetition. :is() takes on the specificity of its most specific argument, but :where() always contributes zero specificity regardless of what’s inside it, which makes it ideal for writing low-priority base styles or resets that are trivially easy to override later.

Example

:is(h1, h2, h3) { font-weight: 700; }
:where(h1, h2, h3) { margin: 0; } /* contributes 0 specificity */
16.Why is the universal selector (*) considered expensive, and when is it still reasonable to use?17.What’s the difference between chaining selectors together and combining them with a comma?18.What are pseudo-classes and pseudo-elements, and how do you tell them apart syntactically?19.How do :first-child, :last-child, and :nth-child() differ from :first-of-type and :nth-of-type()?20.How would you debug a style that “isn’t applying” when you believe your selector is correct?21.What’s the difference between content-box and border-box for box-sizing?22.What is margin collapsing, and when does it happen?23.When should you reach for padding versus margin?24.What are the common values of the overflow property, and what does each do?25.What are the most common display values, and how do they differ in flow behavior?26.Why is float considered a legacy layout technique, and what problem does clearfix solve?27.What’s the difference between visibility: hidden, display: none, and opacity: 0?28.What does the display property’s inline-block value give you that plain inline doesn’t?29.What is a block formatting context, in plain terms?30.How do min-width/max-width and min-height/max-height interact with a fixed width?31.Why do font-family declarations usually list several fonts as a stack?32.What’s the practical difference between rem, em, px, and %?33.Why is a unitless line-height generally recommended over a pixel value?34.What are the main ways to represent color in CSS, and how does alpha transparency fit in?35.What does the currentColor keyword do, and where is it useful?36.What does @font-face do, and what should you consider when using custom web fonts?37.How do you truncate overflowing text with an ellipsis, and what properties does it require?38.What’s the difference between word-break and overflow-wrap when handling long words?39.What’s the difference between font-weight: bold and the <strong> element, from a styling perspective?40.How would you handle color contrast to keep text accessible?41.What are the differences between static, relative, absolute, fixed, and sticky positioning?42.What determines the “containing block” for an absolutely positioned element?43.How does z-index work, and what creates a new stacking context?44.Why does position: sticky sometimes fail to stick, even with top: 0 set?45.What are the common ways to horizontally and vertically center an element?46.How does the transform property affect stacking context and containing block behavior?47.What’s the difference between relative and absolute positioning offsets, in terms of what they’re measured from?48.What happens if you set a negative z-index on an element?49.How would you build a full-page overlay or modal backdrop with CSS positioning?50.Why can a child with z-index: 9999 still render behind another element with a much lower z-index?51.How do flex-grow, flex-shrink, and flex-basis work together to size flex items?52.What’s the difference between justify-content, align-items, and align-content in flexbox?53.What does flex-wrap do, and what’s the difference between nowrap, wrap, and wrap-reverse?54.How do you define named grid areas with CSS Grid, and why are they useful?55.What is the fr unit in CSS Grid, and how does it differ from a percentage?56.What’s the difference between minmax(), auto-fit, and auto-fill in a grid template?57.When would you reach for flexbox over CSS Grid, and vice versa?58.What does the gap property do, and does it work in both flexbox and grid?59.What does align-self do differently from align-items?60.How do you make a flex item shrink-to-fit its content instead of stretching to fill the row?61.What’s the difference between mobile-first and desktop-first responsive design?62.How would you decide where to place media query breakpoints?63.What does the viewport meta tag do, and why does responsive CSS depend on it?64.How does clamp() help with responsive typography, and how do its three arguments work?65.What are container queries, and how do they differ from media queries?66.What does the aspect-ratio property do, and what problem did people solve without it before?67.How would you make images responsive by default in a fluid layout?68.What’s the difference between a min-width and a max-width media query?69.How can CSS Grid’s repeat(auto-fit, minmax()) reduce the number of media queries needed for a responsive grid?70.What accessibility consideration is often overlooked when hiding content at certain breakpoints?71.When would you use a transition instead of an animation?72.How does @keyframes syntax work, and what do the percentage values represent?73.What does animation-fill-mode control, and what problem does forwards solve?74.What do easing/timing functions like ease, linear, and cubic-bezier() actually control?75.What does the will-change property do, and why should it be used sparingly?76.Why is animating transform and opacity preferred over animating top/left or width/height for performance?77.What is the prefers-reduced-motion media feature, and why does it matter?78.Can you transition a property change that happens via a class toggle in JavaScript?79.What happens if you set animation-iteration-count: infinite alongside animation-direction: alternate?80.How would you delay a transition or animation from starting immediately?81.How do CSS custom properties (variables) differ from Sass/Less variables?82.What does the :has() selector let you do that wasn’t previously possible in pure CSS?83.How does native CSS nesting work, and what should you watch out for with specificity?84.What problem do cascade layers (@layer) solve?85.What’s a common gotcha around margin collapsing that trips up beginners?86.Why does setting z-index sometimes have no visible effect at all?87.What’s a practical strategy for avoiding “specificity wars” in a growing codebase?88.What are logical properties, and why use margin-inline-start over margin-left?89.What’s the difference between a CSS custom property’s fallback value and its inherited value?90.How do cascade layers interact with normal specificity and !important?91.What is the critical rendering path, and how does CSS factor into it?92.Why are certain selectors considered “expensive,” and how does the browser actually match them?93.What’s the practical difference between a BEM-based architecture and a utility-first (like Tailwind-style) approach?94.What’s the difference between a browser reflow (layout) and a repaint, and why does it matter for performance?95.Why should !important be minimized as a general architectural practice, beyond just being “bad style”?96.What does the CSS contain property do, and how does it help performance?97.How would you go about identifying and removing unused CSS from a large project?98.What are the general performance tradeoffs of CSS-in-JS versus plain CSS or CSS modules?99.Why is it generally better to load CSS in the <head> and JavaScript near the end of <body> (or with defer)?100.What’s a sensible high-level strategy for keeping a large team’s CSS maintainable over time?
You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in