Section 39: CSS Best Practices and Project Review
Use this section to review a CSS project before release. Focus on predictable styles, reusable components, responsive behavior, accessible interactions, and measured performance.
1. Recommended practices
- Use consistent naming conventions such as BEM or a documented utility system.
- Prefer reusable components and shared design tokens.
- Use Flexbox and Grid for layout rather than positioning every element manually.
- Use responsive sizing and media or container queries where appropriate.
- Keep specificity manageable and avoid unnecessary !important declarations.
- Provide focus states, sufficient contrast, and reduced-motion support.
- Test real content, long text, empty states, and validation errors.
2. Before and after refactoring
/* Before: overly broad and repetitive */
div { margin: 10px; }
.blue { color: blue; }
.page div p { font-size: 16px; }
/* After: component-oriented and reusable */
:root {
--space-sm: .625rem;
--color-link: #1d4ed8;
}
.notice { margin: var(--space-sm); }
.notice__link { color: var(--color-link); }
.notice__message { font-size: 1rem; }Refactoring should preserve intended behavior. Verify that the new selectors still cover every required state and page.
3. Final project review checklist
- All stylesheets load successfully.
- There are no unintended horizontal scrollbars.
- Navigation and form controls work with a keyboard.
- Focus indicators remain visible.
- Text and interface elements have suitable contrast.
- Cards and tables work with long content.
- Responsive breakpoints are based on content needs.
- Animations respect reduced-motion preferences.
- Unsupported features have suitable fallbacks when required.
- Performance is measured on realistic devices and conditions.
4. Practical review exercise
Review the landing page and dashboard from Sections 36 and 37. Find and fix at least five issues, including one responsive issue, one accessibility issue, one maintainability issue, one overflow issue, and one performance or compatibility concern.
Knowledge check: 10 MCQs
- Why use reusable CSS components? Answer: To reduce duplication and improve consistency. Explanation: Shared styles are easier to maintain.
- Why should !important be used sparingly? Answer: It can make cascade behavior harder to manage. Explanation: Excessive use can cause specificity and override problems.
- What should guide responsive breakpoints? Answer: Where the content or layout needs to adapt. Explanation: Breakpoints should solve actual layout constraints.
- What should be checked before release? Answer: Layout, accessibility, compatibility, and performance. Explanation: Visual correctness alone is not enough.
- Why test long text? Answer: It can reveal wrapping and overflow problems. Explanation: Real-world content is not always short.
- Should a refactor preserve required behavior? Answer: Yes. Explanation: Improvements must not silently break existing states.
- Why use CSS custom properties? Answer: To centralize reusable design values. Explanation: Shared tokens simplify design changes.
- Is one desktop screenshot enough for testing? Answer: No. Explanation: Multiple viewports and interaction states must be checked.
- What should happen to nonessential motion for reduced-motion users? Answer: It should be reduced or removed as appropriate. Explanation: Respect the user's motion preference.
- What is the goal of a CSS review? Answer: Reliable, accessible, maintainable styling. Explanation: Good CSS supports long-term development as well as appearance.