Tutorials Logic, IN info@tutorialslogic.com
CSS

Top 50 CSS Interview Questions

Curated questions covering flexbox, grid, specificity, box model, animations, responsive design, CSS variables, and modern CSS features.

01

Explain the CSS Box Model.

The CSS Box Model describes every element as a rectangular box with four areas: content (innermost), padding (space between content and border), border, and margin (outermost space between elements). Total width = content + padding + border + margin.

02

What is the difference between box-sizing: content-box and border-box?

content-box (default) — width/height applies to content only; padding and border are added on top. border-box — width/height includes content, padding, and border. border-box makes layout calculations much easier and is the modern standard.

Example
* { box-sizing: border-box; } /* recommended global reset */
03

Explain CSS specificity.

  • Inline styles — highest specificity (1,0,0,0)
  • ID selectors — (0,1,0,0)
  • Class, attribute, pseudo-class selectors — (0,0,1,0)
  • Element and pseudo-element selectors — (0,0,0,1)
  • !important overrides all specificity (avoid using it).
04

Compare Flexbox and CSS Grid.

  • Flexbox — one-dimensional layout (row OR column). Best for component-level layouts and aligning items along one axis.
  • CSS Grid — two-dimensional layout (rows AND columns). Best for page-level layouts.
  • They complement each other — use Grid for the overall layout and Flexbox for components within.
05

Describe CSS pseudo-classes and pseudo-elements.

  • Pseudo-classes (:) — select elements based on state: :hover, :focus, :nth-child(), :first-child, :not(), :checked.
  • Pseudo-elements (::) — style specific parts of an element: ::before, ::after, ::first-line, ::first-letter, ::placeholder.
Example
a:hover { color: blue; }
p::first-line { font-weight: bold; }
06

What is the difference between position: relative, absolute, fixed, and sticky?

  • relative — positioned relative to its normal position; still occupies space in the flow.
  • absolute — removed from flow; positioned relative to nearest positioned ancestor.
  • fixed — removed from flow; positioned relative to the viewport; stays on scroll.
  • sticky — hybrid of relative and fixed; sticks when it reaches a scroll threshold.
07

How does z-index work?

z-index controls the stacking order of positioned elements (position other than static). Higher z-index appears on top. z-index only works on positioned elements. Elements in the same stacking context are compared; nested stacking contexts are treated as a unit.

08

What are CSS custom properties (variables)?

CSS custom properties (--variable-name) store reusable values. They cascade and can be overridden in nested scopes. Accessed with var().

Example
:root {
  --primary: #38bdf8;
  --spacing: 16px;
}

.btn {
  background: var(--primary);
  padding: var(--spacing);
}
09

Compare display: none and visibility: hidden.

display: none removes the element from the document flow — it takes up no space and is not accessible to screen readers. visibility: hidden hides the element visually but it still occupies its space in the layout and remains in the accessibility tree.

10

Describe CSS media queries.

Media queries apply styles based on device characteristics like screen width, height, orientation, or resolution. They are the foundation of responsive design.

Example
@media (max-width: 768px) {
  .container { flex-direction: column; }
}
@media (prefers-color-scheme: dark) {
  body { background: #0f172a; }
}
11

What is the difference between em, rem, px, vw, and vh?

  • px — absolute pixels; not affected by user font size preferences.
  • em — relative to the font-size of the parent element; compounds in nested elements.
  • rem — relative to the root (html) font-size; preferred for consistent scaling.
  • vw/vh — percentage of viewport width/height; useful for full-screen layouts.
12

Explain the CSS cascade.

The cascade determines which CSS rule applies when multiple rules target the same element. Priority order: !important > inline styles > specificity > source order. The browser also applies user-agent (browser default) styles at the lowest priority.

13

What is Flexbox and what are its main properties?

  • Container: display:flex, flex-direction, flex-wrap, justify-content, align-items, align-content, gap.
  • Items: flex-grow, flex-shrink, flex-basis, flex (shorthand), align-self, order.
  • justify-content aligns along the main axis; align-items aligns along the cross axis.
14

Explain CSS Grid and how do you define rows and columns.

CSS Grid creates a two-dimensional layout system with explicit rows and columns.

Example
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto;
  gap: 16px;
}
.item { grid-column: 1 / 3; /* span 2 columns */ }
15

