CSS Interview Cheat Sheet
Complete CSS reference covering selectors, layouts, CSS3+ features, and common interview questions.
Overview
Comprehensive CSS interview preparation guide covering selectors & specificity, modern layout systems (Flexbox, Grid), CSS3+ features, responsive design, and common interview scenarios with visual examples.
Reference Content
š Core Concepts
CSS Box Model
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā MARGIN ā
āāāāāāāāāāā¬āāāāāāāāāāāāāāāāāā¬āāāāāāāāāā¤
ā ā BORDER ā ā
ā MARGIN āāāāāāāāāāāāāāāāāā⤠MARGIN ā
ā ā PADDING ā ā
ā āāāāāāāāāāāāāāāāāā⤠ā
ā ā CONTENT ā ā
ā ā ā ā
ā āāāāāāāāāāāāāāāāāāā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāSpecificity Hierarchy
Interview Context: This is CSS fundamentals - you MUST know this cold. How to Calculate: Count each selector type, compare left-to-right.
Inline styles: 1,0,0,0 (highest - style="color: red")
IDs: 0,1,0,0 (#header, #nav)
Classes/Attrs: 0,0,1,0 (.button, [type="text"], :hover)
Elements: 0,0,0,1 (div, p, h1)
Key Rules:
- Higher specificity always wins
- Equal specificity? Last rule wins (cascade)
!importanttrumps everything (avoid!)- Inline styles beat everything
div.header { color: blue; } / 0,0,1,1 /
#main div { color: red; } / 0,1,0,1 /
.header { color: green; } / 0,0,1,0 /
Answer: Red wins (ID beats class+element)Layout Systems
/ Container properties /
.flex-container {
display: flex;
flex-direction: row | column | row-reverse | column-reverse;
flex-wrap: nowrap | wrap | wrap-reverse;
justify-content: flex-start | center | space-between | space-around | space-evenly;
align-items: stretch | flex-start | center | flex-end | baseline;
align-content: stretch | flex-start | center | flex-end | space-between;
gap: 1rem; / Modern gap property /
}/ Item properties /
.flex-item {
flex: 1 0 auto; / grow shrink basis /
align-self: auto | flex-start | center | flex-end | stretch;
order: 1; / Change visual order /
}
Flexbox Layout Patterns
Row Layout (justify-content: space-between):
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā [Item 1] [Item 2] [Item 3] ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāColumn Layout (align-items: center):
āāāāāāāāāāāā
ā [Item 1] ā
ā [Item 2] ā
ā [Item 3] ā
āāāāāāāāāāāā
Flex Wrap:
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā [1] [2] [3] ā
ā [4] [5] ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
CSS3+ Modern Features
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
--font-size-base: 1rem;
--spacing-unit: 8px;
}.component {
color: var(--primary-color);
font-size: calc(var(--font-size-base) * 1.25);
padding: calc(var(--spacing-unit) * 2);
/ Fallback values /
background: var(--undefined-var, #fallback);
}
/ Dynamic theming /
[data-theme="dark"] {
--primary-color: #4dabf7;
--background: #1a1a1a;
}
Responsive Design
/ Mobile-first approach /
.component { / Mobile styles (320px+) / }@media (min-width: 768px) {
.component { / Tablet styles / }
}
@media (min-width: 1024px) {
.component { / Desktop styles / }
}
@media (min-width: 1440px) {
.component { / Large desktop / }
}
/ Modern queries /
@media (orientation: landscape) { / Landscape mode / }
@media (hover: hover) { / Devices with hover capability / }
@media (prefers-color-scheme: dark) { / System dark mode / }
Performance & Optimization
/ Above-the-fold critical styles /
.hero, .navigation, .header {
/ Critical styles here /
}/ Preload important fonts /
/ /
/ Font display strategies /
@font-face {
font-family: 'CustomFont';
src: url('font.woff2');
font-display: swap; / or fallback, optional /
}
Common Interview Questions
Browser Compatibility & Debugging
/ Feature queries /
@supports (display: grid) {
.layout { display: grid; }
}@supports not (display: grid) {
.layout { display: flex; }
}
/ Vendor prefixes (automated by tools) /
.gradient {
background: -webkit-linear-gradient(red, blue);
background: linear-gradient(red, blue);
}
Quick Reference
display: block, inline, flex, grid, noneposition: static, relative, absolute, fixed, stickybox-sizing: content-box, border-boxoverflow: visible, hidden, scroll, autoz-index: stacking context control
š” Quick Tips
- Know the Box Model: Content ā Padding ā Border ā Margin
- Master Flexbox vs Grid: Flexbox for 1D, Grid for 2D layouts
- Understand Specificity: Calculate specificity weights accurately
- Practice Centering: Know multiple centering techniques
- CSS Variables: Show knowledge of modern CSS features
ā ļø Common Gotchas
- Flexbox:
justify-content= main axis,align-items= cross axis - Grid:
grid-template-areasnames must be valid CSS identifiers - Position Sticky: Requires a scroll container and threshold value
- Z-index: Only works on positioned elements