Skip to content
Laviyx Learn

Try

CSS course menu

Intermediate CSS Interview Questions and Answers

Intermediate CSS interview questions with answers: specificity calculation, Flexbox vs Grid, justify-content vs align-items, flex: 1, margin collapsing, stacking contexts and z-index, media queries and mobile first, responsive images, transitions vs animations, and BEM.

Intermediate5 min readUpdated
On this page

These questions check whether you can reason about layout and the cascade, not just recall properties. Practise explaining the "why" behind each answer.

1. How is specificity calculated? Which wins: #nav a or .menu .item a?#

Show solution

Specificity is counted as (ids, classes/attributes/pseudo-classes, elements/pseudo-elements) and compared left to right. #nav a is (1, 0, 1); .menu .item a is (0, 2, 1). The id wins, because one id outranks any number of classes. Inline styles beat both, and !important beats normal declarations. :where() contributes zero specificity.

2. When would you use Flexbox and when Grid?#

Show solution

Flexbox is one-dimensional: items in a row or a column, sized by their content (navbars, toolbars, aligning items in a line). Grid is two-dimensional: rows and columns together, where you define the layout and items fill it (page layouts, galleries, dashboards). They are often combined: a Grid page with Flexbox inside components.

3. What is the difference between justify-content and align-items?#

Show solution

In Flexbox, justify-content aligns items along the main axis (horizontal for flex-direction: row), and align-items aligns them along the cross axis (vertical for rows). With flex-direction: column the axes swap. In Grid, justify-* is the inline (row) axis and align-* the block (column) axis.

4. What does flex: 1 mean?#

Show solution

It is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. Items start from zero size and share the free space equally, so several flex: 1 items end up equal width (unless their content is wider than their share). flex: auto (1 1 auto) starts from the content size instead, and flex: none keeps the natural size.

5. What is margin collapsing?#

Show solution

When vertical margins of block boxes touch, they combine into a single margin equal to the larger one, instead of adding up. It happens between adjacent siblings and between a parent and its first or last child when no padding, border or content separates them. It never happens to horizontal margins or inside flex and grid containers.

6. Why might z-index not work?#

Show solution

Common reasons: the element is not positioned (and not a flex or grid item), so z-index is ignored; or the element is inside a stacking context (created by an ancestor with opacity below 1, a transform, a filter, a positioned element with a z-index, and so on). A child can never appear above something outside its parent's stacking context, whatever its own z-index.

7. Explain mobile-first CSS.#

Show solution

Write base styles for small screens, then add @media (min-width: ...) rules for larger screens, in increasing order. Small-screen layouts are usually simpler, so the base CSS stays small and larger screens add complexity rather than undoing it. It also pairs with the viewport meta tag and fluid units.

8. How do you make images responsive?#

Show solution

In CSS: img { max-width: 100%; height: auto; } so images shrink with their container and keep their aspect ratio, plus width and height attributes in HTML to prevent layout shift. In HTML: srcset and sizes (or <picture>) so the browser downloads an appropriately sized file. object-fit: cover crops images to fill a box with a fixed aspect ratio.

9. What is the difference between transitions and animations?#

Show solution

A transition animates between two states when a property changes (for example on :hover); it needs a trigger and only has a start and an end. A keyframe animation (@keyframes plus animation) can run on its own, loop, have many intermediate steps, alternate direction and be paused. For performance, animate transform and opacity.

10. What are CSS custom properties and how do they differ from Sass variables?#

Show solution

Custom properties (--brand: #2f5fe0; read with var(--brand)) live in the browser: they inherit through the DOM, can be changed per component, in media queries and by JavaScript at runtime, which makes them ideal for theming. Sass variables are replaced with fixed values when the Sass is compiled, so they cannot change at runtime or per element.

11. What is BEM?#

Show solution

A naming convention: Block (.card), Element (.card__title) and Modifier (.card--featured). Every selector is a single class, so specificity stays low and equal, names show where styles belong, and components do not leak into each other.

12. How do you hide content visually but keep it for screen readers?#

Show solution

Use a "visually hidden" utility class rather than display: none or visibility: hidden (which also hide content from assistive technology):

CSS
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

13. What is the difference between em and rem for spacing, and when would you use each?#

Show solution

rem spacing is consistent across the whole page because it depends only on the root font size; use it for most layout spacing. em spacing scales with the element's own font size, which is useful inside components that come in sizes, such as buttons whose padding should grow with their text (padding: 0.5em 1em).

14. What does object-fit do?#

Show solution

It controls how an <img> or <video> fills its box when the box has a different aspect ratio: cover fills the box and crops the excess, contain shows the whole image with empty space, fill (default) stretches it. object-position chooses which part stays visible.

Keep going#

Continue with the advanced CSS questions, or browse every question with hints on the CSS interview page.

Finished this lesson?

CSSBeginner CSS Interview Questions and AnswersCommon CSS interview questions for freshers with clear answers: what CSS is, ways to add CSS, selectors, the box model, margin vs padding, display values, position, units, id vs class, inheritance, and pseudo-classes vs pseudo-elements.Beginner4 min readCSSAdvanced CSS Interview Questions and AnswersAdvanced CSS interview questions with answers: the full cascade and cascade layers, block formatting contexts, containing blocks, rendering performance (layout, paint, composite), container queries, :has(), CSS-in-JS vs utility CSS, critical CSS, and accessibility in CSS.Advanced5 min readCSSCSS Flexbox: A Complete Beginner's Guide with ExamplesLearn CSS Flexbox with live examples: flex containers and items, flex-direction, main and cross axes, justify-content, align-items, flex-wrap, gap, align-content, and common layouts such as navbars, card rows and sticky footers.Intermediate7 min readCSSCSS Grid Layout: A Complete Beginner's Guide with ExamplesLearn CSS Grid with live examples: grid containers, grid-template-columns and rows, the fr unit, repeat(), gap, minmax(), auto-fit and auto-fill for responsive grids without media queries, spanning cells, and aligning grid items.Intermediate6 min readCSSCSS Cascade, Specificity and Inheritance ExplainedUnderstand how CSS decides which style wins: the cascade (origin, importance, layers, specificity, order), how to calculate specificity for ids, classes and elements, which properties are inherited, inherit/initial/unset, and why to avoid !important.Beginner6 min read