π‘ Part 5: 30 Advanced CSS Interview Questions & Answers

Table of contents
- 1. β What is a stacking context in CSS?
- 2. β What is the difference between z-index and stacking context?
- 3. β How do CSS variables (custom properties) work?
- 4. β How are CSS variables different from SASS/LESS variables?
- 5. β What are attribute selectors in CSS?
- 6. β What is the :not() pseudo-class?
- 7. β What is specificity in CSS?
- 8. β How to calculate the specificity of this selector: #header .nav li a?
- 9. β How can you override !important?
- 10. β What is the use of the :is() pseudo-class?
- 11. β What are pseudo-elements? Give examples.
- 12. β Difference between pseudo-class and pseudo-element?
- 13. β What is contain property in CSS?
- 14. β What is BEM in CSS?
- 15. β What is the difference between visibility: hidden and display: none?
- 16. β Explain the box model in CSS.
- 17. β How does box-sizing affect layout?
- 18. β What is the default position value in CSS?
- 19. β Whatβs the difference between relative and absolute positioning?
- 20. β What is a media query?
- 21. β How do rem and em units differ?
- 22. β What is the difference between min-width and max-width?
- 23. β How to optimize CSS performance?
- 24. β How to make animations smooth?
- 25. β What is the clip-path property?
- 26. β What is object-fit in CSS?
- 27. β What is the difference between auto and 0 in margins?
- 28. β Can pseudo-elements be animated?
- 29. β How does CSS handle dark mode?
- 30. β What is the difference between currentColor and inherit?
Welcome to Part 5 of our frontend interview series. In this article, weβll cover 30 advanced CSS questions that are commonly asked in real interviews. These questions go beyond the basics β focusing on deep concepts like rendering, performance, custom properties, stacking contexts, advanced selectors, and modern techniques used by senior developers.
1. β What is a stacking context in CSS?
Answer:
A stacking context is an element that contains a set of layers, with a specific z-index hierarchy. It helps manage which elements appear on top of each other.
A new stacking context is created when:
A z-index is applied to a positioned element.
An element has
opacity
less than 1.An element has
transform
,filter
,will-change
, orperspective
.
Example:
.parent {
position: relative;
z-index: 1;
opacity: 0.9; /* creates new stacking context */
}
2. β What is the difference between z-index
and stacking context?
Answer:
z-index
only works within the same stacking context.A new stacking context isolates
z-index
values inside it.
So, even if an element has a higher z-index, it can be behind another stacking context with lower z-index but higher in the tree.
3. β How do CSS variables (custom properties) work?
Answer:
CSS variables start with --
and can be reused throughout the CSS. They're scoped to the element they are defined in and cascade like normal properties.
:root {
--main-color: #f62718;
}
button {
color: var(--main-color);
}
4. β How are CSS variables different from SASS/LESS variables?
Answer:
Feature | CSS Variables | SASS/LESS Variables |
Live updates | Yes | No |
Scoped | Yes | No (global only) |
Use in JS | Yes (getComputedStyle ) | No |
CSS variables are dynamic and part of the runtime, unlike preprocessor variables.
5. β What are attribute selectors in CSS?
Answer:
Attribute selectors match HTML elements based on attributes and their values.
Examples:
input[type="text"] { } /* Exact match */
a[href^="https://"] { } /* Starts with */
a[href$=".pdf"] { } /* Ends with */
6. β What is the :not()
pseudo-class?
Answer:
It selects every element that does not match the given selector.
button:not(.primary) {
background: gray;
}
This improves code efficiency by avoiding extra classes or JS logic.
7. β What is specificity in CSS?
Answer:
Specificity determines which rule wins when multiple rules match the same element. It's calculated as:
Inline styles (1000)
ID selectors (100)
Class/selectors/pseudo-classes (10)
Element selectors (1)
8. β How to calculate the specificity of this selector: #header .nav li a
?
Answer:
#header
β 100.nav
β 10li
β 1a
β 1
Total = 112
9. β How can you override !important
?
Answer:
You can override an !important
rule by writing another rule with:
The same property
Higher specificity
Also using
!important
10. β What is the use of the :is()
pseudo-class?
Answer::is()
simplifies complex selectors and reduces repetition.
:is(h1, h2, h3) {
font-weight: bold;
}
11. β What are pseudo-elements? Give examples.
Answer:
Pseudo-elements style specific parts of elements.
Examples:
p::first-line { }
p::before { content: "π "; }
12. β Difference between pseudo-class and pseudo-element?
Answer:
Feature | Pseudo-Class (:hover ) | Pseudo-Element (::before ) |
Affects | Element state | Part of the element |
Syntax | Single colon : | Double colon :: |
13. β What is contain
property in CSS?
Answer:
The contain
property improves performance by letting browsers skip layout, paint, or size calculations for isolated sections.
.card {
contain: layout paint;
}
14. β What is BEM in CSS?
Answer:
BEM = Block Element Modifier
Itβs a naming convention for writing clean and reusable CSS.
<div class="card card--active">
<div class="card__title">Title</div>
</div>
15. β What is the difference between visibility: hidden
and display: none
?
Answer:
visibility: hidden
hides the element but keeps its space.display: none
removes it completely from the layout.
16. β Explain the box model in CSS.
Answer:
Each element is a box with:
Content
Padding
Border
Margin
Understanding it helps with spacing and layout issues.
17. β How does box-sizing
affect layout?
Answer:box-sizing: border-box
includes padding and border in the total width/height, making layouts easier to manage.
18. β What is the default position value in CSS?
Answer:position: static
is the default, meaning the element flows normally in the document layout.
19. β Whatβs the difference between relative
and absolute
positioning?
Answer:
relative
: positions relative to itself.absolute
: positions relative to the nearest positioned ancestor.
20. β What is a media query?
Answer:
Media queries are used to apply styles conditionally based on screen size, resolution, etc.
@media (max-width: 600px) {
body { font-size: 14px; }
}
21. β How do rem
and em
units differ?
Answer:
em
: relative to parent elementrem
: relative to root (html
)
22. β What is the difference between min-width
and max-width
?
Answer:
min-width
: applies style when screen is larger than valuemax-width
: applies style when screen is smaller than value
23. β How to optimize CSS performance?
Answer:
Use fewer selectors and combine rules.
Avoid deeply nested selectors.
Minify and compress CSS.
Use CSS containment.
Remove unused styles.
24. β How to make animations smooth?
Answer:
Use
transform
andopacity
instead oftop
,left
.Use
will-change
to hint the browser.
.card {
will-change: transform;
}
25. β What is the clip-path
property?
Answer:
It creates complex shapes like circles, polygons by clipping elements.
clip-path: circle(50%);
26. β What is object-fit
in CSS?
Answer:
It defines how images/videos fit inside containers.
img {
object-fit: cover;
}
27. β What is the difference between auto
and 0
in margins?
Answer:
margin: auto
centers block elements.margin: 0
removes all margin.
28. β Can pseudo-elements be animated?
Answer:
Yes. Use ::before
or ::after
with transitions/animations.
29. β How does CSS handle dark mode?
Answer:
Use media query:
@media (prefers-color-scheme: dark) {
body {
background: #000;
color: #fff;
}
}
30. β What is the difference between currentColor
and inherit
?
Answer:
currentColor
: uses the value of the elementβscolor
property.inherit
: uses the parentβs value for any property.
π Conclusion:
These advanced CSS questions are commonly asked in real-world interviews, especially for senior frontend roles. Make sure you not only memorize answers but also experiment with code and observe the results in real projects.
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: π In-depth explorations of emerging technologies π‘ Practical tutorials and how-to guides π§Insights on software development best practices πReviews of the latest tools and frameworks π‘ Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letβs connect and grow together! π