CSS Styling and Responsive Design
Style websites with AI — responsive layouts using Flexbox and Grid, modern CSS techniques, design tokens, and mobile-first design that works on every screen.
Premium Course Content
This lesson is part of a premium course. Upgrade to Pro to unlock all premium courses and content.
- Access all premium courses
- 1000+ AI skill templates included
- New content added weekly
🔄 Quick Recall: In the last lesson, you built semantic HTML structures with AI — page layouts, forms, and accessible navigation. Now let’s make them look good with CSS.
Design Tokens: Your CSS Foundation
Before writing any component styles, establish your design system:
Create a CSS custom properties (variables) file for a website design system:
Brand colors:
- Primary: [your brand color, e.g., #3b82f6]
- Secondary: [secondary color]
- Accent: [accent color for CTAs]
Generate from these base colors:
1. Color scale: 50, 100, 200, ..., 900 for primary and secondary
2. Semantic colors: success, warning, error, info
3. Surface colors: background, surface, surface-raised (light and dark mode)
4. Text colors: primary, secondary, muted, on-primary
Spacing scale:
- 8px base unit: space-1 (4px) through space-20 (160px)
Typography:
- Font families: heading, body, mono
- Size scale: xs through 4xl using clamp() for fluid sizing
- Line heights and font weights
Border radius: sm, md, lg, xl, full
Shadows: sm, md, lg, xl
Use :root for light mode defaults and @media (prefers-color-scheme: dark) for dark mode.
✅ Quick Check: Why use
clamp()for font sizes instead of fixed values?
Because clamp() creates fluid typography that scales smoothly between screen sizes. font-size: clamp(1rem, 2.5vw, 1.5rem) means the font is never smaller than 1rem, never larger than 1.5rem, and scales fluidly between — no media query breakpoints needed. The text is always readable on mobile and always proportional on desktop.
Responsive Layouts with Grid
Page Layout
Create a responsive page layout using CSS Grid:
Structure:
- Header (full width, sticky)
- Sidebar (left, 280px on desktop, hidden on mobile with toggle)
- Main content (flexible, centered, max-width 800px for readability)
- Footer (full width)
Responsive behavior:
- Mobile (< 768px): Single column, no sidebar, hamburger menu
- Tablet (768px - 1024px): Sidebar collapses to icons only (64px)
- Desktop (> 1024px): Full sidebar visible
Use mobile-first approach (base styles for mobile, min-width queries for larger).
Use CSS custom properties for spacing and colors.
No frameworks — vanilla CSS only.
Card Grid
Create a responsive card grid using CSS Grid:
Requirements:
- Cards auto-fill available width
- Minimum card width: 280px
- Maximum card width: 1fr (flexible)
- Consistent gap: var(--space-6)
- Cards have equal height (stretch to tallest in row)
- Card content: image, title, description, button pinned to bottom
Use grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
Mobile: 1 column. Tablet: 2 columns. Desktop: 3-4 columns (automatic).
Component Styling
Buttons
Create a button system with CSS:
Button types:
- Primary (solid background, white text)
- Secondary (outline, colored border and text)
- Ghost (text only, subtle hover)
- Danger (red variant for destructive actions)
Sizes: sm, md (default), lg
States for each: default, hover, focus-visible, active, disabled
Requirements:
- Use CSS custom properties for all colors
- focus-visible outline (not focus) for keyboard-only indicators
- Smooth transitions (150ms ease)
- Disabled state reduces opacity, removes pointer events
- All sizes maintain consistent padding ratios
Use .btn as base class with modifier classes (.btn-primary, .btn-sm, etc.)
Navigation Bar
Create a responsive navigation bar with CSS:
Desktop (> 768px):
- Horizontal layout, logo left, links center, CTA button right
- Dropdown menus appear on hover with smooth animation
- Active link indicated with bottom border
Mobile (< 768px):
- Logo left, hamburger icon right
- Menu slides in from right (off-canvas) when toggled
- Full-height overlay with large touch targets (48px minimum)
- Dropdown items expand vertically
Use Flexbox for the nav layout.
Include smooth transitions for hover states and mobile menu toggle.
Mobile menu toggle will need a small JavaScript snippet (provide it separately).
Dark Mode
Add dark mode support to this stylesheet:
[paste your existing CSS]
Requirements:
1. Use @media (prefers-color-scheme: dark) to detect system preference
2. Override only the CSS custom properties (colors, shadows) — not component styles
3. Ensure sufficient contrast ratios (WCAG AA: 4.5:1 for text)
4. Adjust shadow colors (dark mode shadows should use darker rgba values)
5. Images and illustrations: add slight reduction in brightness
Also add a manual toggle class (.dark-mode on body) that overrides system preference.
✅ Quick Check: Why override custom properties instead of rewriting component styles for dark mode?
Because if your components use var(--color-bg) and var(--color-text), switching to dark mode means changing only the variable definitions — not every component. A button that uses background: var(--color-primary) and color: var(--color-on-primary) works in both modes without any additional CSS. This is the power of design tokens: one change propagates everywhere.
CSS Verification
After AI generates CSS, check these common issues:
Review this CSS for quality and best practices:
[paste your CSS]
Check for:
1. Hardcoded colors or spacing (should use custom properties)
2. Missing mobile-first media queries (should use min-width, not max-width)
3. Accessibility issues (contrast, focus states, touch targets)
4. Performance issues (expensive selectors, large box-shadows, excessive animations)
5. Missing dark mode consideration
6. Inconsistent naming conventions
7. Specificity conflicts or !important usage
Suggest improvements for each issue found.
Exercise: Style Your Page
- Create a design tokens file using the custom properties prompt
- Style the HTML page you built in Lesson 2 with a responsive layout (Grid)
- Add a card grid section with at least 4 cards
- Add a button system with primary and secondary variants
- Add dark mode support using custom property overrides
- Test at mobile (375px), tablet (768px), and desktop (1200px) widths
Key Takeaways
- Start every project with design tokens (CSS custom properties) — colors, spacing, typography, and shadows defined once and used everywhere
- Mobile-first CSS (base styles + min-width queries) is cleaner than desktop-first (overrides + max-width queries)
- CSS Grid handles two-dimensional layouts (page structure, card grids); Flexbox handles one-dimensional alignment (nav bars, card internals)
- Use
clamp()for fluid typography that scales smoothly without breakpoints - Dark mode becomes trivial when components use custom properties — override the tokens, not the components
- Always verify AI-generated CSS for hardcoded values, missing focus states, and accessibility contrast issues
Up Next: In the next lesson, you’ll add interactivity with JavaScript — DOM manipulation, event handling, and dynamic features that bring your pages to life.
Knowledge Check
Complete the quiz above first
Lesson completed!