Ask any question about CSS here... and get an instant response.
Why isn't my media query for mobile devices applying correctly when I resize the browser window?
Asked on Dec 18, 2025
Answer
Media queries are crucial for responsive design, but they can sometimes behave unexpectedly if not set up correctly. Let's explore a basic example of how to ensure your media query works as intended.
<!-- BEGIN COPY / PASTE -->
<style>
body {
background-color: lightblue;
}
@media (max-width: 768px) {
body {
background-color: lightcoral;
}
}
</style>
<!-- END COPY / PASTE -->Additional Comment:
- Ensure your media query is correctly targeting the desired screen width. In the example, it changes the background color for screens 768px wide or smaller.
- Check if your CSS is being overridden by other styles. Use browser developer tools to inspect and debug.
- Verify that your HTML includes the viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1">. - Remember that resizing a desktop browser window may not perfectly simulate a mobile device environment.
Recommended Links:
