Skip to content

Media Features

Lecture notes: a mobile design checklist, the width, height, resolution, hover and pointer media features

HTML & CSS — Course for BeginnersLecture 13 of 199 min31 views

1. Mobile design acceptance checklist

The markup developer is the "eyes" between the designer and the developer. Before starting the markup, it's worth reviewing the layout and giving feedback to the designer.

Differences of the mobile interface

  • No hover — a finger can't "hover" over an element without tapping
  • Larger click area — the minimum size of an interactive element is ~48×48 px
  • No keyboard — entering text is harder, it's better to use checkboxes, radio buttons, toggles
  • Gestures — swipe, zoom; but gestures are rarely used on websites

Simulating hover on mobile

On iOS, simulating hover requires an interactive element — <a> or <button>. A plain <span> won't work. On Android it works with any element.

Attention

Don't rely on hover! Important content hidden behind hover (product descriptions, tooltips) becomes inaccessible on mobile. It's better to show the information right away or use tapping.

What to check in the layout

  • Size of interactive elements (min. 48×48 px)
  • No horizontal dropdown lists
  • Text readability without zooming
  • Sufficient spacing between buttons / links
  • Elements with two values → checkbox or toggle
  • Few options → radio buttons instead of a dropdown

2. meta viewport — configuring the viewport

Without the viewport meta tag, a mobile browser renders the site as a desktop one (with a width of ~980 px), and the user has to zoom manually.

HTML
<meta name="viewport" content="width=device-width, initial-scale=1.0">
ParameterValueDescription
widthdevice-widthViewport width = the device's recommended width (accounting for DPR)
initial-scale1.0Initial zoom level (1.0 = 100%)
minimum-scalenumberMinimum allowed zoom level
maximum-scalenumberMaximum allowed zoom level
user-scalableyes / noAllow/disallow zooming by the user
Attention

Don't disable zooming! user-scalable=no and maximum-scale=1 prevent users from enlarging small text or examining images. This harms the site's accessibility.

Tip

Without a viewport, media queries won't work correctly on mobile — the browser will assume a width of ~980 px instead of the real 375–414 px.

3. Media queries in CSS — extended syntax

A media query in CSS can be combined with a device type:

CSS
@media screen {
    body {
        font-size: 14px;
    }
}

A more complex query — with a width condition:

CSS
@media (max-width: 480px) {
    /* Styles for screens up to and including 480px */
}

Strategy: fewer media queries is better

Instead of two paired queries (up to 480 and from 481) — keep the base CSS for mobile and one query from 481:

CSS
/* Base styles — always apply (mobile) */
.sidebar,
.content {
    width: 100%;
}

/* From 481px — columns are added */
@media (min-width: 481px) {
    .content  { width: 75%; }
    .sidebar { width: 25%; }
}

4. Critical CSS

Critical CSS is the minimal set of styles needed to render the site's first screen. It's embedded directly into a <style> inside <head>.

Why?

  • An external CSS file takes time to load
  • Inline CSS renders instantly along with the page
  • The user sees the first-screen content before the main CSS loads

What affects speed

FactorRecommendation
Number of filesFewer files — fewer server requests
File sizeMinification, image optimization
CSS complexitySimpler selectors — faster rendering
Deferred loadingLazy loading for images below the first screen
Tip

In practice, critical CSS is generated automatically using build tools (Webpack, Vite, etc.). Doing it manually is impractical.

5. The em unit — the current element's font size

1em = the current element's font-size value. If an element has no explicit font-size, the value is inherited from the parent up the chain to the root element.

CSS
.box {
    font-size: 20px;
    width: 10em;    /* 10 × 20 = 200px */
    padding: 1em;   /* 1 × 20 = 20px */
}
Attention

Be careful with em! If you move a widget into a container with a different font-size, all sizes in em will change proportionally. This can lead to an unexpected enlargement or reduction of the element.

6. The rem unit — the root element's font size

1rem = the font-size value of the <html> element (not <body>). Unlike em, rem doesn't depend on the nesting context.

CSS
html {
    font-size: 10px;   /* 1rem = 10px, easy to calculate */
}

.box {
    width: 30rem;    /* 300px */
    padding: 1.5rem; /* 15px */
}

When rem is convenient

  • Version for the visually impaired — we change only the font-size on html, and everything scales
  • Global scaling — if the client asks to "shrink everything by 1.5×", it's enough to change a single value
Tip

Technique: html { font-size: 10px; } — then a value from the layout (for example, 961px) is simply divided by 10 → 96.1rem.

7. Where to use which units

UnitTypeWhere to use
pxRelativeThe main unit — everything that doesn't fall into the other categories
%RelativeColumns (25%, 33%, 50%, 75%, 100%) — only when the division is visually apparent
emRelativeRarely; when there's a clear proportion relative to the font size
remRelativeVersion for the visually impaired, global scaling
vw, vhRelativeResponsiveness (introduced in the next lessons)
cm, mm, inAbsoluteOnly for print (@media print)
ptAbsolutePrint media, typography (rarely on the web)
Attention

Don't fudge percentages! If you end up with values like 41.5% or 21.4% — you're probably "fudging" the layout. Such code is hard to maintain. Use percentages only for obvious proportions (1/2, 1/3, 1/4).

8. Useful CSS shorthands

inset — a shorthand for positioning

The inset property replaces setting top, right, bottom, left all at once:

