-
Hajipur, Bihar, 844101
In Responsive Web Design (RWD), media queries are one of the most powerful tools for creating layouts that adapt to different devices and screen sizes. Media queries allow developers to apply CSS rules conditionally based on characteristics of the user’s device, such as screen width, height, resolution, orientation, or even the type of device. By using media queries effectively, a website can provide an optimal user experience across desktops, tablets, and mobile devices.
In this chapter, you will learn what media queries are, why they are important, core concepts, practical examples, accessibility considerations, common mistakes, best practices, and real-world applications.
Media queries are CSS techniques that let you apply styles only when certain conditions are met. These conditions are based on the media features of a device or viewport. For example, you can define different styles for screens smaller than 768 pixels wide or adjust layouts for devices in landscape orientation.
The basic syntax of a media query is:
@media (max-width: 768px) {
body {
font-size: 14px;
}
}
In this example, the font size changes only when the viewport width is 768 pixels or less.
Media queries are essential for RWD because they allow websites to:
Adapt layouts for multiple screen sizes without creating separate pages.
Enhance readability by adjusting font sizes and line heights for smaller screens.
Improve usability by repositioning or resizing navigation menus, buttons, and interactive elements.
Optimize images, videos, and other media to fit the device’s display.
Maintain a consistent user experience regardless of device type or resolution.
Without media queries, websites may look great on desktops but become difficult to navigate or read on tablets and mobile devices.
A breakpoint is a specific screen width or device condition at which the layout changes. Common breakpoints include:
Large desktop: 1200px and above
Desktop: 992px–1199px
Tablet: 768px–991px
Mobile: below 768px
Breakpoints are not fixed rules; developers can define them based on the website’s content and layout requirements.
Media queries can target different types of devices using media types such as:
all – Suitable for all devices
screen – Screens, tablets, smartphones, and desktops
print – Printed documents
speech – Screen readers
You can combine multiple features in a single media query using logical operators:
and – All conditions must be true
, (comma) – Acts as an OR
not – Negates the condition
Example:
@media screen and (max-width: 768px) and (orientation: portrait) {
.container {
grid-template-columns: 1fr;
}
}
This rule applies only to screens smaller than 768px in portrait orientation.
body {
font-size: 18px;
}
@media (max-width: 992px) {
body {
font-size: 16px;
}
}
@media (max-width: 768px) {
body {
font-size: 14px;
}
}
This ensures that text remains readable across different screen sizes.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
@media (max-width: 992px) {
.grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 768px) {
.grid-container {
grid-template-columns: 1fr;
}
}
The grid layout adjusts dynamically as the viewport shrinks, maintaining readability and usability.
.navbar {
display: flex;
}
@media (max-width: 768px) {
.navbar {
flex-direction: column;
}
}
The menu switches from a horizontal to a vertical layout on smaller screens for easier navigation.
Maintain adequate font sizes to ensure readability on all devices.
Ensure interactive elements like buttons remain large enough for touchscreens.
Avoid hiding essential content behind media queries that may be difficult for screen readers to access.
Test responsive changes with keyboard navigation and assistive technologies.
Proper use of media queries improves usability for all users, including those with disabilities.
Overusing breakpoints, which can make CSS hard to maintain.
Using fixed widths instead of flexible units like % or fr.
Forgetting to test on real devices, relying solely on browser resizing.
Not considering orientation changes on tablets and smartphones.
Hiding essential content or functionality with media queries.
Avoiding these mistakes ensures your responsive design remains functional and accessible.
Use a mobile-first approach: write default styles for small screens first, then add media queries for larger screens.
Define breakpoints based on content needs, not just device sizes.
Combine relative units with media queries for better scalability.
Keep CSS organized and group media queries logically for maintainability.
Test layouts across multiple devices, screen sizes, and orientations.
Use min-width media queries for mobile-first design and max-width for desktop-first approaches.
Media queries are widely used in:
Responsive grids and card layouts for e-commerce sites
Typography adjustments for blogs, news portals, and corporate websites
Adaptive navigation menus for mobile devices
Image and video scaling for multimedia websites
Form layouts that adjust based on available screen width
By leveraging media queries, developers can create highly adaptable websites that provide an optimal experience for all users.
Media queries are a core component of Responsive Web Design, allowing websites to adapt dynamically to different devices and viewport sizes. By defining breakpoints and applying conditional styles, developers can optimize typography, layout, navigation, and media for desktops, tablets, and mobile devices. Proper use of media queries enhances usability, readability, and accessibility, making websites more user-friendly and professional. Mastering media queries is essential for any modern web developer focused on responsive design.
Q1. Hide a sidebar on screens smaller than 768px
Q2. Change body background color only on tablets
Q3. Resize image to 100% width on mobile devices
Q4. Stack two columns vertically on small screens
Q5. Change font size for headings on large desktops
Q6. Adjust padding and margin for small screens
Q7. Show a hamburger icon below 600px screen width
Q8. Apply dark background only in portrait mode
Q9. Make navbar fixed only on desktop
Q10. Hide a banner on screens wider than 1200px