Layout Fundamentals: Flexbox & Grid
- Flexbox — one-dimensional layout (row or column), alignment, distribution of space
- CSS Grid — two-dimensional layout (rows and columns simultaneously), template areas, auto-placement
- Box model —
content-boxvsborder-box, margin collapsing - Positioning —
static,relative,absolute,fixed,stickyand their stacking context implications
Q1.When should you use Flexbox vs CSS Grid? Can you use both together?
justify-content, align-items, flex-grow, flex-shrink, flex-basis
CSS Grid — two-dimensional layout:
• Best for laying out items across rows AND columns simultaneously
• Layout-driven — you define the grid structure, then place items into it
• Great for: page layouts, dashboards, image galleries, any design that requires alignment in both axes
• Key properties: grid-template-columns, grid-template-rows, grid-template-areas, gap
Using both together (common pattern):
• Use Grid for the overall page layout (header, sidebar, main, footer)
• Use Flexbox for components within grid areas (nav items, card content)
• This is the recommended approach — they solve different problems at different scales
Quick decision guide:
• Laying out items in one direction? → Flexbox
• Need alignment in two dimensions? → Grid
• Building a page-level layout? → Grid
• Aligning items within a component? → Flexbox
• Not sure? → Start with Flexbox; switch to Grid if you find yourself fighting alignmentQ2.Explain CSS specificity. How is it calculated, and how do you manage it in large projects?
style="..." (specificity: 1,0,0,0)
2. ID selectors — #header (specificity: 0,1,0,0)
3. Class, attribute, and pseudo-class selectors — .nav, [type="text"], :hover (specificity: 0,0,1,0)
4. Element and pseudo-element selectors — div, ::before (specificity: 0,0,0,1)
Calculation examples:
• div.container p → 0,0,1,2 (1 class + 2 elements)
• #main .nav li a:hover → 0,1,1,2 (1 ID + 1 class + 1 pseudo-class + 2 elements) — wait, that's 0,1,2,2
• !important overrides all specificity (but creates maintenance nightmares)
Managing specificity in large projects:
• BEM methodology — use flat class selectors (.block__element--modifier) to keep specificity consistently low
• CSS Modules / CSS-in-JS — scoped styles eliminate specificity conflicts entirely
• Utility-first (Tailwind) — single-purpose classes avoid the cascade altogether
• CSS Layers (@layer) — the modern solution: define explicit cascade precedence regardless of specificity
Best practice: Keep specificity as low and flat as possible. If you need !important, something is wrong with your architecture.Responsive Design & Modern CSS
- Media queries — breakpoints, mobile-first vs desktop-first approaches
- Fluid design — relative units (
em,rem,vw,vh),clamp(), container queries - Responsive images —
srcset,<picture>,aspect-ratio - Modern CSS features — custom properties (CSS variables),
@container,:has(), nesting
Q3.What is the difference between em, rem, px, vw, and vh units? When should you use each?
<html>) font size (default 16px)
• Use for: typography, spacing, and component sizing — scales uniformly when the user changes their browser's base font size
• Best for accessibility: respects user font-size preferences
em:
• Relative to the parent element's font size
• Compounds when nested (a 1.5em inside a 1.5em = 2.25x the base)
• Use for: padding/margin relative to the element's own font size (e.g., button padding that scales with text)
vw / vh (viewport width / height):
• 1vw = 1% of viewport width; 1vh = 1% of viewport height
• Use for: full-screen sections, fluid typography with clamp()
• Caveat: vh is unreliable on mobile (browser chrome changes viewport height) — use dvh (dynamic viewport height) instead
Modern best practice:
• Use rem for most sizing (accessible, predictable)
• Use em for component-internal spacing that should scale with the component's text size
• Use clamp(min, preferred, max) for fluid typography: font-size: clamp(1rem, 2.5vw, 2rem)
• Use px only for fine details (borders, outlines)Q4.How do CSS animations and transitions differ? When should you use each?
transition: opacity 0.3s ease-in-out
• Only animates between two states (start and end)
• Cannot loop, cannot animate on page load, cannot sequence multiple steps
CSS Animations (@keyframes):
• Define complex, multi-step animations with full control over intermediate states
• Can run automatically on load, loop infinitely, alternate direction, and pause/resume
• More powerful but more verbose
• Use when: you need multi-step animations, looping, or animation on load
Performance guidelines:
• Only animate transform and opacity for 60fps — these are GPU-composited and don't trigger layout or paint
• Animating width, height, top, left triggers layout recalculation (expensive)
• Use will-change sparingly to hint to the browser about upcoming animations
• Prefer translate over top/left for movement animations
When to use JavaScript animation libraries instead:
• Complex choreographed sequences (Framer Motion, GSAP)
• Physics-based animations (react-spring)
• Animations that depend on scroll position (Intersection Observer + CSS classes, or GSAP ScrollTrigger)
• Gesture-driven animations (drag, pinch, swipe)CSS Architecture & CSS-in-JS
- Methodologies — BEM, SMACSS, ITCSS, and utility-first approaches
- CSS-in-JS — styled-components, Emotion, CSS Modules, and their tradeoffs
- Build-time CSS — Tailwind CSS, vanilla-extract, and zero-runtime approaches
- Design tokens — CSS custom properties for theming and consistency
Q5.Compare CSS Modules, styled-components, and Tailwind CSS. When would you choose each?
Frequently Asked Questions
How deeply should frontend developers know CSS?
For frontend roles, CSS mastery is expected. You should confidently handle Flexbox, Grid, responsive design, accessibility (focus styles, screen reader considerations), and at least one CSS architecture approach. For full-stack roles, solid working knowledge (layout, responsive design, basic animations) is sufficient. In both cases, knowing CSS well is a differentiator — many developers underinvest in CSS skills.
Is CSS-in-JS still relevant, or should I use Tailwind?
Both are actively used. CSS-in-JS (styled-components, Emotion) is common in established React codebases and design systems. Tailwind has gained massive adoption for new projects due to its speed and simplicity. The trend is moving toward build-time solutions (Tailwind, vanilla-extract) that avoid runtime CSS generation. Know the tradeoffs of each approach rather than picking a side.
Should I learn CSS preprocessors like Sass in 2025?
Sass knowledge is still useful for maintaining existing projects, but modern CSS has adopted many features that once required preprocessors: custom properties (variables), nesting (CSS Nesting), color functions (oklch), and container queries. For new projects, native CSS with PostCSS or Tailwind is increasingly preferred. Learn Sass basics but invest more time in modern CSS features.
Don't freeze in your next interview
InterviewsUnlocked gives you real-time AI coaching during live interviews — role-tailored answers, follow-up cues, and confidence when you need it most.
Related Resources
JavaScript Interview Questions & Answers for All Levels
Comprehensive JavaScript interview prep covering closures, prototypes, event loop, ES6+ features, async patterns, and real-world coding challenges with expert answers.
Read moreSkills & TechnologiesReact Interview Questions & Answers: Hooks, Patterns, and Performance
Master React interviews with questions covering hooks, component patterns, state management, performance optimization, and Next.js — with detailed expert answers.
Read moreInterview QuestionsTop Frontend Developer Interview Questions & Answers
Ace your frontend developer interview with questions on JavaScript, React, CSS, web performance, accessibility, and system design with expert answers.
Read moreInterview QuestionsTop Software Engineer Interview Questions & Answers
Prepare for your software engineering interview with expert-crafted questions and detailed answers covering data structures, algorithms, system design, and behavioral topics.
Read moreInterview TipsTechnical Interview Preparation: A 4-Week Plan
A structured 4-week technical interview preparation plan covering data structures, algorithms, system design, and mock interviews with daily schedules.
Read more