CSS · Practice

CSS Interview Questions

29 questions you are likely to be asked about CSS, from beginner to experienced level — each with a spoken-style answer, a common follow-up and a link to the lesson behind it.

Advanced 15 min Topic 26 of 26
0/26 in CSS

Beginner questions (8)

01Explain the CSS box model and box-sizing.

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.

Learn: Box Model
02What are the different values of the position property?

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).

Learn: Positioning
03What is the difference between em, rem and px?

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.

Learn: Typography
04What are the three ways to add CSS to a page, and which is preferred?

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.

Learn: Introduction to CSS
05What is the cascade?

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.

Learn: Specificity and Inheritance
06Which properties are inherited?

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.

Learn: Specificity and Inheritance
07display: none vs visibility: hidden vs opacity: 0?

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.

Learn: Display Properties
08block vs inline vs inline-block?

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.

Learn: Display Properties

Intermediate questions (13)

01When would you choose Flexbox over CSS Grid?

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.

Learn: Flexbox
02How is CSS specificity calculated?

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.

Learn: Specificity and Inheritance
03What is the difference between pseudo-classes and pseudo-elements?

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.

Learn: Pseudo-Elements
04How does z-index work, and why does it sometimes “not work”?

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.

Learn: Z-Index
05What are CSS custom properties and how are they different from Sass variables?

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.

Learn: CSS Variables
06Explain mobile-first responsive design.

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.

Learn: Media Queries
07What is margin collapsing?

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.

Learn: Margin and Padding
08How do you centre an element horizontally and vertically?

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.

CSS Illustrative
.center {
  display: grid;
  place-items: center;
  min-height: 100vh;
}
Learn: Flexbox
09What do flex-grow, flex-shrink and flex-basis do?

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.

Learn: Flexbox
10What does repeat(auto-fit, minmax(250px, 1fr)) do?

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.

Learn: CSS Grid
11What are grid-template-areas?

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.

Learn: CSS Grid
12Transitions vs animations?

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.

Learn: Animations
13What is a stacking context?

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.

Learn: Z-Index

Experienced questions (8)

01Which CSS properties are cheap to animate and why?

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.

Learn: Animations
02How do you organise CSS in a large project?

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.

Learn: CSS Architecture
03What are container queries and how are they different from media queries?

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.

Learn: Responsive Design
04How do you make CSS colours and units theme-able?

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.

Learn: CSS Variables
05What causes layout shift (CLS) and how does CSS help prevent it?

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.

Learn: Responsive Design
06What do :is(), :where() and :has() add?

: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.

Learn: Selectors
07BEM, utility-first and CSS modules — how do they compare?

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.

Learn: CSS Architecture
08How do you make focus styles accessible?

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.

Learn: Accessibility
Progress is saved in this browser. Sign in to sync.