CSSInterview Questionsintermediate

CSS Interview Questions and Answers

25 CSS questions with short answers, working code and layout bugs, split for freshers, experienced developers and the five-year level.

Published
TL;DR – Quick Answer

CSS interviews focus on the box model, specificity, positioning, flexbox, grid and responsive design. Experienced candidates also face stacking contexts, layers, container queries and rendering speed. The strongest answers explain why a layout behaves as it does and how you would debug it.

On This Page

CSS interview questions test whether you can predict what the browser will do. An interviewer may show a broken layout and ask for the cause. You need the rules behind the box model, the cascade, positioning, flexbox and grid.

Below are 25 CSS interview questions and answers. Read the bold part aloud, then try the code. If layouts still feel hard, begin with the CSS tutorial.

How CSS interview questions change with experience

Level Main focus Typical task
Fresher Selectors, box model, layout Centre a card
2 to 4 years Responsive pages, debugging Fix a broken header
5 years and above Systems, speed, large code Plan themes or a style migration

The same topic returns at each level. A fresher explains specificity. A five-year developer explains how a team avoids fighting it.

CSS interview questions and answers for freshers

Start here for CSS interview questions for freshers. Draw boxes on paper while you answer.

Q1. What is CSS, and how can you add it?

30-second answer: CSS controls how HTML looks. You can write it inline on an element, inside a style tag or in an external file. External files are best for most sites because pages can share and cache them.

Inline styles are hard to reuse and hard to override. A small style block in the head can make the first screen appear sooner.

Q2. What is the box model?

30-second answer: Every box has content, padding, a border and a margin. Padding is inside the border. Margin is outside it and creates space from other boxes.

.card {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 2px solid #ccc;
}

With border-box, this card is 300px wide in total. With the default content-box, it would be 344px: 300 plus 40 for padding and 4 for borders.

Q3. How does specificity work?

30-second answer: Specificity is compared as IDs, then classes, then element names. One ID beats any number of classes, and one class beats any number of element names. If scores match, the later rule wins.

Selector ID Class Element
p 0 0 1
.card p 0 1 1
#main .card 1 1 0

Attribute selectors and pseudo-classes such as :hover count like classes. :where() adds zero.

Q4. What is the cascade?

30-second answer: The cascade is how the browser chooses between rules. It checks origin and importance first, then layers, specificity and finally source order.

So specificity is only one step. This is the point many candidates miss. A rule with a low score can still win because it is marked important or sits in a stronger layer.

Q5. What is inheritance?

30-second answer: Some properties pass from parent to child, such as color and font-family. Layout properties such as margin and border do not.

Use inherit to take the parent's value and initial for the property's default. unset inherits only if that property normally inherits.

Q6. How do the position values differ?

30-second answer: static is normal. relative shifts the box but keeps its old space. absolute leaves normal flow. fixed normally follows the viewport. sticky scrolls normally until it reaches a set edge.

An absolute box uses the nearest positioned ancestor. A transformed ancestor can also become its reference.

Common mistake: Writing position: sticky without top, bottom, left or right. It then has no point at which to stick.

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

30-second answer: display: none removes the box and its space. visibility: hidden hides it but keeps its space. opacity: 0 makes it transparent, but it can still receive clicks and focus.

The first two also hide content from screen readers.

Q8. Which units should you use?

30-second answer: Use rem for text and spacing, percentages or fr for flexible layout, and px for small fixed details. em is based on the element's font size, while rem is based on the root font size.

For font-size itself, em uses the parent's size. Nested em font sizes can therefore grow or shrink unexpectedly.

Q9. What is flexbox?

30-second answer: Flexbox arranges items along one main axis. justify-content places them on that axis. align-items places them on the cross axis.

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

The axes swap when flex-direction is column. For more examples, see CSS flexbox.

Q10. When should you use grid instead of flexbox?

30-second answer: Use flexbox for one direction, such as a navigation row. Use grid when rows and columns must line up, such as a dashboard.

They work well together. A grid can place cards, and flexbox can arrange the content of each card.

Q11. What does a media query do?

30-second answer: It applies styles only when a condition is true, such as a minimum screen width or a user's dark theme preference.

.cards { display: grid; gap: 1rem; }

@media (min-width: 700px) {
  .cards { grid-template-columns: 1fr 1fr; }
}

Base rules cover phones here. The larger screen gets an extra column. This is called a mobile-first approach.

Q12. What is the difference between a pseudo-class and a pseudo-element?

30-second answer: A pseudo-class selects a state or position, such as :hover or :first-child. A pseudo-element styles a part of an element, such as ::before.

::before and ::after normally need content. Do not put essential text there because support by assistive tools can vary.

CSS interview questions and answers for experienced developers

Now the interviewer expects you to find the cause. These CSS interview questions and answers for experienced candidates reward a calm debugging method.

Q13. Why is a large z-index not working?

30-second answer: The element is probably inside a stacking context that sits below another one. Its z-index competes only inside that context.

transform, opacity below 1, filter and a positioned element with a z-index can create a context. Find the parent and fix its order instead of adding more nines.

Q14. Why do margins collapse?

30-second answer: Vertical margins of neighbouring blocks can join into one. The larger positive margin is used. A parent's and first child's top margins can also join.

Padding, a border, display: flow-root or a flex parent stops parent and child collapse. Margins of flex and grid items do not collapse.

Q15. What does flex: 1 mean?

30-second answer: It is short for grow 1, shrink 1 and a basis of 0%. The free space is shared from that zero basis, so items tend to be equal.

