CSS Media Queries
1. Introduction to CSS Media Queries
A Media Query checks the characteristics of the user's device and applies CSS rules only when the specified condition is true.
Why Use Media Queries?
- Create mobile-friendly websites.
- Improve user experience.
- Support different screen sizes.
- Build responsive layouts.
- Reduce the need for separate mobile websites.
Desktop
↓
Tablet
↓
Mobile
2. Media Query Syntax
Media Queries begin with the @media rule followed by a condition and the CSS styles to apply.
@media (max-width:768px){
body{
background:#f8f9fa;
}
}
| Part | Description |
|---|---|
| @media | Starts the Media Query. |
| (condition) | Defines when styles are applied. |
| { } | Contains responsive CSS rules. |
3. max-width Media Query
The max-width condition applies CSS rules when the screen width is less than or equal to the specified value.
@media (max-width:768px){
h1{
font-size:24px;
}
}
| Screen Width | Media Query Runs? |
|---|---|
| 500px | ✔ Yes |
| 768px | ✔ Yes |
| 900px | ✖ No |
4. min-width Media Query
The min-width condition applies CSS rules when the screen width is greater than or equal to the specified value.
@media (min-width:992px){
body{
background:#ffffff;
}
}
| Screen Width | Media Query Runs? |
|---|---|
| 800px | ✖ No |
| 992px | ✔ Yes |
| 1200px | ✔ Yes |
5. Common Device Breakpoints
Developers commonly use breakpoints to target different screen sizes such as mobile phones, tablets, laptops, and desktop computers.
| Device | Typical Width |
|---|---|
| Mobile | 0px – 767px |
| Tablet | 768px – 991px |
| Laptop | 992px – 1199px |
| Desktop | 1200px and above |
/* Mobile */
@media(max-width:767px){}
/* Tablet */
@media(min-width:768px){}
/* Desktop */
@media(min-width:1200px){}
6. Combining Media Conditions
You can combine multiple media conditions using the and keyword. This allows CSS to apply styles only when all specified conditions are true.
@media (min-width:768px) and
(max-width:991px){
body{
background:#e9ecef;
}
}
| Condition | Result |
|---|---|
| min-width:768px | Screen width must be at least 768px. |
| max-width:991px | Screen width must not exceed 991px. |
7. Responsive Navigation with Media Queries
Media Queries are commonly used to make navigation menus responsive by changing their layout on smaller screens.
nav{
display:flex;
justify-content:space-between;
}
@media(max-width:768px){
nav{
flex-direction:column;
}
}
| Desktop | Mobile |
|---|---|
| Horizontal Menu | Vertical Menu |
8. Responsive Grid & Flexbox using Media Queries
Media Queries can change Flexbox and Grid layouts according to the screen size.
Flexbox Example
.container{
display:flex;
}
@media(max-width:768px){
.container{
flex-direction:column;
}
}
Grid Example
.gallery{
display:grid;
grid-template-columns:
repeat(3,1fr);
}
@media(max-width:768px){
.gallery{
grid-template-columns:1fr;
}
}
9. Practical Media Query Examples
Example 1: Hide Sidebar on Mobile
@media(max-width:768px){
.sidebar{
display:none;
}
}
Example 2: Change Font Size
@media(max-width:600px){
h1{
font-size:24px;
}
}
Example 3: Responsive Button
@media(max-width:576px){
.btn{
width:100%;
}
}
10. Media Query Comparison Table
| Media Query | Purpose |
|---|---|
| max-width | Apply styles below a specified width. |
| min-width | Apply styles above a specified width. |
| orientation | Target portrait or landscape mode. |
| and | Combine multiple conditions. |
| not | Exclude matching conditions. |
| only | Prevent older browsers from applying styles. |
@media (orientation:landscape){
body{
background:#f8f9fa;
}
}
11. CSS Media Query Best Practices
Following best practices helps you write clean, efficient, and maintainable Media Queries for responsive websites.
- Use a mobile-first approach whenever possible.
- Keep breakpoints consistent throughout the project.
- Use relative units like
emandrem. - Avoid writing unnecessary Media Queries.
- Group similar Media Queries together.
- Test layouts on different devices and browsers.
- Combine Flexbox or Grid with Media Queries.
- Keep CSS organized for easy maintenance.
12. Common Media Query Mistakes
| Mistake | Correct Practice |
|---|---|
| Using too many breakpoints. | Use only necessary breakpoints. |
| Ignoring mobile devices. | Start with a mobile-first design. |
| Using fixed widths. | Use responsive units. |
| Not testing on real devices. | Test on phones, tablets, and desktops. |
| Writing duplicate Media Queries. | Organize and reuse existing queries. |
❌ Incorrect
.box{
width:900px;
}
✔ Correct
.box{
width:100%;
max-width:900px;
}
13. Browser Support
CSS Media Queries are supported by all modern web browsers, making them one of the most reliable responsive design features.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Media Query Interview Questions
- What is a CSS Media Query?
- Why are Media Queries used?
- What is the difference between
max-widthandmin-width? - What are breakpoints in Responsive Web Design?
- What is the mobile-first approach?
- How do you combine multiple Media Query conditions?
- What is the purpose of the
orientationfeature? - Can Media Queries be used with Flexbox and Grid?
- How do Media Queries improve user experience?
- Which CSS rule begins every Media Query?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Media Queries.
- ✔ Media Query Syntax.
- ✔
max-widthandmin-width. - ✔ Common Device Breakpoints.
- ✔ Combining Media Conditions.
- ✔ Responsive Navigation.
- ✔ Responsive Flexbox and Grid.
- ✔ Practical Examples.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
Quick Quiz
Which Media Query is commonly used to apply styles for mobile devices?