CSS Media Queries: Min and Max for Responsive Design

Web design has come a long way, and one of the most crucial advancements is the introduction of CSS media queries. These powerful tools allow developers to create responsive designs that automatically adapt to different screen sizes and device types. In this article, we’ll dive into the world of CSS media queries, focusing on the min and max properties to achieve flexible and user-friendly designs.

How to write media queries in CSS?

To understand the power of min and max in CSS media queries, we first need to grasp the concept of media queries. These are a set of rules that determine the styling of elements based on specific conditions. Media queries are an essential part of responsive design, as they allow a single stylesheet to cater to different devices and screen sizes.

Syntax and Structure of Media Queries

A media query consists of a media type and one or more expressions, which evaluate as either true or false. The syntax for a media query is as follows:

@media (media-feature) {
  /* CSS rules */

The media type is usually omitted, as modern browsers assume the default media type is “screen.” Media features, on the other hand, are the conditions used to apply CSS rules. This is where min and max properties come into play.

The Magic of Min and Max

The min and max properties in media queries represent the minimum and maximum values of a specific media feature. Commonly used media features include width, height, aspect ratio, resolution, and more. By utilizing min and max, developers can control the styling of their websites or applications based on specific criteria.

Using Min-Width and Max-Width

Min-width and max-width are the most commonly used media features in responsive design. They allow developers to apply CSS rules based on the width of the viewport. Let’s explore how min-width and max-width work with practical examples.

Example 1: Using Min-Width

@media (min-width: 768px) {
  /* CSS rules for viewports at least 768px wide */

In this example, the CSS rules within the media query will only apply if the viewport width is at least 768 pixels.

Example 2: Using Max-Width

@media (max-width: 767px) {
  /* CSS rules for viewports up to 767px wide */

In this case, the CSS rules will apply to viewports with a width of 767 pixels or less.

Combining Min-Width and Max-Width

It’s also possible to combine both min-width and max-width in a single media query, like so:

@media (min-width: 480px) and (max-width: 767px) {
  /* CSS rules for viewports between 480px and 767px wide */

This media query targets viewports with a width between 480 pixels and 767 pixels, inclusive.

Min-Height and Max-Height

Just like with width, you can also use min-height and max-height to target specific viewport heights. These media features are especially helpful for adjusting the layout of elements vertically.

Example 1: Using Min-Height

@media (min-height: 600px) {
  /* CSS rules for viewports at least 600px tall */

Example 2: Using Max-Height

@media (max-height: 599px) {
  /* CSS rules for viewports up to 599px tall */

Combining Multiple Media Features

You can combine multiple media features in a single media query to target a specific range of devices or conditions. This allows for even greater control over your designs and layouts. Let’s see how this works in practice.

Example 1: Combining Width and Height

@media (min-width: 768px) and (min-height: 600px) {
/* CSS rules for viewports at least 768px wide and 600px tall */

In this example, the CSS rules will only apply if the viewport meets both width and height criteria.

Example 2: Combining Aspect Ratio and Resolution

@media (min-aspect-ratio: 16/9) and (min-resolution: 300dpi) {
/* CSS rules for viewports with an aspect ratio of at least 16:9 and a resolution of 300dpi or higher */

This media query targets devices with a minimum aspect ratio of 16:9 and a resolution of 300 dots per inch (dpi) or higher.

Tips for Working with Min and Max

  • Always design for the smallest screen first (mobile-first approach) and then progressively enhance your design for larger screens. This ensures your design remains clean and functional on all devices.
  • Use percentage-based units for widths and heights instead of fixed pixel values. This will allow your layouts to scale seamlessly across different devices.
  • Test your designs on multiple devices and screen sizes to ensure a consistent and pleasing user experience.
  • Combine media queries with other CSS techniques, such as Flexbox and CSS Grid, to create truly responsive and adaptable layouts.
  • Don’t overuse media queries. Although they’re powerful, too many media queries can make your CSS code hard to maintain and may lead to unexpected results.


CSS media queries, specifically the min and max properties, have become an indispensable tool for web developers in crafting responsive designs. By understanding the power of min and max, you can create websites and applications that adapt seamlessly to different devices and screen sizes, providing an exceptional user experience. Keep experimenting and refining your skills to stay ahead in the ever-evolving world of web design.

Leave a Comment