Technology

CSS Interview Questions and Answers

CSS questions test whether you can build layouts that work on every screen. These CSS interview questions cover selectors, the box model and specificity for freshers, then flexbox, grid, positioning, responsive design, animations, architecture and modern CSS features for experienced frontend developers.

30 questions · Updated

Reading answers is not the same as saying them.Practise CSS questions out loud and get a score, what you missed and a model answer for each one.
Practise free with AI
CSS Interview Questions with answers. Examples: What is CSS specificity? What is the difference between display: none and visibility: hidden? What are the position values in CSS? When do you use flexbox and when do you use grid?
CSS Interview Questions: 30 questions with model answers on this page.

Basic CSS interview questions

Fundamentals, definitions and simple scenarios. Good for freshers and warm-ups.

1. What is CSS specificity?

Answer flow · 4 steps
  1. Specificity decides which rule wins when several target the same element.

  2. Inline styles beat IDs, IDs beat classes, attribute selectors and pseudo-classes, and those beat element selectors and pseudo-elements.

  3. If specificity is equal, the later rule wins. !important overrides normal rules and should be avoided.

  4. Keeping selectors low-specificity, such as single classes, makes CSS easier to maintain.

2. What is the difference between display: none and visibility: hidden?

display: none removes the element from the layout entirely, so it takes no space and is hidden from screen readers. visibility: hidden hides it but keeps its space in the layout. opacity: 0 makes it invisible but still clickable and accessible. For hiding content visually while keeping it for screen readers, a visually-hidden utility class is used.

3. What are the position values in CSS?

static is the default, in normal flow. relative stays in flow but can be offset, and becomes the reference for absolutely positioned children. absolute is removed from flow and positioned relative to the nearest positioned ancestor. fixed is positioned relative to the viewport and stays on scroll. sticky acts as relative until a scroll threshold, then sticks like fixed within its container.

4. What is the CSS box model, and what does box-sizing do?

Answer flow · 3 steps
  1. Every element is a box of content, padding, border and margin.

  2. With the default box-sizing: content-box, width and height apply only to the content, so padding and border add to the total size.

  3. With box-sizing: border-box, width includes padding and border, which makes sizing predictable, so most projects apply it to all elements.

5. What are the three ways to add CSS to a page?

Answer flow · 3 steps
  1. Inline styles in a style attribute, internal styles in a <style> tag in the head, and external stylesheets linked with <link rel="stylesheet">.

  2. External files are preferred because they can be cached, reused across pages and kept separate from content.

  3. Inline styles have very high specificity and are hard to maintain, though frameworks sometimes use them for dynamic values.

6. What are the main types of CSS selectors?