Compare transition and animation in CSS.

  • transition — animates a property change between two states (triggered by :hover etc.). Simple, two-state.
  • animation — uses @keyframes to define multi-step animations that can loop, delay, and run automatically.
Example
/* Transition */
.btn { transition: background 0.3s ease; }
.btn:hover { background: blue; }

/* Animation */
@keyframes spin { to { transform: rotate(360deg); } }
.loader { animation: spin 1s linear infinite; }
16

What is the difference between inline, block, and inline-block?

  • block — takes full width, starts on new line, respects width/height/margin.
  • inline — takes only content width, no new line, ignores width/height/top-bottom margin.
  • inline-block — inline flow but respects width/height/margin like a block.
17

Explain the :root pseudo-class.

:root selects the highest-level parent element (the <html> element). It has higher specificity than the html selector. Commonly used to define CSS custom properties (variables) that are globally accessible.

18

What is object-fit and when do you use it?

object-fit controls how an <img> or <video> fills its container. Values: fill (default, stretches), contain (fits inside, letterbox), cover (fills, crops), none (original size), scale-down.

Example
img {
  width: 300px;
  height: 200px;
  object-fit: cover;
}
19

Compare margin and padding.

Padding is the space between the content and the border (inside the element). Margin is the space outside the border (between elements). Margins can collapse (adjacent vertical margins merge into the larger one); padding never collapses.

20

Explain CSS clamp().

clamp(min, preferred, max) sets a value that scales between a minimum and maximum. Commonly used for fluid typography that responds to viewport size without media queries.

Example
h1 { font-size: clamp(1.2rem, 4vw, 2.5rem); }
21

What is a CSS reset vs normalize.css?

  • CSS Reset — removes all browser default styles, giving a blank slate. Can be too aggressive.
  • Normalize.css — preserves useful browser defaults while fixing inconsistencies across browsers. More practical approach.
22

Explain the difference between :nth-child and :nth-of-type.

:nth-child(n) selects the nth child of its parent regardless of element type. :nth-of-type(n) selects the nth sibling of the same element type. They differ when mixed element types are present as siblings.

23

What is the will-change property?

will-change hints to the browser that an element will be animated, allowing it to optimize rendering in advance (e.g., promote to a GPU compositing layer). Use sparingly — overuse wastes memory and can degrade performance.

Example
.animated { will-change: transform, opacity; }
24

Explain CSS containment (contain property).

The contain property tells the browser that an element and its contents are independent from the rest of the document tree. This allows the browser to optimize rendering by limiting layout, paint, and style recalculations to the contained element.

Example
.card { contain: layout paint; }
25

Compare min-width, max-width, and width.

  • width — sets a fixed width.
  • min-width — element cannot be narrower than this value; overrides width if viewport is smaller.
  • max-width — element cannot be wider than this value; commonly used with width: 100% for responsive containers.
Example
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}
26

What is the difference between opacity and rgba() for transparency?

  • opacity — applies transparency to the entire element including its children and text.
  • rgba() / hsla() — applies transparency only to the specific property (background, border, etc.); children are not affected.
Example
/* opacity affects everything including children */
.overlay { opacity: 0.5; }

/* rgba affects only the background */
.overlay { background: rgba(0, 0, 0, 0.5); }
27

What are CSS combinators?

  • Descendant (space) — selects all descendants: div p
  • Child (>) — selects direct children only: div > p
  • Adjacent sibling (+) — selects immediately following sibling: h2 + p
  • General sibling (~) — selects all following siblings: h2 ~ p
Example
div > p { color: red; }   /* direct children only */
h2 + p { margin-top: 0; } /* first p after h2 */
28

Compare absolute and relative units in CSS.

  • Absolute units — fixed size regardless of context: px, pt, cm, mm, in.
  • Relative units — scale based on context: em, rem, %, vw, vh, vmin, vmax, ch.
  • Prefer relative units for responsive, accessible designs.
29

Explain CSS Grid auto-placement.

CSS Grid auto-placement automatically places items into grid cells when no explicit placement is defined. Controlled by grid-auto-flow (row, column, dense). grid-auto-rows and grid-auto-columns define the size of implicitly created tracks.

Example
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 200px;
  grid-auto-flow: dense;
}
30

