Loading content…
Loading content…
A comprehensive compile of junior, mid, and senior level CSS interview questions and answers covering layout specifications, specificity mathematics, and modern styling mechanisms
(inline, ID, class, element):style="color: red;") -> Weight: (1, 0, 0, 0)#header) -> Weight: (0, 1, 0, 0).card, [type="text"], :hover) -> Weight: (0, 0, 1, 0)div, p, ::before) -> Weight: (0, 0, 0, 1)*) and combinators (+, >, ~) have no specificity weight (0, 0, 0, 0).!important rule overrides all other rules, but is not considered a part of specificity. Avoid using it in production as it makes code hard to maintain./* Method 1: Flexbox (Best for centering simple items inside containers) */
.parent-flex {
display: flex;
justify-content: center;
align-items: center;
}
/* Method 2: Grid (Simplest syntax) */
.parent-grid {
display: grid;
place-items: center;
}
/* Method 3: Absolute Positioning (Best when element needs to float over others) */
.parent-absolute {
position: relative;
}
.child-absolute {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
:): Used to style an element based on its state or position (e.g., :hover, :focus, :active, :first-child, :nth-child(2)). They target elements that already exist in the DOM.::): Used to style a specific part of an element or inject content that does not exist in the DOM (e.g., ::before, ::after, ::first-letter, ::placeholder). They create a virtual "sub-element" inside the targeted tag./* Hover state pseudo-class */
.link:hover { color: red; }
/* Inject decoration before content pseudo-element */
.link::before { content: "🔗 "; }
@media): Check the dimensions of the browser viewport. Use this for global page layouts (e.g., hiding sidebar navigation on mobile screens, setting page max-widths).@container): Check the dimensions of the immediate parent wrapper. Use this for modular, reusable components (e.g. cards, headers) that need to change layout based on where they are placed in a dashboard, without depending on viewport widths.$color: red): Evaluated at compile-time. They compile down to static CSS values. Once built, Sass variables do not exist in the DOM and cannot be modified dynamically.--color: red): Evaluated at runtime in the browser. They inherit according to standard CSS cascading rules.element.style.setProperty('--color', 'blue')) and media queries, making them ideal for run-time adjustments like real-time color customizers and user-selected dark modes.width, height, margin, padding, top, left, display, or reading DOM layout values like offsetWidth.color, background-color, box-shadow, visibility.CSS Transform animations (transform: translate(), scale()) and opacity instead of animating positions. These properties bypass the Layout and Paint stages, moving straight to the GPU Compositor phase for smooth 60fps animations.will-change to inform the browser to optimize for future animations (use sparingly).CSS Interview Preparation Checklist
Marking it complete updates your roadmap progress percentage.