Answer flow · 3 steps
  1. Type selectors (p), class (.card), ID (#header), attribute ([type="email"]), the universal selector (*), and combinators: descendant (nav a), child (ul > li), adjacent sibling (h2 + p) and general sibling (h2 ~ p).

  2. Pseudo-classes such as :hover and pseudo-elements such as ::before complete the set.

  3. Selectors can be grouped with commas.

7. What is margin collapsing?

Answer flow · 3 steps
  1. When the top and bottom margins of block elements touch vertically, they combine into a single margin equal to the larger of the two, instead of adding up.

  2. It also happens between a parent and its first or last child when nothing separates them.

  3. It does not happen in flex or grid layouts, or with padding, borders or display: flow-root on the parent.

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

Answer flow · 3 steps
  1. Block elements start on a new line and take the full width, and accept width, height and vertical margins.

  2. Inline elements flow within text, take only the width of their content and ignore width, height and vertical margins.

  3. Inline-block elements flow inline like text but accept width, height and all margins and padding, which suits buttons and badges.

9. Which colour formats can you use in CSS?

Named colours (red), hex (#1a73e8, with optional alpha #1a73e880), rgb() and hsl() with optional alpha, and modern spaces such as oklch(), which gives more even, predictable lightness and access to wider colour gamuts. HSL and OKLCH make it easy to create lighter and darker shades of a brand colour, and color-mix() blends colours directly in CSS.

10. What are floats, and are they still used?

Answer flow · 3 steps
  1. float moves an element to the left or right so text wraps around it, which was originally meant for images in articles.

  2. Before flexbox and grid, floats were used for whole page layouts, needing clearfix tricks to stop parents collapsing.

  3. Today floats should only be used for wrapping text around images; flexbox and grid handle layout.

11. What is the difference between pseudo-classes and pseudo-elements?

Pseudo-classes, with one colon, select an element in a state or position: :hover, :focus, :checked, :first-child, :nth-child(odd). Pseudo-elements, with two colons, style part of an element or add generated content: ::before, ::after, ::placeholder, ::marker and ::first-line. ::before and ::after need a content property to appear.

Intermediate CSS interview questions

Applied problems, trade-offs and questions about your own projects.

12. When do you use flexbox and when do you use grid?

Answer flow · 3 steps
  1. Flexbox is one-dimensional: it lays items out in a row or column and is ideal for components such as navigation bars, buttons with icons, and centring.

  2. Grid is two-dimensional, controlling rows and columns together, and is ideal for page layouts, card grids and complex alignments.

  3. They work well together, for example a grid page layout with flex components inside.

13. How do you centre an element horizontally and vertically?

The simplest modern ways are display: flex with justify-content: center and align-items: center on the parent, or display: grid with place-items: center. For an absolutely positioned element, use top: 50%, left: 50% and transform: translate(-50%, -50%), or inset: 0 with margin: auto and a set size.

14. What are the differences between px, em, rem, %, vw and vh?

px is a fixed unit. em is relative to the element's own font size (or the parent's for font-size), so it compounds when nested. rem is relative to the root font size, which makes it predictable and respects user font settings, so I use rem for font sizes and spacing. % is relative to the parent. vw and vh are 1% of the viewport width and height; dvh handles mobile browser bars better.

15. How do you build a responsive layout?

I design mobile-first with fluid layouts using flexbox and grid, relative units and max-width, then add min-width media queries where the design breaks, not for specific devices. Images use max-width: 100% and srcset. clamp() creates fluid typography, grid with repeat(auto-fit, minmax(250px, 1fr)) creates responsive cards without media queries, and container queries style components based on their container size.

16. What are CSS custom properties (variables)?

Answer flow · 3 steps
  1. Custom properties store values for reuse, declared like --brand: #0a7 and used with var(--brand, fallback).

  2. Unlike Sass variables, they are live in the browser: they cascade and inherit, can be changed inside media queries or by a theme class, and can be read and updated by JavaScript.

  3. They are the basis of design tokens and dark mode themes.

17. What is the difference between transitions and animations?

Answer flow · 3 steps
  1. A transition animates a property smoothly when its value changes, for example on hover, between a start and end state, with transition: transform 200ms ease.

  2. An animation uses @keyframes to define several steps and can run on its own, loop, alternate direction and be paused.

  3. Transitions suit simple state changes; keyframe animations suit loaders and more complex motion.

18. How do you truncate text with an ellipsis?

Answer flow · 3 steps
  1. For one line, combine white-space: nowrap, overflow: hidden and text-overflow: ellipsis on an element with a set width.

  2. For several lines, use display: -webkit-box with -webkit-line-clamp: 3, -webkit-box-orient: vertical and overflow: hidden, or the standard line-clamp property where supported.

  3. Make sure the full text is still available, for example in a title or on the detail page.

19. How do grid template areas and fr units work?

Answer flow · 3 steps
  1. grid-template-areas lets you name regions of a grid and draw the layout as text, such as "header header" "sidebar main", then place elements with grid-area: header.

  2. The fr unit shares out the free space, so grid-template-columns: 1fr 3fr gives the second column three times the space.

  3. Changing the areas inside a media query rearranges the whole layout simply.

20. What does object-fit do?

object-fit controls how an image or video fills its box when their proportions differ. cover fills the box completely and crops the excess, contain shows the whole image with empty space, fill stretches it, and none keeps its original size. object-position chooses which part stays visible. It is essential for consistent card images and avatars.

21. Why use a preprocessor such as Sass?

Sass adds variables, nesting, mixins, functions, loops and partials that compile to plain CSS, which made large stylesheets easier to organise. Many of these features now exist in CSS itself, including custom properties, nesting, calc() and cascade layers, so new projects often need Sass less, but it remains common in existing codebases and design systems.

22. What do calc(), min(), max() and clamp() do?

calc() mixes units in calculations, such as width: calc(100% - 2rem). min() and max() pick the smaller or larger of several values, for example width: min(100%, 60ch). clamp(min, preferred, max) keeps a value between limits, so font-size: clamp(1rem, 2.5vw, 2rem) gives fluid typography that never becomes too small or too large, without media queries.

High level CSS interview questions

System design, deep internals, leadership and tough follow-ups.

23. What is a stacking context, and why might z-index not work?

Answer flow · 3 steps
  1. A stacking context is a group of elements stacked together on the z-axis. z-index only compares elements within the same stacking context.

  2. New contexts are created by positioned elements with a z-index, opacity below 1, transform, filter, isolation: isolate and some other properties.

  3. If a parent has created a context with a low z-index, a child with z-index 9999 still cannot appear above elements outside that parent.

24. How do you make CSS animations perform well?

Answer flow · 3 steps
  1. Animate only transform and opacity where possible, because they can run on the compositor without triggering layout or paint.

  2. Avoid animating width, height, top or left, which cause layout recalculation.

  3. Use will-change sparingly for elements about to animate, prefer CSS transitions and animations over JavaScript for simple effects, and respect prefers-reduced-motion for users who are sensitive to motion.

25. How do you organise CSS in a large project?

Answer flow · 3 steps
  1. Options include naming methods like BEM, CSS Modules for locally scoped class names, utility-first frameworks like Tailwind CSS, and CSS-in-JS.

  2. I combine design tokens as CSS custom properties for colours, spacing and typography, a small base layer, component-scoped styles, and cascade layers (@layer) to control order.

  3. The goal is predictable styles without specificity wars or dead code.

26. What are container queries?

Answer flow · 3 steps
  1. Container queries style an element based on the size of its container rather than the whole viewport.

  2. You mark a parent with container-type: inline-size, then write @container (min-width: 400px) { ... } rules for its children.

  3. A card component can then switch to a horizontal layout wherever it has enough room, in a sidebar or a main column, which makes components truly reusable.

27. What is the difference between reflow, repaint and compositing?

Answer flow · 4 steps
  1. Changing geometry, such as width, height, position or font size, triggers layout (reflow), where the browser recalculates positions, which is the most expensive step.

  2. Changing colours or backgrounds triggers paint.

  3. Changing transform or opacity can often be handled by the compositor on the GPU without layout or paint.

  4. Reading layout values such as offsetHeight right after writing styles forces layout and causes layout thrashing.

28. How do you implement dark mode with CSS?

Define colours as custom properties on :root, then redefine them inside @media (prefers-color-scheme: dark) to follow the system setting. To let users override it, set a data-theme attribute on the html element with JavaScript, save the choice in localStorage, and apply it before the first paint to avoid a flash. color-scheme: light dark makes form controls and scrollbars match, and light-dark() simplifies paired values.

29. What are logical properties?

Logical properties describe direction relative to the writing mode instead of the screen: margin-inline-start instead of margin-left, padding-block instead of padding-top and bottom, inset-inline-end instead of right, and inline-size instead of width. Layouts then flip correctly for right-to-left languages such as Arabic and Urdu without separate stylesheets.

30. What do :is(), :where() and :has() do?

:is() matches any selector in its list, shortening long selectors, and takes the specificity of its most specific argument. :where() works the same but always has zero specificity, which is ideal for easy-to-override base styles. :has() is a parent selector: card:has(img) styles cards that contain an image, and form:has(:invalid) styles a form with invalid fields, which used to require JavaScript.

Ready to test yourself?Pick your topics and level, answer by voice or text, and get instant feedback. Free.
Start a mock interview

Preparing for a frontend developer interview?

See all Frontend Developer interview questions, covering HTML, CSS, JavaScript, TypeScript, React and more.

Related skill interview questions

More technology interview questions

HR round, company and bank interview questions

Bank and government exams

Interview questions for other fields