What is the difference between flex-grow, flex-shrink, and flex-basis?

  • flex-basis — the initial size of a flex item before free space is distributed.
  • flex-grow — how much a flex item grows relative to others when there is extra space.
  • flex-shrink — how much a flex item shrinks relative to others when there is not enough space.
  • flex: 1 is shorthand for flex-grow:1, flex-shrink:1, flex-basis:0%.
Example
.item { flex: 1 1 200px; } /* grow, shrink, basis */
31

Explain the CSS :is() and :where() pseudo-class.

  • :is() — matches any element in the selector list; takes the specificity of its most specific argument.
  • :where() — same as :is() but always has zero specificity; useful for low-specificity base styles.
Example
/* Instead of h1 a, h2 a, h3 a */
:is(h1, h2, h3) a { color: blue; }

/* Zero specificity reset */
:where(h1, h2, h3) { margin: 0; }
32

Compare transform and position for animations.

transform (translate, scale, rotate) is GPU-accelerated and does not trigger layout reflow — it is the preferred way to animate movement. Changing position (top, left) triggers layout recalculation on every frame, which is much more expensive.

Example
/* Preferred — GPU accelerated */
.box { transition: transform 0.3s; }
.box:hover { transform: translateX(20px); }

/* Avoid for animation */
.box:hover { left: 20px; }
33

What is CSS subgrid?

Subgrid allows a grid item to participate in the parent grid's tl-row or column tracks. Without subgrid, a nested grid creates its own independent tracks. With subgrid, nested items align to the parent grid lines.

