๐ฏ Learning Objectives
- Understand responsive web design principles
- Learn about viewport and meta tags
- Master CSS media queries
- Implement mobile-first approach
- Convert profile page to responsive design
๐ฑ What is Responsive Web Design?
Responsive Web Design (RWD) is an approach that makes web pages render well on all devices and screen sizes.
Key principles:
- Fluid grids (flexible layouts)
- Flexible images
- Media queries
- Mobile-first mindset
๐ฑ Mobile
320px - 480px
๐ฑ Tablet
481px - 768px
๐ป Desktop
769px+
Why Responsive Design?
- Mobile traffic: Over 60% of web traffic is mobile
- SEO: Google prioritizes mobile-friendly sites
- User experience: Users expect sites to work on any device
- Cost-effective: One site for all devices
๐ง Viewport Meta Tag
The viewport meta tag tells browsers how to control the page's dimensions and scaling.
- width=device-width: Sets width to device screen width
- initial-scale=1.0: Sets initial zoom level to 100%
โ ๏ธ Always include this in your <head> section!
Without it, mobile browsers display desktop version zoomed out.
๐ Media Queries
Media queries apply different CSS styles based on device characteristics (usually screen width).
Basic Syntax
Common Breakpoints
| Device | Breakpoint | Media Query |
|---|---|---|
| Mobile | < 768px | @media (max-width: 767px) |
| Tablet | 768px - 1024px | @media (min-width: 768px) and (max-width: 1024px) |
| Desktop | > 1024px | @media (min-width: 1025px) |
Media Query Examples
Live Example - Responsive Container:
Resize your browser window to see how the container adapts:
๐ฑ This container is responsive!
Try resizing your browser: Mobile (100% width) โ Tablet (750px) โ Desktop (1000px) โ Large (1140px)
Max-Width vs Min-Width
๐ฒ Mobile-First Approach
Mobile-first means writing CSS for mobile devices first, then adding styles for larger screens.
Why Mobile-First?
- Forces you to prioritize content
- Better performance on mobile
- Easier to scale up than down
- Most traffic comes from mobile
Example: Mobile-First Navigation
Live Example - Responsive Navigation:
On mobile (< 768px): Vertical stack | On desktop (โฅ 768px): Horizontal row
Notice: This is showing the mobile version. At 768px+, it would switch to horizontal layout.
๐ผ๏ธ Responsive Images
Live Example - Responsive Image:
This image scales with its container (try resizing your browser):
Notice: The image never exceeds its container width and maintains aspect ratio.
Responsive Background Images
๐ป Main Activity: Responsive Profile Page
Task: Convert profile-bio.html to Responsive Design
Update your 2-column layout to stack on mobile devices.
Updated HTML (add viewport)
Responsive CSS (responsive.css)
Live Example - Layout Transformation:
This demonstrates how a 2-column layout adapts across screen sizes:
Columns stack vertically
Still stacked, but larger fonts/images
Side-by-side columns (30% / 70%)
Notice: The layout progressively enhances from mobile to desktop.
๐งช Testing Responsive Design
Using Chrome DevTools:
- Open your page in Chrome
- Press F12 or Ctrl+Shift+I
- Click the device icon (Toggle device toolbar) or press Ctrl+Shift+M
- Select different devices from the dropdown
- Or drag to manually resize
Test These Breakpoints:
- 320px (iPhone SE)
- 375px (iPhone X/11)
- 768px (iPad)
- 1024px (iPad Pro)
- 1920px (Desktop)
โ๏ธ Practice Exercises
Exercise 1: Responsive Navigation
Create a navigation that:
- Stacks vertically on mobile
- Displays horizontally on desktop
- Has hover effects
Exercise 2: Responsive Grid
Create a product grid that shows:
- 1 column on mobile (< 768px)
- 2 columns on tablet (768px - 1024px)
- 3 columns on desktop (> 1024px)
Exercise 3: Hide/Show Content
Use media queries to hide sidebar on mobile:
Live Example - Conditional Display:
On mobile, the sidebar is hidden. On desktop (โฅ 1024px), it appears:
Always visible
Hidden on mobile
Notice: The sidebar (shown faded here) would be completely hidden on mobile devices.
โ Today's Deliverable
- Responsive profile-bio.html page
- responsive.css with media queries
- Screenshots at 3 different screen sizes (mobile, tablet, desktop)
- Committed to GitHub with message: "Add responsive design"