Skip to content

Introduction to Media Queries

Lecture notes: why responsiveness matters, @media syntax, media types and logical operators

HTML & CSS — Course for BeginnersLecture 12 of 195 min34 views

1. Why responsive markup is needed

The same site at the same address must render correctly on different devices: desktop, tablet, smartphone. The variety of screen sizes and their aspect ratios (4:3, 16:9, 18:9, 21:9) makes responsiveness mandatory.

More than 60% of modern site traffic comes from mobile devices.

Two approaches to responsiveness

ApproachEssence
Separate mobile versionA separate domain (for example, m.example.com). For large services with a lot of functionality
Responsive markupOne domain, one HTML — different styles for different screen widths through media queries
Tip

A separate mobile version makes sense only for very large portals where mobile users do not need a significant part of the functionality. In most cases — use responsive markup with media queries.

2. Desktop First vs Mobile First

ApproachPrincipleMedia query
Desktop FirstWe start with the desktop version, then remove the excess for narrower screensmax-width
Mobile First (recommended)We start with the mobile version, then add elements for wider screensmin-width
Tip

Mobile First is better, because the mobile version is the base CSS without media queries, while the tablet and desktop versions supplement it. This means less CSS code and faster loading for mobile users.

3. Device types in media queries

TypeDescription
screenDevices with a screen (monitors, tablets, smartphones)
printPrinting
speechVoice devices (screen readers)
allAll device types (default)

4. Loading separate CSS files for different devices

The media attribute of the <link> tag allows loading different CSS files depending on the device type or screen width:

HTML
<!-- Styles for screens only -->
<link rel="stylesheet" href="screen.css" media="screen">

<!-- Styles for printing only -->
<link rel="stylesheet" href="print.css" media="print">

<!-- Styles for tablets and wider (Mobile First) -->
<link rel="stylesheet" href="tablet.css" media="(min-width: 768px)">
Tip

The advantage of splitting files: a mobile device will load only its own CSS, not all the code for desktop and tablet. This saves traffic and speeds up loading.

5. CSS for printing

The printed version of a page needs a different design:

  • Remove the menu, ads, sidebars, background images
  • Text color — black (colored text becomes pale or invisible when printed)
  • Browsers usually remove the background automatically when printing to save ink
  • Units of measurement on paper are absolute (cm, mm) — the sheet size is always known
CSS
/* print.css — styles for printing */
.navigation,
.sidebar,
.advertisement {
    display: none;
}

body {
    color: #000;
    background: #fff;
}

6. Media queries in CSS

The basic syntax of a media query inside a CSS file:

CSS
/* Styles are applied at a width of 500px and wider */
@media (min-width: 500px) {
    .box {
        background: red;
    }
}

Inside @media we write ordinary CSS rules. They are applied only when the media query condition is met.

ConditionWhen it triggersApproach
min-width: 768pxScreen width from 768px and upMobile First
max-width: 767pxScreen width up to 767px and belowDesktop First

7. Common breakpoints

Commonly accepted width ranges for different device types:

DeviceWidth
Mobile (portrait)up to 767px
Tablet768px – 1024px
Small desktop1025px – 1439px
Large desktopfrom 1440px
Attention

These are approximate values. Specific breakpoints are determined by the layout design, not by a list of devices. Build your markup so that the content looks good at any width, not only on specific devices.

8. Device Pixel Ratio — pixel density

Modern mobile screens have a physical resolution 2–4 times higher than "CSS pixels".

DevicePhysical resolutionCSS resolutionDPR
iPhone SE640 × 1136320 × 5682
iPhone 14 Pro1179 × 2556393 × 8523

Thanks to DPR, a small screen can display smooth fonts and crisp images without shrinking elements to an unreadable size.

Tip

Media queries use CSS pixels, not physical ones. That is why min-width: 768px will trigger the same way both on a tablet with DPR 2 and on a monitor with DPR 1.

9. Testing responsive markup

The order of checks — from the fastest to the most reliable:

  1. Main browser — narrow the window, check visually
  2. Other browsers on the computer (Firefox, Safari, Edge)
  3. DevTools → Toggle Device Toolbar (F12 → devices icon) — emulation of different screen sizes
  4. Real devices — the most reliable method
Attention

Emulation ≠ a real device. The difference: the browser's system elements (address bar, navigation panel) take up space; font rendering differs; some CSS properties have bugs on specific devices. The more important the project — the more real devices you need to check.

Remote debugging on real devices

  • Chrome: connect an Android device via USB → chrome://inspect
  • Safari: connect an iPhone/iPad via USB → Safari → Develop → [device]

10. Mobile First example with split files

HTML
<!-- Base styles — mobile version (without media queries) -->
<link rel="stylesheet" href="mobile.css">

<!-- Tablet: from 768px -->
<link rel="stylesheet" href="tablet.css" media="(min-width: 768px)">

<!-- Desktop: from 1025px -->
<link rel="stylesheet" href="desktop.css" media="(min-width: 1025px)">

The styles are applied cascadingly: on desktop mobile.css + tablet.css + desktop.css all work. Each subsequent file supplements or overrides the previous one.

11. Summary table

TopicKey point
Media queries@media (min-width: ...) or @media (max-width: ...) — conditional application of styles
Mobile FirstBase CSS is the mobile version; media queries add styles for wider screens
Device typesscreen, print, speech, all
Splitting CSSThe media attribute in <link> — loads only the needed CSS
Breakpoints~768px (tablet), ~1025px (desktop) — approximate, dictated by the design
Device Pixel RatioThe ratio of physical pixels to CSS pixels; media queries work with CSS pixels
TestingBrowsers → DevTools → real devices; emulation ≠ reality

More interesting articles

Read the blog
01 / 06