Example
.parent {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.child {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: subgrid; /* inherits parent columns */
}
34

Explain the difference between :focus and :focus-visible.

:focus applies styles whenever an element receives focus (mouse click or keyboard). :focus-visible applies styles only when the element is focused via keyboard navigation. This allows you to show focus rings for keyboard users without showing them on mouse clicks.

Example
button:focus-visible {
  outline: 2px solid #38bdf8;
  outline-offset: 2px;
}
35

What is CSS logical properties?

Logical properties use writing-mode-relative directions (inline/block) instead of physical directions (left/right/top/bottom). This makes layouts work correctly for RTL languages and vertical writing modes.

Example
/* Physical */
.box { margin-left: 16px; padding-top: 8px; }

/* Logical equivalents */
.box { margin-inline-start: 16px; padding-block-start: 8px; }
36

Explain the CSS aspect-ratio property.

aspect-ratio sets a preferred aspect ratio for an element. The browser maintains this ratio as the element resizes. Replaces the old padding-top hack for responsive embeds.

Example
.video-wrapper {
  aspect-ratio: 16 / 9;
  width: 100%;
}

.avatar {
  aspect-ratio: 1; /* square */
  width: 80px;
}
37

Compare CSS Grid fr unit and percentage.

fr (fractional unit) distributes available space after fixed-size tracks and gaps are accounted for. Percentage is calculated from the tl-container width and does not account for gaps. fr is more flexible and avoids overflow caused by gaps.

Example
/* fr accounts for gap automatically */
.grid { grid-template-columns: repeat(3, 1fr); gap: 16px; }

/* % causes overflow with gap */
.grid { grid-template-columns: repeat(3, 33.33%); gap: 16px; }
38

What is CSS scroll-behavior and scroll-snap?

  • scroll-behavior: smooth — enables smooth scrolling for anchor links and JavaScript scroll methods.
  • scroll-snap — snaps the scroll position to defined points. scroll-snap-type on the container, scroll-snap-align on items.
Example
html { scroll-behavior: smooth; }

.carousel {
  scroll-snap-type: x mandatory;
  overflow-x: scroll;
}
.slide { scroll-snap-align: start; }
39

Explain the CSS gap property.

gap (formerly grid-gap) sets the spacing between rows and columns in Grid and Flexbox layouts. It is shorthand for row-gap and column-gap. Unlike margins, gap only applies between items, not on the outer edges.

Example
.flex { display: flex; gap: 16px; }
.grid { display: grid; gap: 24px 16px; /* row-gap column-gap */ }
40

What is CSS @layer?

@layer (Cascade Layers) allows you to organize CSS into named layers with explicit priority order. Styles in later-declared layers win over earlier ones regardless of specificity. Useful for managing third-party CSS and design systems.

Example
@layer reset, base, components, utilities;

@layer base {
  h1 { font-size: 2rem; }
}
@layer utilities {
  .text-lg { font-size: 1.25rem; } /* wins over base */
}
41

Explain the difference between CSS Grid place-items and place-content.

  • place-items — shorthand for align-items and justify-items; aligns grid items within their cells.
  • place-content — shorthand for align-content and justify-content; aligns the entire grid within the container.
  • place-self — overrides place-items for a single item.
Example
.grid {
  display: grid;
  place-items: center; /* center items in cells */
  place-content: center; /* center grid in tl-container */
}
42

What is CSS counter() and how is it used?

CSS counters allow automatic numbering of elements using counter-reset, counter-increment, and counter() or counters() functions. Useful for custom ordered lists, section numbering, and tl-table of contents.

Example
ol {
  counter-reset: item;
  list-style: none;
}
li::before {
  counter-increment: item;
  content: counter(item) ". ";
  font-weight: bold;
}
43

Compare overflow: hidden, scroll, and auto.

  • hidden — clips content that overflows; no scrollbar shown.
  • scroll — always shows scrollbars even if content fits.
  • auto — shows scrollbars only when content overflows (recommended).
  • overflow: hidden on a parent also creates a new block formatting context, which clears floats.
44

Explain the CSS :has() pseudo-class.

:has() is a relational pseudo-class that selects a parent based on its children — the first true "parent selector" in CSS. It can also select elements based on following siblings.

Example
/* tl-card with an image gets different padding */
.card:has(img) { padding: 0; }

/* Label before a required input */
label:has(+ input:required)::after { content: " *"; color: red; }
45

What is the difference between CSS Grid minmax() and clamp()?

  • minmax(min, max) — used in grid-template-columns/rows to define a size range for a track.
  • clamp(min, preferred, max) — used for any CSS length value to create fluid sizing.
Example
.grid {
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
h1 { font-size: clamp(1.5rem, 5vw, 3rem); }
46

Explain CSS stacking context.

A stacking context is a three-dimensional conceptual model where elements are stacked along the z-axis. A new stacking context is created by: position + z-index, opacity < 1, transform, filter, will-change, isolation: isolate. Elements inside a stacking context are painted together and z-index only compares within the same context.

Example
.modal-wrapper {
  isolation: isolate; /* creates new stacking context */
}
47

Compare CSS Grid auto-fill and auto-fit.

  • auto-fill — creates as many tracks as possible to fill the container, even if some are empty.
  • auto-fit — same as auto-fill but collapses empty tracks to zero width, allowing items to stretch to fill the space.
Example
/* auto-fit: items stretch to fill */
.grid { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

/* auto-fill: empty columns remain */
.grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
48

What is CSS color-scheme?

color-scheme tells the browser which color schemes an element supports. Setting it to light dark allows the browser to automatically apply appropriate system UI colors (scrollbars, form controls, backgrounds) for the user's preferred color scheme.

Example
:root { color-scheme: light dark; }

/* Or per-element */
.dark-section { color-scheme: dark; }
49

Explain the CSS content-visibility property.

content-visibility: auto tells the browser to skip rendering of off-screen elements until they are near the viewport. This can dramatically improve initial page load performance for long pages by reducing the rendering work.

Example
.article-section {
  content-visibility: auto;
  contain-intrinsic-size: 0 500px; /* estimated height to prevent layout shift */
}
50

What is the difference between CSS Grid and Flexbox for centering?

Both can center content, but Grid is simpler for two-dimensional centering.

Example
/* Flexbox centering */
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Grid centering (simpler) */
.grid-center {
  display: grid;
  place-items: center;
}
Role-Based Preparation

Prepare across the full job, not one topic at a time.

Use curated frontend, Java, Python, or cloud and DevOps packs with 200 questions, model answers, code examples, and a seven-day plan.

200 curated Q&As

Combine four related topic areas into one focused role-preparation path.

Examples and trade-offs

Practise explaining code, design decisions, limitations, and failure modes.

Seven-day plan

Move from baseline review to a timed mock interview and final checklist.

Next Step
Use CSS interview prep to move into practice and application.

After reviewing interview answers, reinforce the topic with a relevant guide, hands-on practice, or a stronger resume story.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.