CSS Media Queries & Responsive Design Kya Hote Hain?
Har Screen Size ke liye Perfect Layouts Banana Sikhein
Introduction to Responsive Design
Responsive Web Design (RWD) ek aisi design approach hai jiska uddeshya hai ki ek website alag-alag devices (jaise desktop, tablet, aur mobile) par aasaani se use ki ja sake aur acchi dikhe. Iske liye flexible layouts, flexible images, aur CSS Media Queries ka use hota hai.
CSS Media Queries Kya Hain?
CSS Media Queries ek technique hai jo aapko specific conditions ke basis par CSS styles apply karne ki suvidha deti hai. Yeh conditions aamtaur par device ki screen width, height, ya orientation par based hoti hain.
Syntax:
@media media-type and (condition: value) {
/* CSS rules yahan aayenge */
}
/* Example: Jab screen width 768px ya usse kam ho */
@media screen and (max-width: 768px) {
body {
background-color: lightblue;
}
}
Breakpoints in Responsive Design
Breakpoints woh specific screen widths hote hain jahan par website ka layout change hota hai taaki content sahi se dikhe. For example, ek 3-column layout mobile par 1-column layout me badal jaata hai.
Device Type | Breakpoint |
---|---|
Phones | Below 768px |
Tablets | 768px to 992px |
Desktops | Above 992px |
Yeh values fixed nahi hain, aap apne design ke hisab se breakpoints choose kar sakte hain.
Mobile-First vs. Desktop-First
Mobile-First Approach (Recommended)
Is approach me, hum pehle mobile devices ke liye default styles likhte hain aur fir min-width
media queries ka use karke badi screens (tablets, desktops) ke liye styles add karte hain.
/* Default styles for mobile */
.container {
width: 100%;
}
/* Styles for tablets and larger screens */
@media (min-width: 768px) {
.container {
width: 750px;
}
}
Desktop-First Approach
Is approach me, hum pehle desktop ke liye styles likhte hain aur fir max-width
ka use karke chhoti screens ke liye styles ko adjust ya override karte hain.
/* Default styles for desktop */
.container {
width: 960px;
}
/* Styles for tablets and smaller screens */
@media (max-width: 992px) {
.container {
width: 100%;
}
}
Key Takeaways
- Responsive Design ka goal har device par best user experience dena hai.
- CSS Media Queries conditions ke basis par styles apply karti hain.
- Breakpoints woh points hote hain jahan layout change hota hai.
- Mobile-First approach modern web development me best practice maani jaati hai.
min-width
ka use "is width se upar" ke liye aurmax-width
ka use "is width tak" ke liye hota hai.
Ek `div` banayein aur media query ka use karke uska background color alag-alag screen sizes par change karein.
Practice in CSS EditorApni knowledge test karne ke liye is quick quiz ko dein.
Start Quiz