๐Ÿ“… Week 3 - Day 1: Responsive Design Basics

Monday | Duration: 3 hours

๐ŸŽฏ 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.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • 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

@media (condition) { /* CSS rules */ }

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

/* Default styles for mobile (mobile-first) */ body { font-size: 16px; } .container { width: 100%; padding: 10px; } /* Tablet and above */ @media (min-width: 768px) { body { font-size: 18px; } .container { width: 750px; margin: 0 auto; } } /* Desktop and above */ @media (min-width: 1024px) { body { font-size: 20px; } .container { width: 1000px; } } /* Large desktop */ @media (min-width: 1200px) { .container { width: 1140px; } }

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

/* Max-width: Desktop-first (styles for smaller screens) */ @media (max-width: 768px) { /* Applies when screen is 768px or less */ } /* Min-width: Mobile-first (styles for larger screens) โœ… Recommended */ @media (min-width: 768px) { /* Applies when screen is 768px or more */ }

๐Ÿ“ฒ 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

/* Mobile styles (default) */ nav { display: flex; flex-direction: column; } nav a { padding: 15px; border-bottom: 1px solid #ddd; } /* Tablet and up: horizontal navigation */ @media (min-width: 768px) { nav { flex-direction: row; justify-content: space-around; } nav a { border-bottom: none; border-right: 1px solid #ddd; } }

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

/* Make images responsive */ img { max-width: 100%; height: auto; display: block; }

Live Example - Responsive Image:

This image scales with its container (try resizing your browser):

Responsive demo

Notice: The image never exceeds its container width and maintains aspect ratio.

Responsive Background Images

.hero { background-image: url('mobile-bg.jpg'); background-size: cover; background-position: center; } @media (min-width: 768px) { .hero { background-image: url('desktop-bg.jpg'); } }

๐Ÿ’ป 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)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Profile</title> <link rel="stylesheet" href="css/responsive.css"> </head> <!-- Rest of HTML stays the same -->

Responsive CSS (responsive.css)

/* Reset */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Base styles (Mobile-first) */ body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; background-color: #f4f4f4; } /* Header */ header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; text-align: center; } header h1 { font-size: 1.8em; } /* Container: Stacked on mobile */ .container { max-width: 1200px; margin: 20px auto; padding: 0 15px; } /* Columns: Full width on mobile */ .profile-column, .bio-column { width: 100%; background: white; padding: 20px; margin-bottom: 20px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); } .profile-column img { width: 150px; height: 150px; border-radius: 50%; border: 5px solid #667eea; margin: 0 auto 20px; display: block; } .profile-column h2 { color: #667eea; margin-bottom: 10px; text-align: center; font-size: 1.5em; } .profile-column > p { text-align: center; color: #666; margin-bottom: 20px; } .profile-column h3, .bio-column h3 { color: #555; margin-top: 20px; margin-bottom: 10px; border-bottom: 2px solid #667eea; padding-bottom: 5px; } .profile-column ul { list-style: none; } .profile-column li { padding: 8px 0; border-bottom: 1px solid #eee; } .bio-column h2 { color: #667eea; font-size: 1.8em; margin-bottom: 15px; } .bio-column p { margin-bottom: 15px; } .bio-column ul { margin-left: 25px; margin-bottom: 15px; } /* Footer */ footer { background: #333; color: white; text-align: center; padding: 20px; margin-top: 20px; } /* ========== TABLET (768px and up) ========== */ @media (min-width: 768px) { header h1 { font-size: 2.5em; } .container { padding: 0 20px; } .profile-column img { width: 200px; height: 200px; } .profile-column h2 { font-size: 1.8em; } .bio-column h2 { font-size: 2.2em; } } /* ========== DESKTOP (1024px and up) ========== */ @media (min-width: 1024px) { header { padding: 30px; } /* Side-by-side columns */ .container { display: flex; gap: 30px; margin: 30px auto; } .profile-column { width: 30%; margin-bottom: 0; } .bio-column { width: 70%; margin-bottom: 0; } } /* ========== LARGE DESKTOP (1200px and up) ========== */ @media (min-width: 1200px) { header h1 { font-size: 3em; } .profile-column, .bio-column { padding: 30px; } }

Live Example - Layout Transformation:

This demonstrates how a 2-column layout adapts across screen sizes:

๐Ÿ“ฑ Mobile View (< 768px)
Columns stack vertically
๐Ÿ“ฑ Tablet View (768px - 1023px)
Still stacked, but larger fonts/images
๐Ÿ’ป Desktop View (โ‰ฅ 1024px)
Side-by-side columns (30% / 70%)

Notice: The layout progressively enhances from mobile to desktop.

๐Ÿงช Testing Responsive Design

Using Chrome DevTools:

  1. Open your page in Chrome
  2. Press F12 or Ctrl+Shift+I
  3. Click the device icon (Toggle device toolbar) or press Ctrl+Shift+M
  4. Select different devices from the dropdown
  5. 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:

/* Mobile: hide sidebar */ .sidebar { display: none; } /* Desktop: show sidebar */ @media (min-width: 1024px) { .sidebar { display: block; } }

Live Example - Conditional Display:

On mobile, the sidebar is hidden. On desktop (โ‰ฅ 1024px), it appears:

Main Content
Always visible
Sidebar
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"