CSS Interview Questions
Answers written to be said out loud in an interview — each with a common follow-up question and a link to the tutorial that explains it in depth.
- Beginner
- 8
- Intermediate
- 13
- Experienced
- 8
01Explain the CSS box model and box-sizing.Beginner
Every element is a box of content, padding, border and margin. With the default content-box, width covers only the content; padding and border are added on top. With border-box, width includes padding and border, which makes sizing predictable.
02What are the different values of the position property?Beginner
static (default flow), relative (offset from its normal place), absolute (positioned relative to the nearest positioned ancestor), fixed (relative to the viewport) and sticky (behaves like relative until a scroll threshold, then sticks).
03What is the difference between em, rem and px?Beginner
px is a fixed unit. em is relative to the font size of the element (or parent for font-size), so it compounds when nested. rem is relative to the root font size, which makes it predictable and good for accessibility.
04What are the three ways to add CSS to a page, and which is preferred?Beginner
Inline styles (style="…"), an internal <style> block and an external stylesheet linked with <link rel="stylesheet">. External files are preferred: they are cached, shared across pages and keep content separate from presentation. Inline styles also have the highest specificity, which makes them hard to override.
05What is the cascade?Beginner
The algorithm that decides which declaration wins when several rules set the same property. It compares origin and importance (!important), then cascade layers, then specificity, and finally source order — the later rule wins.
06Which properties are inherited?Beginner
Mostly text-related ones: color, font-family, font-size, line-height, text-align, visibility. Box properties such as margin, padding, border, width and background are not inherited. Any property can be forced with the inherit keyword.
07display: none vs visibility: hidden vs opacity: 0?Beginner
display: none removes the element from layout and from the accessibility tree. visibility: hidden keeps its space but hides it and makes it non-interactive. opacity: 0 keeps space and still receives clicks and focus.
08block vs inline vs inline-block?Beginner
Block elements start on a new line and take the full width; width, height and vertical margins apply. Inline elements flow inside text and ignore width, height and vertical margins. inline-block flows like text but respects width, height and all margins.
09When would you choose Flexbox over CSS Grid?Intermediate
Flexbox is one-dimensional — ideal for laying out items in a single row or column such as toolbars and navigation. Grid is two-dimensional — ideal for page layouts and galleries where rows and columns must align. They are often combined.
10How is CSS specificity calculated?Intermediate
Specificity is compared as a tuple: inline styles, then ID selectors, then classes/attributes/pseudo-classes, then elements/pseudo-elements. The higher tuple wins; on a tie, the later rule wins. !important overrides normal declarations.
11What is the difference between pseudo-classes and pseudo-elements?Intermediate
Pseudo-classes (:hover, :focus-visible, :nth-child) select elements in a particular state. Pseudo-elements (::before, ::after, ::placeholder) style a specific part of an element or insert generated content.
12How does z-index work, and why does it sometimes “not work”?Intermediate
z-index only applies to positioned elements (and flex/grid items) and compares within the same stacking context. A parent with transform, opacity < 1 or its own z-index creates a new stacking context, trapping children inside it.
13What are CSS custom properties and how are they different from Sass variables?Intermediate
Custom properties (--brand: #4f46e5) live in the browser, cascade, inherit and can be changed at runtime — for example to switch themes. Sass variables are replaced at build time and cannot change after compilation.
14Explain mobile-first responsive design.Intermediate
Write base styles for small screens first, then add min-width media queries to enhance layouts for larger screens. It keeps CSS simpler and prioritises the most constrained devices.
15What is margin collapsing?Intermediate
Vertical margins of adjacent block elements (or a parent and its first/last child) combine into a single margin equal to the larger one, instead of adding up. It does not happen in flex or grid containers, with padding or borders between them, or with horizontal margins.
16How do you centre an element horizontally and vertically?Intermediate
The modern answer is a flex or grid container: display: grid; place-items: center; or display: flex; justify-content: center; align-items: center. For a fixed-width block, margin-inline: auto centres it horizontally.
.center {
display: grid;
place-items: center;
min-height: 100vh;
}
17What do flex-grow, flex-shrink and flex-basis do?Intermediate
flex-basis is the starting size before free space is shared. flex-grow sets how much of the leftover space an item takes; flex-shrink how much it gives up when there is not enough room. flex: 1 means 1 1 0%: grow equally from zero.
18What does repeat(auto-fit, minmax(250px, 1fr)) do?Intermediate
It creates as many columns as fit, each at least 250px and sharing leftover space equally — a responsive card grid with no media queries. auto-fit collapses empty tracks so items stretch; auto-fill keeps empty tracks.
19What are grid-template-areas?Intermediate
Named regions that let you draw the layout in ASCII: grid-template-areas: "header header" "sidebar main". Children are placed with grid-area: header. Redefining the areas in a media query rearranges the whole page without touching HTML.
20Transitions vs animations?Intermediate
A transition animates a property between two states when it changes (for example on :hover). A @keyframes animation can run on its own, loop, have many steps and play on page load. Both should respect prefers-reduced-motion.
21What is a stacking context?Intermediate
An isolated layer in which z-index values are compared. It is created by position with z-index, opacity below 1, transform, filter, isolation: isolate and more. A child can never appear above something outside its parent’s stacking context, whatever its z-index.
22Which CSS properties are cheap to animate and why?Experienced
transform and opacity can usually be handled by the compositor without triggering layout or paint, so they animate smoothly. Animating width, height, top or left forces layout recalculation on every frame.
23How do you organise CSS in a large project?Experienced
Use a consistent methodology (BEM, utility-first, or CSS Modules), design tokens via custom properties, low and flat specificity, and component-scoped styles. Avoid deep nesting and !important.
24What are container queries and how are they different from media queries?Experienced
Media queries respond to the viewport; container queries respond to the size of a parent marked with container-type: inline-size. That lets a component adapt to where it is placed — a card in a sidebar versus in the main column.
25How do you make CSS colours and units theme-able?Experienced
Define design tokens as custom properties on :root (--surface, --text, --radius) and use them everywhere. Swap their values for dark mode with prefers-color-scheme or a data-theme attribute. Components then never hard-code colours.
26What causes layout shift (CLS) and how does CSS help prevent it?Experienced
Content that changes size after first paint: images without dimensions, late web fonts, injected banners. Set width/height or aspect-ratio on media, reserve space for dynamic content, and use font-display with size-adjusted fallback fonts.
27What do :is(), :where() and :has() add?Experienced
:is() groups selectors and takes the highest specificity in its list. :where() does the same with zero specificity, ideal for resets. :has() is a parent selector: .card:has(img) styles a card that contains an image.
28BEM, utility-first and CSS modules — how do they compare?Experienced
BEM (block__element--modifier) is a naming convention that keeps specificity flat in global CSS. Utility-first (Tailwind) composes small single-purpose classes in markup. CSS modules scope class names per component at build time. All three avoid accidental global clashes.
29How do you make focus styles accessible?Experienced
Never remove outlines without a replacement. Use :focus-visible to show a clear ring for keyboard users without showing it on mouse clicks, with at least 3:1 contrast against the background.