Skip to content

Media Queries

A media query allows you to define specific CSS styles based on conditions such as:

  • screen size (width/height),
  • device type (print, screen),
  • resolution,
  • dark or light mode, etc.

It is a fundamental tool of responsive design, to adapt the layout to all devices: desktop, tablet, mobile.


@media condition {
/* CSS styles here */
}

/* Style par défaut : Desktop */
body {
background: white;
font-size: 18px;
}
/* Tablette : max-width 768px */
@media (max-width: 768px) {
body {
background: #f0f0f0;
font-size: 16px;
}
}
/* Mobile : max-width 480px */
@media (max-width: 480px) {
body {
background: #e0e0e0;
font-size: 14px;
}
}

DeviceMax width (max-width)
Mobile480px to 600px
Tablet768px to 1024px
Laptop1024px to 1440px
Large desktop1440px+

@media (min-width: 1024px) { ... }
@media (max-width: 768px) { ... }
@media (orientation: portrait) { ... }
@media (orientation: landscape) { ... }
@media (prefers-color-scheme: dark) {
body {
background: black;
color: white;
}
}

@media (min-width: 600px) and (max-width: 900px) {
/* Styles for screens between 600px and 900px */
}

  • Use mobile-first media queries: base styles for small screens, then min-width for larger ones.
  • Avoid overloading everything in a single rule.
  • Keep a clear structure (desktop / tablet / mobile).
  • Use fluid units (em, %, rem) whenever possible.

.card {
width: 400px;
}
@media (max-width: 768px) {
.card {
width: 90%;
}
}

@media (max-width: 768px) {
/* styles here */
}

This is the most common form and the one most used today.

By default, screen is implied, so you can safely omit screen in 99% of cases.


@media screen and (max-width: 768px) {
/* styles here */
}

Here you specify that the rules apply only to screens (and not to printing, for example).


TypeDescription
allTous les types de médias (défaut)
screenÉcrans (ordinateurs, tablettes, etc.)
printImpression papier
speechSynthèse vocale (lecture d’écran)

  • If you only target screens, you can simply use @media (shorter, more readable).

  • If you want to exclude print or speech, explicitly use:

    @media screen and (max-width: 768px) { ... }

SyntaxRecommended?Notes
@media (...)✅ YESEnough in 99% of cases
@media screen and (...)✅ YESMore explicit, useful if you also handle print
@media print✅ YESFor print styles