CSS
.overlay {
  position: absolute;

  /* Instead of top: 0; right: 0; bottom: 0; left: 0; */
  inset: 0;
}

The syntax is analogous to margin: one value — for all sides, two — for vertical/horizontal, four — for each side separately.

Logical properties: margin-inline and padding-block

Logical properties set spacing relative to the text direction, not the physical sides of the screen:

Logical propertyPhysical equivalent (for LTR)
margin-inlinemargin-left + margin-right
padding-blockpadding-top + padding-bottom
margin-inline-startmargin-left (for LTR) / margin-right (for RTL)
CSS
.card {
  margin-inline: auto;       /* Horizontal centering */
  padding-block: 2rem;       /* Top and bottom spacing */
}

place-items and place-content — quick centering

In a Grid container, place-items is a shorthand for align-items + justify-items, and place-content — for align-content + justify-content:

CSS
.container {
  display: grid;
  place-items: center;   /* Centers content on both axes */
}
Tip

The shortest centering: display: grid; place-items: center; — two lines for perfect centering of elements both horizontally and vertically.

9. Don't over-optimize with em

A common mistake is to see that a heading is 2.5 times larger than the text and replace font-size with 2.5em. It works until the designer changes one size without the other.

Bad optimization

CSS
.service p {
    font-size: 16px;
}
.service h2 {
    font-size: 2.5em;  /* 40px, but depends on the parent's font-size */
}

Better — explicit values

CSS
.service p {
    font-size: 16px;
}
.service h2 {
    font-size: 40px;
}

@media (max-width: 768px) {
    .service p  { font-size: 14px; }
    .service h2 { font-size: 28px; }
}
Tip

Rule: values in CSS should match what you see in the layout. If the layout says 40px — write 40px, not 2.5em. That way it's easier to find and change.

10. Margin collapsing

In the normal flow (without flex), the vertical margins of adjacent elements don't add up, they collapse — the larger of the two remains.

CSS
.block-1 {
    margin-bottom: 70px;
}
.block-2 {
    margin-top: 30px;
}
/* The distance between blocks = 70px (not 100px!) */
Attention

In a flex container, collapsing doesn't happen! The margins add up: 70 + 30 = 100px.

ContextVertical margins
Normal flowCollapse (the larger one remains)
Flex containerAdd up (the sum)
Grid containerAdd up (the sum)

11. Additional media features

Besides min-width / max-width, there are other media features:

FeatureExampleDescription
orientation@media (orientation: landscape)Landscape orientation of the device
orientation@media (orientation: portrait)Portrait orientation of the device
min-height@media (min-height: 600px)Minimum viewport height
max-height@media (max-height: 400px)Maximum viewport height
Tip

Most commonly used are min-width / max-width. Orientation — less often. Height — even less often, because the page scrolls vertically.

12. The aspect-ratio and orientation media features

aspect-ratio

Defines the width-to-height ratio of the display area (viewport). It's written as a fraction:

CSS
@media (min-aspect-ratio: 16/9) {
  /* Styles for wide screens */
}

It supports the min- and max- prefixes. To figure out which ratio is larger — convert the fraction to a decimal: 16/9 = 1.78, 16/10 = 1.6. So, min-aspect-ratio: 16/9 doesn't include 16:10 monitors.

Popular aspect ratios

RatioWhere it's found
4:3Old CRT monitors, designer monitors
16:9The most popular: Full HD, HD, 4K
3:2Microsoft Surface, some modern laptops
16:10MacBook Pro, some laptops (more height — handier for code)
21:9Ultrawide monitors, the cinematic format

orientation

Defines the orientation of the device. It takes two values:

  • portrait — vertical (height > width)
  • landscape — horizontal (width > height)
CSS
@media (orientation: landscape) {
  /* Styles for horizontal orientation */
}

Combined media query

For correct operation across different devices, a combination is often needed:

CSS
@media (min-width: 641px),
       (orientation: landscape) and (max-width: 640px) {
  /* Large screens OR horizontal phones */
  .menu {
    display: flex;
    flex-wrap: wrap;
  }
  .menu__item {
    width: 49%;
  }
}

Just orientation: landscape isn't enough — on a vertically held iPad (a large screen!) the elements would be one per row. And a vertically rotated monitor would also register as portrait.

13. Markup developer's tools

ToolPurpose
Chrome WorkspacesCSS changes in DevTools are saved to a file automatically — without manual copying
Pixel Perfect (extension)Overlays the layout image on top of the site for pixel-by-pixel comparison
letter-spacingAdjusts the spacing between letters (a negative value can be used for "tightening")
FigmaLets you copy partial CSS properties of elements, speeding up markup

14. Summary table

TopicKey points
meta viewportwidth=device-width, initial-scale=1.0 — a required tag for responsive markup
Media queries in CSS@media (min-width: ...) — Mobile First; @media (max-width: ...) — Desktop First
Critical CSSFirst-screen CSS is embedded in <style> for instant rendering
em1em = the current element's font-size; depends on context
rem1rem = the <html> element's font-size; independent of nesting
CSS Shorthandsinset (positioning), margin-inline/padding-block (logical), place-items (centering)
Margin collapsingIn the normal flow vertical margins collapse; in flex/grid — they don't
overflow: hiddenA crutch for horizontal scrolling — better to find the root cause
aspect-ratioThe width/height ratio of the viewport; supports min-/max-
orientationportrait (vertical) / landscape (horizontal)

More interesting articles

Read the blog
01 / 06