Code Snippets
/

CSS Media Query Recipes (Retina, DPI, Color Depth, Aspect Ratio)

CSS Media Query Recipes (Retina, DPI, Color Depth, Aspect Ratio)

Media queries do far more than `min-width`. This snippet collects the recipes you reach for in real projects: viewport breakpoints, high-DPI / retina screens, color-depth and color-gamut gating, user preference queries (`prefers-color-scheme`, `prefers-reduced-motion`), and aspect-ratio plus orientation for video and game UIs. Drop the ones you do not need; keep the rest as a starting point.

CSS
Medium
4 snippets
css-media-queries
css-responsive-design
css-units

874 views

13

/* Base styles target the smallest screens (phones in portrait). */
.container {
    padding: 1rem;
    max-width: 100%;
}

/* Tablet and up: 640px is a common min-width breakpoint for two-column layouts. */
@media (min-width: 640px) {
    .container {
        padding: 1.5rem;
        max-width: 600px;
        margin-inline: auto;
    }
}

/* Small laptop and up: bump to wider container, tighter line-height. */
@media (min-width: 1024px) {
    .container {
        max-width: 960px;
    }
    body {
        line-height: 1.6;
    }
}

/* Large desktop. */
@media (min-width: 1440px) {
    .container {
        max-width: 1200px;
    }
}

/* Avoid mixing min-width and max-width in the same stylesheet; pick one
   direction (mobile-first = min-width is the modern default) and stick to it. */

Mobile-first means your base CSS is the smallest screen and each @media (min-width: ...) block layers on richer styles for larger viewports. This avoids the override-fest that happens when you write desktop styles first and then max-width queries to undo them. Pick three or four breakpoints and stick with them; common starting points are 640px (small tablet), 1024px (small laptop), and 1440px (large desktop). Use rem and % for sizing inside the queries so a user changing the browser zoom or root font size still gets a comfortable layout.