Layout Challenges
Layout Challenges
Holy Grail Layout
/* Flexbox */
.layout {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 0 0 200px;
}
.content {
flex: 1;
}
/* Grid */
.layout {
display: grid;
grid-template-columns: 200px 1fr 200px;
min-height: 100vh;
}
Sticky Footer
/* Flexbox */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
}
/* Grid */
body {
display: grid;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
/* Modern */
body {
display: flex;
flex-direction: column;
min-height: 100dvh;
}
Equal Height Columns
/* Flexbox */
.container {
display: flex;
}
.column {
flex: 1;
}
/* Grid */
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Responsive Sidebar
.layout {
display: grid;
grid-template-columns: 250px 1fr;
min-height: 100vh;
}
@media (max-width: 768px) {
.layout {
grid-template-columns: 1fr;
}
.sidebar {
display: none;
}
}
Aspect Ratio Box
/* Modern */
.box {
aspect-ratio: 16 / 9;
}
/* Fallback */
.box {
width: 100%;
padding-top: 56.25%; /* 9/16 * 100 */
position: relative;
}
.box-content {
position: absolute;
inset: 0;
}
Centering Techniques
Centering Techniques
Flexbox (Recommended)
.container {
display: flex;
justify-content: center;
align-items: center;
}
Grid
.container {
display: grid;
place-items: center;
}
Absolute + Transform
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Margin Auto (Flex/Grid)
.parent {
display: flex;
}
.child {
margin: auto;
}
Text Centering
.parent {
text-align: center;
}
.child {
display: inline-block;
}
Vertical Centering (Single Line)
.parent {
line-height: 200px; /* Match height */
}
.child {
display: inline-block;
vertical-align: middle;
}
},
{
"id": "css-optimization",
"title": "CSS Optimization",
"content": "## CSS Optimization
Reduce Specificity
/* Bad */
#header .nav ul li a { }
/* Good */
.nav-link { }
Use Shorthand
/* Bad */
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
/* Good */
margin: 10px 20px;
Avoid Redundancy
/* Bad */
.button { padding: 10px; }
.button-primary { padding: 10px; background: blue; }
/* Good */
.button { padding: 10px; }
.button-primary { background: blue; }
Use CSS Custom Properties
:root {
--primary: #3b82f6;
--spacing: 1rem;
}
.button {
background: var(--primary);
padding: var(--spacing);
}
Critical CSS
<style>
/* Above-the-fold styles */
.header { }
.hero { }
</style>
<link rel="preload" href="styles.css" as="style">
Minimize Layout Thrashing
/* Bad: Triggers layout */
.width { width: 100px; }
.height { height: 100px; }
.top { top: 0; }
/* Good: GPU-accelerated */
.transform { transform: translateX(100px); }
.opacity { opacity: 0.5; }
Container Queries vs Media Queries
/* Container queries are better for components */
.card-container {
container-type: inline-size;
}
@container (min-width: 400px) {
.card { display: flex; }
}
Practice Problems
Create a reusable React component implementing Common CSS Interview Questions. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Common CSS Interview Questions using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Common CSS Interview Questions for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the simplest way to center an element with Flexbox?
2. What is the simplest way to center with Grid?
3. How do you create a sticky footer with Flexbox?
4. What is the best practice for CSS specificity?
5. What is the modern way to set aspect ratio?
Flashcards
Question
How do you center with Flexbox?
Click to reveal answer
Answer
display: flex; justify-content: center; align-items: center;
Question
How do you create a sticky footer?
Click to reveal answer
Answer
Flexbox: min-height: 100vh on body, flex: 1 on main. Grid: grid-template-rows: auto 1fr auto.
Question
How do you prevent margin collapse?
Click to reveal answer
Answer
Use padding instead of margin, or use flex/grid containers which create new formatting contexts.
Question
What is the difference between position: sticky and fixed?
Click to reveal answer
Answer
sticky sticks within its parent container. fixed sticks to the viewport.
Question
How do you optimize CSS performance?
Click to reveal answer
Answer
Use GPU-accelerated properties (transform, opacity), reduce specificity, use shorthand, and minimize layout thrashing.
Revision Notes
Key Takeaways
- 1.Flexbox and Grid provide the simplest centering solutions
- 2.Sticky footer uses flex: 1 or grid-template-rows: 1fr
- 3.Keep CSS specificity low for maintainability
- 4.Use GPU-accelerated properties for animations
- 5.CSS custom properties enable dynamic theming
Interview Tips
- •Practice centering with multiple techniques
- •Know the sticky footer solutions
- •Be able to explain CSS specificity rules
- •Understand when to use Grid vs Flexbox
- •Know how to optimize CSS performance
Cheat Sheet
CSS Interview Cheat Sheet
Centering Techniques
/* Flexbox */
display: flex; justify-content: center; align-items: center;
/* Grid */
display: grid; place-items: center;
/* Absolute */
position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
Sticky Footer
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
Holy Grail
.layout { display: grid; grid-template-columns: 200px 1fr 200px; }
Aspect Ratio
box { aspect-ratio: 16 / 9; }
Optimization
- Keep specificity low
- Use GPU-accelerated properties
- Use shorthand properties
- Use CSS custom properties