๐ฑ CSS COURSE โ MODULE 10: RESPONSIVE DESIGN
๐ File: responsive.md
๐ฑ Responsive Design in CSS
๐ Introduction
Responsive design means: ๐ Your website adapts to all screen sizes
- ๐ฑ Mobile
- ๐ Tablet
- ๐ป Desktop
Without responsive design โ website breaks on phones.
๐ง 1. Mobile-First Concept
Modern CSS starts with mobile first:
body {
font-size: 16px;
}
โ Default = mobile styles โ Then we scale up for bigger screens
๐ 2. Media Queries
Media queries allow different styles based on screen size.
@media (max-width: 768px) {
body {
background: lightgray;
}
}
โ Applies only on screens โค 768px (tablet/mobile)
๐ฑ 3. Common Breakpoints
| Device | Width |
|---|---|
| Mobile | 0 - 576px |
| Tablet | 576px - 768px |
| Laptop | 768px - 1024px |
| Desktop | 1024px+ |
๐งฉ 4. Responsive Text
h1 {
font-size: 2rem;
}
@media (max-width: 600px) {
h1 {
font-size: 1.5rem;
}
}
โ Smaller text on small screens
๐งฑ 5. Responsive Layout with Flexbox
.container {
display: flex;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
โ Row โ Column on mobile
๐งฑ 6. Responsive Grid
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr;
}
}
โ 3 columns โ 1 column on mobile
๐ 7. Fluid Units (VERY IMPORTANT)
Use flexible units instead of fixed px:
div {
width: 80%;
}
or:
div {
font-size: 1rem;
}
โ Helps scaling across devices
๐ฑ 8. Viewport Meta Tag (HTML IMPORTANT)
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
โ REQUIRED for responsive design
๐ง 9. Responsive Strategy
1. Mobile first
2. Use flexible units (%, rem, fr)
3. Use media queries
4. Test on real devices
๐งฉ Real Example
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
.container {
display: flex;
gap: 10px;
}
.box {
flex: 1;
padding: 20px;
background: blue;
color: white;
}
@media (max-width: 600px) {
.container {
flex-direction: column;
}
}
โ ๏ธ Common Mistakes
- Designing only for desktop โ
- Using only px โ
- Forgetting viewport meta tag โ
- Overusing media queries โ
๐ฏ Pro Tip
๐ Best responsive combo:
- Flexbox โ layout adaptation
- Grid โ structure
- rem/% โ sizing
- media queries โ control breakpoints
๐งช Mini Exercise
Create:
- A responsive navbar
- A 3-column layout โ becomes 1 column on mobile
- A responsive text section
- A card layout that adapts screen size