CSS Interview Questions
CSS Interview Questions
BASIC :
INTERMEDIATE :
18. What are CSS transitions, and how do they differ from animations?
CSS transitions allow changes in CSS properties to occur smoothly over
a specified duration. Transitions require a triggering event (like hover).
CSS animations, defined with @keyframes, allow for more complex
sequences of animations that can loop and do not require a triggering
event.
ADVANCED :
21. How does the CSS Flexbox layout work, and when would you use
it?
Flexbox is a layout model that allows items to align and distribute space
within a container. It is useful for creating flexible and responsive
layouts without using floats or positioning. Use Flexbox for one-
dimensional layouts (either row or column).
22. What are CSS preprocessors, and how do they enhance CSS?
CSS preprocessors like Sass, Less, and Stylus extend CSS with features
like variables, nested rules, mixins, and functions. They compile down to
standard CSS, making it easier to manage and maintain complex
stylesheets.
23. Explain the concept of CSS specificity hierarchy and how it can be
overridden.
Specificity hierarchy determines which CSS rules are applied when
multiple rules match an element. It can be overridden using !important,
inline styles, or by writing more specific selectors.
25. What are CSS combinators, and how are they used?
Combinators describe the relationship between two selectors. Types of
combinators include:
Descendant ( ): div p selects all <p> elements inside a <div>.
Child (>): div > p selects all <p> elements that are direct children of a
<div>.
Adjacent sibling (+): h1 + p selects the first <p> element that is
immediately preceded by an <h1>.
General sibling (~): h1 ~ p selects all <p> elements preceded by an <h1>.
29. Explain how to create a responsive image gallery using CSS Grid.
Define a grid container with responsive columns using grid-template-
columns and auto-fit or auto-fill combined with minmax.
Example:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px;
}
.gallery img {
width: 100%;
height: auto;
}
30. What is the difference between CSS Modules and traditional CSS?
CSS Modules are a way to modularize and scope CSS by generating
unique class names at build time, preventing class name collisions.
Traditional CSS does not provide automatic scoping, which can lead to
global styles affecting unintended elements.