π‘ Part 5: 30 Advanced CSS Interview Questions & Answers
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
opacityless 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-indexonly works within the same stacking context.A new stacking context isolates
z-indexvalues 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: hiddenhides the element but keeps its space.display: noneremoves 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
transformandopacityinstead oftop,left.Use
will-changeto 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: autocenters block elements.margin: 0removes 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βscolorproperty.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.
π Stay Connected
If you found this article helpful and want to receive more such beginner-friendly and industry-relevant Interviews related notes, tutorials, and project ideas β π© Subscribe to our newsletter by entering your email below.
And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment, π₯ Donβt forget to subscribe to my YouTube channel β Knowledge Factory 22 β for regular content on tech concepts, career tips, and coding insights!
Stay curious. Keep building. π

