Skip to content
GitHub

CSS Media Query

Switch to Zen Mode


The basic syntax of a CSS media query is:

@media [media-type] and (condition) {
/* CSS rules */
}
css
  • @media: The keyword that starts the media query.
  • [media-type]: Optional. Specifies the type of device (screen, print, etc.).
  • and: Combines multiple conditions (optional).
  • (condition): Specifies the condition for the query (e.g., max-width, min-height, width of the viewport, resolution, orientation).

In the following example, the background color of the body will change to light blue only if the viewport width is 600px or smaller.

@media screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
css


Media queries can target various media features. Some of the most commonly used features include:

  • width: The width of the viewport (or device screen).
  • height: The height of the viewport (or device screen).
  • min-width: Specifies the minimum width of the viewport.
  • max-width: Specifies the maximum width of the viewport.

These are the most commonly used features for responsive design, allowing you to target devices based on their screen size.

  • orientation: Defines whether the device is in portrait or landscape mode.
@media (orientation: portrait) { ... }
@media (orientation: landscape) { ... }
css
  • resolution: Specifies the resolution of the device, which is useful for high-DPI (dots per inch) screens like Retina displays.
@media (min-resolution: 192dpi) { ... }
css
  • aspect-ratio: Specifies the aspect ratio of the device’s screen.
@media (max-aspect-ratio: 16/9) { ... }
css

You can use min-width and max-width to create responsive ranges that define the different breakpoints for different screen sizes. This technique is often used in responsive web design to ensure your layout adapts at specific intervals.

/* Styles for small screens */
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
/* Styles for medium screens */
@media (min-width: 601px) and (max-width: 1024px) {
body {
background-color: lightgreen;
}
}
/* Styles for large screens */
@media (min-width: 1025px) {
body {
background-color: lightcoral;
}
}
css

You can write media queries directly within a CSS file, or you can include them in an external stylesheet. Below is an example of a media query applied within a CSS file:

/* Default styles for larger screens */
body {
font-size: 18px;
background-color: lightgray;
}
/* Styles for smaller screens */
@media screen and (max-width: 600px) {
body {
font-size: 14px;
background-color: lightblue;
}
}
css

In the above example, the body font size and background color change depending on the screen width.


  • Start by designing your layout for the smallest devices first, and then use media queries to adapt it for larger devices.
  • Example:
    body {
    font-size: 14px;
    }
    /* Larger screens */
    @media (min-width: 600px) {
    body {
    font-size: 18px;
    }
    }
    css
  • Use as few breakpoints as possible to keep the CSS maintainable.
  • Start with basic ranges (e.g., 600px, 1024px) and refine from there if needed.
  • Don’t define styles in every media query unless necessary; instead, override only the properties that need adjusting.
  • Use relative units (e.g., em, rem, vw, vh) for font sizes, margins, paddings, etc., to make your layout more flexible.

Here are some examples of common media queries used in responsive web design:

/* Mobile-first styles */
body {
font-size: 14px;
background-color: lightgray;
}
@media (min-width: 600px) {
body {
font-size: 16px;
background-color: lightgreen;
}
}
@media (min-width: 1024px) {
body {
font-size: 18px;
background-color: lightblue;
}
}
css
@media (orientation: portrait) {
.sidebar {
display: block;
}
}
@media (orientation: landscape) {
.sidebar {
display: none;
}
}
css
@media only screen and (min-resolution: 192dpi) {
img {
max-width: 100%;
height: auto;
}
}
css

You can also combine multiple media queries using the and, or, and not operators to create more specific conditions.

@media screen and (max-width: 800px) and (orientation: landscape) {
body {
background-color: lightgreen;
}
}
css

Below is a table listing the sizes of various device screens in pixels commonly used in CSS responsive design. This is based on common screen resolutions and device types:

DeviceScreen Resolution (Width x Height)Device TypeCSS Media Query Range
iPhone 12/13/141170 x 2532Mobilemax-width: 767px
iPhone SE (2nd Gen)750 x 1334Mobilemax-width: 767px
iPhone 6/7/8750 x 1334Mobilemax-width: 767px
iPhone X/XS1125 x 2436Mobilemax-width: 767px
Samsung Galaxy S201440 x 3200Mobilemax-width: 767px
Pixel 51080 x 2340Mobilemax-width: 767px
iPad Mini (5th Gen)768 x 1024Tablet768px - 1024px
iPad (8th Gen)810 x 1080Tablet768px - 1024px
iPad Air (4th Gen)820 x 1180Tablet768px - 1024px
iPad Pro 11”834 x 1194Tablet768px - 1024px
iPad Pro 12.9”1024 x 1366Tablet768px - 1024px
MacBook Air (13”)2560 x 1600Laptop/PCmin-width: 1025px
MacBook Pro (13”)2560 x 1600Laptop/PCmin-width: 1025px
Dell XPS 131920 x 1080Laptop/PCmin-width: 1025px
MacBook Pro (16”)3072 x 1920Laptop/PCmin-width: 1025px
Microsoft Surface Pro2736 x 1824Laptop/Tablet Hybridmin-width: 1025px
Samsung Galaxy Tab S71600 x 2560Tablet768px - 1024px
Desktop (Full HD)1920 x 1080Desktopmin-width: 1025px
Desktop (4K)3840 x 2160Desktopmin-width: 1025px
  • Mobile Devices (portrait): max-width: 767px
  • Tablets (portrait): min-width: 768px and max-width: 1024px
  • Laptops & Desktops: min-width: 1025px

This table is useful for creating responsive designs that adapt to various device sizes.