The CSS Box Model defines how elements are sized and spaced:
Total size = content + padding + border + margin
In React, I treat components as layout containers or content blocks , and understanding the box model is critical for:
box-sizing: border-boxAvoiding overflow bugs in nested structures.
Example:
* {
box-sizing: border-box;
}
This ensures padding and border don’t unexpectedly affect width/height.
I follow a low-specificity-first principle:
div.btn.primary), which make overrides fragile.Example with CSS Modules:
/* Button.module.css */
.button {
color: white;
}
In React:
<button className={styles.button}>Submit</button>
I also limit use of !important, and avoid inheritance for critical properties like layout, instead opting for explicit declarations .
I align layout responsibilities with component boundaries :
React component tree ↔ CSS layout model:
Example:
<GridLayout>
<Sidebar />
<MainContent />
</GridLayout>
This encourages separation of layout and content logic .
I favor mobile-first CSS with utility tokens and design systems.
Approach:
@media queries or CSS-in-JS media utilsrem, %) for scalabilityExample (Styled Components):
const Card = styled.div`
padding: 1rem;
@media (min-width: 768px) {
padding: 2rem;
}
`;
At scale, I use design tokens and a centralized layout system to maintain consistency.
Key principle: Minimize layout thrashing .
I:
width, top) frequently.transform and opacity for animations—they don’t trigger reflow.In React:
memo, useCallback to avoid triggering costly reflows via DOM updates.I treat each component as a style boundary . CSS scoping prevents leakage and overrides.
CSS Modules :
Great for traditional CSS workflows.
Styled Components / Emotion :
Example:
const Button = styled.button`
background: ${({ primary }) => (primary ? "blue" : "gray")};
`;
For large apps, I often use CSS Modules + utility classes (e.g., Tailwind) for balance.
I design themes using CSS custom properties and a theme context/provider .
CSS:
:root {
--bg-color: white;
}
[data-theme="dark"] {
--bg-color: #121212;
}
React:
<body data-theme={theme}>...</body>
Use a theme toggle to update data-theme, and manage theme state in global context or via localStorage.
Styled Components alternative:
<ThemeProvider theme={lightTheme}>...</ThemeProvider>
Sass brought:
Today, native CSS and CSS-in-JS cover most use cases:
Modern CSS focuses on modularity , interop , and performance . I’ve moved from Sass to:
I divide animations into:
Framer Motion or React Transition Group.@keyframes or transition in CSS.Example (Framer Motion):
<motion.div
animate=
initial=
exit=
/>
I prefer CSS transitions for simple effects (less overhead), and JS-driven animation when interaction/state complexity is high.
Approach:
@tailwind base)Fallbacks:
display: grid;
display: -ms-grid; /* fallback for older IE */
When needed, I polyfill or degrade gracefully, and lean on modern CSS tools for cross-browser safety.