Introduction
In todayβs world, users access websites on different devices like mobile phones, tablets, and desktops. A website must look good on all screen sizes.
π This is where Responsive Design comes in.
Responsive design ensures that your website adapts to different screen sizes and provides a better user experience.
What you’ll Learn
- Responsive design basics
- Media queries in CSS
max-widthvsmin-width- Mobile-first approach
- Responsive Flexbox layouts
- Responsive Grid layouts
- Responsive units (
%, rem, vw) - Making images responsive
- Testing responsive design
- Common mistakes to avoid
1οΈβ£ What is Responsive Design?
Responsive design means:
π Your website adjusts automatically
π Based on screen size
π Mobile, tablet, laptop, desktop
Without responsive design:
- Website may look good on laptop
- But broken on mobile
2οΈβ£ What is a Media Query?
Media queries are a feature in CSS that allow you to apply styles based on screen size or device type.
Example:
@media (max-width: 768px) {
body {
background-color: lightblue;
}
}
Explanation:
@mediaβ Defines a media querymax-width: 768pxβ Applies styles when screen width is 768px or smaller
3οΈβ£ Most Common Breakpoints
Breakpoints are screen sizes where your layout changes.
π Common breakpoints:
- Mobile β up to 480px
- Tablet β 481px to 768px
- Small laptops β 769px to 1024px
- Desktop β 1025px and above
π These are not fixed rulesβyou can adjust based on your design.
Using max-width vs min-width
max-width (Desktop First)
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
π Applies styles for smaller screens.
min-width (Mobile First)
@media (min-width: 768px) {
.container {
flex-direction: row;
}
}
π Applies styles for larger screens.
4οΈβ£ Making Flexbox Responsive
Example:
.container {
display: flex;
gap: 20px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
On desktop β row
On mobile β column
Very common pattern.
5οΈβ£ Making Grid Responsive
Example:
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
@media (max-width: 768px) {
.grid-container {
grid-template-columns: 1fr;
}
}
On desktop β 3 columns
On mobile β 1 column
6οΈβ£ Mobile-First Approach (Professional Method)
Instead of writing desktop first, professionals write:
π Mobile styles first
π Then use min-width
Example:
.container {
display: flex;
flex-direction: column;
}
@media (min-width: 768px) {
.container {
flex-direction: row;
}
}
This is called:
β Mobile-first design
Google prefers this.
7οΈβ£ Important: Viewport Meta Tag
In your HTML <head> section, this must be added:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Without this:
Mobile responsiveness will not work properly.
Very important for beginners.
8οΈβ£ Responsive Units (Very Important)
Using the right units is important for responsive design.
π Avoid fixed units like:
px(not flexible)
π Use flexible units:
%β Relative to parentremβ Relative to root font sizeemβ Relative to parent font sizevwβ Viewport widthvhβ Viewport height
Example:
.container {
width: 80%;
}h1 {
font-size: 2rem;
}
π This helps your layout scale properly on different screen sizes.
9οΈβ£ Making Images Fully Responsive
Earlier you saw basic responsiveness, but letβs understand properly.
img {
max-width: 100%;
height: auto;
}
π What this does:
- Prevents image overflow
- Maintains aspect ratio
- Adjusts automatically with screen size
Pro Tip:
Use smaller images for mobile to improve performance.
π Testing Responsive Design
Creating responsive design is not enoughβyou must test it.
Methods:
π Resize browser window
π Use Chrome DevTools (Inspect β Toggle device toolbar)
π Test on real mobile devices
Why this matters:
Sometimes layout looks fine in code but breaks on real screens.
π± Real-World Example
Letβs understand how actual websites use responsive design.
On Mobile:
- Single column layout
- Menu becomes hamburger icon
- Larger buttons for touch
On Tablet:
- Two-column layout
- Medium spacing
On Desktop:
- Multi-column layout
- Sidebar + content
- Full navigation menu
π This is exactly how platforms like blogs, eCommerce, and dashboards work.
Common Mistakes
- Using too many breakpoints ( Keep it simple and clean )
- Not using mobile-first approach ( Always start from small screens )
- Using fixed widths (px) ( Use flexible units instead )
- Forgetting viewport meta tag ( Responsiveness wonβt work properly )
- Not testing on real devices ( Always check mobile behavior )
Practice Tasks
Task 1: Basic Media Query
Change background color when screen width is less than 768px.
π Bonus: Add another breakpoint at 480px.
Task 2: Responsive Layout
Create a layout with:
- 1 column (mobile)
- 2 columns (tablet)
- 3 columns (desktop)
π Use Grid or Flexbox.
Task 3: Responsive Navbar
Create a navigation bar:
- Vertical layout on mobile
- Horizontal layout on desktop
π Try adding spacing and alignment.
Task 4: Image Responsiveness
Add 3 images and make sure:
- They resize properly
- They donβt overflow
- They look good on mobile
Task 5: Mini Responsive Project
Create a full layout:
- Header
- Sidebar
- Main content
- Footer
π Use:
- Grid for layout
- Media queries for responsiveness
Important Points to Remember
- Responsive design is essential for modern websites
- Use media queries to adjust layouts
- Prefer mobile-first approach
- Use flexible units like %, fr, rem
- Combine with Flexbox and Grid
Conclusion
Responsive design ensures your website works on all devices. With media queries, you can easily create layouts that adapt to different screen sizes.
π Practice different layouts
π Test on multiple devices
π Combine Grid + Flexbox + Media Queries
