# CSS & Layout Best Practices This document covers CSS patterns, layout strategies, and cross-environment compatibility considerations. ## Flexbox Patterns ### Use items-stretch on Main Flex Containers For full-height layouts where children should fill the available space: ```typescript // Good: items-stretch (default) allows children to fill height
{/* Main content fills full height */}
``` ```typescript // Bad: items-center prevents children from filling container height
{/* Main content only as tall as its content */}
``` ### Nested Flex Containers ```typescript
{/* Header - fixed height */}
{/* Main area - fills remaining space */}
{/* Sidebar - fixed width, full height */} {/* Content - fills remaining width */}
...
``` ### min-h-0 for Overflow Control When using flex containers with scrollable children: ```typescript // Without min-h-0, content may overflow
{/* This might overflow if content is tall */}
// With min-h-0, overflow is properly contained
{/* Content scrolls within container */}
``` ## Parent-Child Styling Pattern ### Parent Provides External Styles The parent component controls: - Positioning (absolute, relative, grid placement) - External spacing (margin, gap) - Size constraints (width, max-width) ```typescript // Parent component
{/* Parent sets grid span */}
``` ### Child Provides Internal Layout The child component controls: - Internal padding - Internal layout (flex, grid) - Background, borders, shadows - Typography ```typescript // Child component export function Card({ className, children }: CardProps) { return (
{children}
); } ``` ### Complete Example ```typescript // Page layout (parent) export function DashboardPage() { return (
{/* Parent controls: grid position, external spacing */}
); } // Card component (child) export function StatsCard({ className }: { className?: string }) { return (
{/* Internal layout */}
); } ``` ## Cross-Environment Testing ### Dev Mode (Turbopack) vs Production (Webpack) CSS may behave differently between development and production builds: ```bash # Test in development (Turbopack) pnpm dev # Test in production (Webpack) pnpm build && pnpm start ``` ### Common Differences 1. **CSS Order**: Tailwind classes may be applied in different orders 2. **Purging**: Unused classes removed in production 3. **Minification**: Class names optimized ### Testing Checklist - [ ] Run `pnpm dev` and test all features - [ ] Run `pnpm build && pnpm start` and test again - [ ] Check for visual differences - [ ] Verify responsive breakpoints work - [ ] Test animations and transitions ## Mobile Touch Optimization ### Disable Tap Highlight Prevent the default blue/gray highlight on mobile tap: ```typescript // Using Tailwind // Using inline styles (when needed) // Global reset in CSS @layer base { button, a, [role="button"] { -webkit-tap-highlight-color: transparent; } } ``` ### Touch-Friendly Sizing ```typescript // Minimum touch target: 44x44px // For lists ``` ### Prevent Pull-to-Refresh When implementing custom scroll behaviors: ```typescript
{/* Scrollable content */}
``` ## Responsive Design Patterns ### Mobile-First Approach ```typescript // Start with mobile styles, add breakpoints for larger screens

Title

``` ### Container Queries (Tailwind v4) ```typescript // Container-based responsive styles
{/* Content */}
``` ### Hiding/Showing Elements ```typescript // Hide on mobile, show on desktop
Desktop only content
// Show on mobile, hide on desktop
Mobile only content
``` ## Z-Index Management ### Establish a Scale ```css /* In your CSS or Tailwind config */ :root { --z-dropdown: 10; --z-sticky: 20; --z-fixed: 30; --z-modal-backdrop: 40; --z-modal: 50; --z-popover: 60; --z-tooltip: 70; } ``` ### Tailwind Config ```javascript // tailwind.config.js module.exports = { theme: { extend: { zIndex: { dropdown: '10', sticky: '20', fixed: '30', modalBackdrop: '40', modal: '50', popover: '60', tooltip: '70', }, }, }, }; ``` ### Usage ```typescript
Modal content
Tooltip
``` ## Animation Best Practices ### Use CSS Transitions ```typescript ``` ### Respect Motion Preferences ```typescript // Disable animations for users who prefer reduced motion
Animated element
``` ### Hardware Acceleration ```typescript // Use transform for smooth animations
Slides on hover
``` ## Best Practices Summary 1. **items-stretch**: Default for main flex containers 2. **Parent External, Child Internal**: Clear separation of concerns 3. **Test Both Modes**: Always verify in dev AND production 4. **Touch Optimization**: Disable tap highlight, ensure touch targets 5. **Mobile First**: Build up from smallest screens 6. **Consistent Z-Index**: Use a defined scale 7. **Respect Accessibility**: Honor motion preferences