Introduction to Media Queries in CSS
In the ever-evolving landscape of web design, responsive design has become a crucial aspect. Media queries in CSS play a significant role in achieving this responsiveness. They allow us to apply different styles for different media types/devices, ensuring our websites look and function optimally across various screen sizes and types.
Understanding Media Queries
Media queries are essentially conditions that CSS uses to apply styles based on certain criteria. These criteria can include the width and height of the viewport, the resolution of the device, the orientation of the device, and more. If the condition is met, the styles within the query are applied.
Here's a simple syntax of a media query:

<style>
@media (condition) {
/* CSS rules go here */
}
</style>
Adding Media Queries to Your CSS
Now that we understand what media queries are, let's dive into how to add them to your CSS.
1. Defining the Condition
The first step is to define the condition that will trigger the styles within the media query. This is done within the parentheses after the '@media' rule.
For example, to apply styles when the viewport is 600px wide or less, you would use:

@media (max-width: 600px) { ... }
2. Applying Styles
Once the condition is defined, you can apply your desired styles within the curly braces '{}'. These styles will only be applied if the condition is met.
For instance, to change the font size of a paragraph when the viewport is 600px wide or less, you could use:
@media (max-width: 600px) {
p {
font-size: 16px;
}
}
3. Using Multiple Conditions
You can also use multiple conditions within a single media query. This can be useful for targeting specific devices or screen sizes.

For example, to target devices with a screen resolution of 1024x768 and a color depth of at least 8 bits per color, you could use:
@media (width: 1024px) and (height: 768px) and (color: 8-bit) { ... }
Common Media Query Conditions
Here are some of the most common media query conditions you might use:
max-widthandmin-width: Applies styles based on the width of the viewport.max-heightandmin-height: Applies styles based on the height of the viewport.orientation: Applies styles based on the orientation of the device (portrait or landscape).resolution: Applies styles based on the resolution of the device.aspect-ratio: Applies styles based on the aspect ratio of the viewport.
Media Query Examples
Let's look at a few practical examples of media queries in action.
Responsive Images
Media queries can be used to serve different image sources based on the size of the viewport. This can help improve performance by serving smaller images on smaller screens.
img {
max-width: 100%;
}
@media (min-width: 600px) {
img {
max-width: 600px;
}
}
Responsive Navigation
Media queries can also be used to create responsive navigation menus. For instance, you might want to switch to a hamburger menu on smaller screens.
nav ul {
display: flex;
}
@media (max-width: 600px) {
nav ul {
display: none;
}
nav .hamburger {
display: block;
}
}
Best Practices for Using Media Queries
While media queries are a powerful tool, it's important to use them judiciously. Here are some best practices to keep in mind:
- Test your media queries on a variety of devices and screen sizes to ensure they're working as expected.
- Avoid using too many media queries, as this can lead to complex and difficult-to-maintain code.
- Consider using a mobile-first approach, applying styles for larger screens only when necessary.
- Use a tool like a CSS preprocessor or a CSS-in-JS library to help manage your media queries.
Conclusion
Media queries are a vital part of modern web design, allowing us to create responsive, adaptive websites that look and function well on any device. By understanding how to add media queries to your CSS, you can take full advantage of this powerful tool and create websites that truly shine across all platforms.






















