VITSOLS
2025-10-05
What are CSS Media Queries?
CSS Media Queries are rules that allow developers to apply CSS styles based on device characteristics such as screen width, height, orientation, or resolution. They are the backbone of responsive web design, ensuring websites look great on mobiles, tablets, laptops, and desktops.
Why are Media Queries Helpful?
- ✅ Make websites mobile-friendly.
- ✅ Improve user experience on different devices.
- ✅ Follow Google’s mobile-first indexing SEO rules.
- ✅ Save time by writing adaptive styles instead of building separate mobile sites.
Basic Syntax of a Media Query
@media (condition) {
/* CSS styles go here */
}
Example: Applying styles only when the screen width is less than 768px:
@media (max-width: 768px) {
body {
background-color: lightblue;
}
}
Result: On mobile or smaller screens, the background becomes light blue.
Real-Time Examples of Media Queries
1. Mobile First Design
/* Base (Mobile) */
body {
font-size: 14px;
}
/* Tablet */
@media (min-width: 768px) {
body {
font-size: 16px;
}
}
/* Desktop */
@media (min-width: 1024px) {
body {
font-size: 18px;
}
}
Result: Font size scales up as screen width increases.
2. Responsive Navigation
/* Hide menu on mobile */
@media (max-width: 600px) {
nav ul {
display: none;
}
}
/* Show menu on larger screens */
@media (min-width: 601px) {
nav ul {
display: flex;
}
}
Result: Collapsed menu on mobile, full navigation on desktop.
Popular Media Query Breakpoints
Device | Breakpoint |
---|---|
Mobile | max-width: 600px |
Tablet | min-width: 601px and max-width: 1024px |
Laptop/Desktop | min-width: 1025px |
✅ Top Media Query Interview Questions ?
- What are CSS Media Queries and why are they used?
- Explain the difference between min-width and max-width.
- What is the difference between Mobile-first and Desktop-first approaches?
- How do you target both orientation and resolution using media queries?
- Can we combine multiple conditions in a single media query? Give an example.
- What’s the difference between @media screen and @media all?
- How can you apply different styles for high-resolution (Retina) displays?
- Write a media query that applies styles only for devices between 768px and 1024px.
- What are the limitations of media queries?
- do media queries differ from using CSS frameworks like Bootstrap for responsiveness?