A long word may still stop an item from shrinking because its automatic minimum size protects content. Set min-width: 0 on that item when needed.

Q16. How do you create responsive cards without media queries?

30-second answer: Use grid with auto-fit and minmax. The browser adds columns when each one has enough space.

.cards {
  display: grid;
  gap: 1rem;
  grid-template-columns:
    repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}

The inner min() prevents overflow on a very narrow screen. With auto-fill, empty columns stay. With auto-fit, they collapse.

Q17. How do CSS variables differ from Sass variables?

30-second answer: CSS custom properties exist in the browser, follow the cascade and can change at runtime. Sass variables are replaced while the CSS is built.

Use custom properties for themes. Sass values can still help with build-time calculations.

Q18. What does the :has selector do?

30-second answer: It selects an element based on something inside or after it. For example, .field:has(input:invalid) styles a field wrapper when its input is invalid.

Keep the search small. A broad :has() on the whole page is harder to read and may cost more to evaluate.

Q19. How do you animate without making a page slow?

30-second answer: Prefer transform and opacity. Changing width, top or margin can make the browser calculate layout on many frames.

Respect prefers-reduced-motion. Use will-change briefly, only after measuring a need, because it can use more memory.

Q20. How do container queries differ from media queries?

30-second answer: Media queries check the page or device. Container queries check a parent box, so a component can adapt wherever it is placed.

.card-wrap { container-type: inline-size; }

@container (min-width: 30rem) {
  .card { display: flex; }
}

This suits a card used in both a wide page and a narrow sidebar.

Interview note: Mention that an element cannot query the size container it creates itself. Put the container on a wrapper.

CSS interview questions for 5 years experience

At five years, talk about decisions that a team can live with.

Q21. How do cascade layers help a large project?

30-second answer: @layer sets a clear order for reset, library, component and utility rules. A normal rule in a later layer beats one in an earlier layer even if its specificity is lower.

@layer reset, vendor, components, utilities;

Unlayered normal rules beat all layered normal rules. Important rules reverse the layer order. Plan this before moving old CSS.

Q22. How would you choose between CSS Modules, utilities and CSS-in-JS?

30-second answer: I would compare team skill, build setup, runtime cost, theme needs and how easily old code can move. CSS Modules give local names, utilities keep values consistent and some CSS-in-JS tools add runtime work.

State a choice for a case. For many small components in an existing React app, Modules can be an easy step.

Q23. How would you build light and dark themes?

30-second answer: I would name values by purpose, such as --surface and --text, then change them at the theme root. I would support the system choice and save a user's override.

Check contrast in both themes and set color-scheme. Do not name a token --white if it becomes black at night.

Q24. A page scrolls sideways. How do you find the cause?

30-second answer: Inspect elements wider than the viewport, then check fixed widths, long text, negative margins, grid minimums and 100vw beside a scrollbar.

Fix the element that overflows. Putting overflow-x: hidden on the body can hide the problem and cut off useful content. A temporary outline on every element makes the wide one easy to see.

Q25. How would you clean up a large CSS file safely?

30-second answer: I would record the current look with screenshots, find duplicate values, move one feature at a time and compare before and after. I would remove a rule only after checking dynamic class names too.

Coverage tools are useful, but a rule unused on one page may be needed on another.

Pro tip: Give a migration order, a check and a rollback plan. That makes a senior answer complete.

Three layout tasks to practise

Reading alone will not prepare you for a live task. Build each of these with plain CSS, then explain your choices aloud.

  1. Header: a logo on the left, links on the right and a menu that wraps on a phone. Use flexbox with justify-content: space-between.
  2. Page shell: a header, sidebar, content area and footer. Use grid, and keep the footer at the bottom when the content is short.
  3. Pricing cards: three cards of equal height whose buttons line up at the bottom. Make each card a column flexbox and give its button margin-top: auto.

After each task, add a very long word and a missing image. Real content is rarely neat. If the layout breaks, find the rule that caused it before you change anything.

Time yourself too. Around 20 minutes per task is a sensible target once you know the rules. If you get stuck, say what you expected and what you see. An interviewer can follow that thinking.

How to prepare in six days

Use two days for the cascade and box model, two for flexbox and grid, one for responsive design and one for bugs. Build without copying code. Then explain the winning rule for each visible style.

Pair these CSS interview questions and answers with HTML interview questions. React candidates can continue with React questions for freshers. For a longer learning path, explore the MERN stack program.

Frequently Asked Questions

Which CSS topics matter most for freshers?
Learn the box model, selectors, specificity, positioning, flexbox and media queries first. Then learn basic grid. Practise by building layouts, since a fresher round may include a small task.
Should I learn flexbox or grid first?
Flexbox is often easier to start with because it handles one direction. Learn grid next because pages often need rows and columns together. Interviews commonly compare them.
Are CSS frameworks asked in interviews?
They may be if you list one on your resume. You still need plain CSS because interviewers may ask what a utility or component does underneath. Know one framework well instead of naming many.
What CSS questions are asked at five years of experience?
Expect style architecture, design tokens, cascade layers, container queries, theming and performance. You may also be asked how you would move an old codebase to a new approach safely.
How can I get better at CSS debugging?
Open browser developer tools, inspect the computed value and check which rule won. Turn declarations off one by one and change one thing at a time. Write down the cause after each bug.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — View the Java Full Stack curriculum

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 3 December 2026 LinkedIn
Chat with us