CSS
Layout
- •Use Flexbox for one-dimensional layout (row or column); use Grid for two-dimensional layout
- •Prefer
gapover margins between flex/grid children - •Avoid fixed widths on containers — use
max-widthwith fluid defaults - •Use logical properties (
margin-inline,padding-block) for internationalization-friendly spacing - •Don't use
floatfor layout — it's for wrapping text around images
Responsive Design
- •Design mobile-first — use
min-widthmedia queries to add complexity at larger breakpoints - •Use relative units (
rem,em,%,vw/vh) over fixedpxfor sizing that should scale - •Test layouts at common breakpoints and between them — don't just test exact breakpoints
- •Use
clamp()for fluid typography and spacing that scales between a min and max - •Ensure content is usable without horizontal scrolling at 320px viewport width
Conventions
- •Avoid magic numbers — use design tokens or variables for spacing, colors, and typography
- •Use CSS custom properties (
--color-primary,--space-md) for values that repeat or should be themeable - •Keep specificity low — prefer class selectors; avoid
!importantand deep nesting - •Colocate styles with the component they belong to (CSS Modules, scoped styles, or utility classes)
- •Don't override library/framework component styles with global CSS — use their intended customization API
Common Pitfalls
- •Don't set
height: 100%without ensuring all ancestors have explicit heights - •Avoid
overflow: hiddenas a band-aid — find and fix the element that overflows - •Don't use
z-indexwithout a stacking context strategy — define named layers or a scale - •Avoid
position: absoluteinsideposition: staticparents — the absolute child won't be positioned relative to the parent - •Don't animate
width/height/top/left— usetransformandopacityfor performant animations