Skip to content

Layout Features for Mobile Devices

Lecture notes: Retina displays, Device Pixel Ratio, the viewport meta tag, mobile browsers

HTML & CSS — Course for BeginnersLecture 14 of 198 min47 views

1. The Problem of Decorative Graphics on Retina Displays

Decorative images (icons, backgrounds) are inserted via CSS using background-image. On screens with a high pixel density (Retina), raster graphics may look blurry.

Physical vs CSS pixels

A CSS pixel is a virtual unit. One CSS pixel can correspond to 2, 3, or more physical screen pixels. The DPR (Device Pixel Ratio) factor defines this relationship.

SituationResult
File size = background-sizePerfect quality
File larger than background-sizeThe browser shrinks it — quality is preserved
File smaller than background-sizeThe browser stretches it — quality is lost (blur)
Example

Example: a 16×16 px icon, DPR = 2. The browser will try to render 32×32 physical pixels, but there is only enough information for 16×16 — blur appears.

2. The Brute-Force Solution — an Enlarged Image

The simplest approach is to prepare the image at 2× (or 3×) larger size and specify the desired background-size in CSS:

CSS
.icon {
    background-image: url("icon@2x.png");  /* 32×32 px file */
    background-size: 16px 16px;           /* display as 16×16 */
    background-repeat: no-repeat;
}
Attention

Drawbacks: the enlarged file weighs more; when the icon size changes you need to prepare a new image; for DPR 3 you need yet another variant.

3. Icon Font

An icon font is a font in which icons take the place of letters. Since a font is vector-based, the icons scale without any loss of quality.

Connecting it

CSS
@font-face {
    font-family: "MyIcons";
    src: url("fonts/icons.woff2") format("woff2");
    font-weight: normal;
    font-style: normal;
}

Using it via a pseudo-element

CSS
.link::before {
    content: "\e901";          /* Unicode code of the icon */
    font-family: "MyIcons";
    font-size: 16px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

Advantages

  • Size is changed via font-size — with no loss of quality
  • Color is changed via color (like regular text)
  • Ready-made icon sets (Font Awesome, Material Icons, etc.)

Drawbacks

  • An icon is a character (text), not graphics — semantically incorrect
  • You cannot color individual parts of an icon with different colors
  • An outdated approach — SVG has all the advantages without the drawbacks

4. SVG — the Modern Solution

SVG (Scalable Vector Graphics) is a vector graphics format. It scales without loss of quality because it describes shapes with formulas rather than pixels.

Two ways to use it

MethodHowColor changeAccess from JS
External filebackground-image: url("icon.svg")NoNo
InlineThe <svg> tag directly in the HTMLYes (via fill)Yes

SVG structure

HTML
<svg class="icon" viewBox="0 0 24 24">
    <path d="M12 2C6.48 2 2 6.48 2 12s..."/>
</svg>
  • <svg> — the container; viewBox sets the coordinate system
  • <path> — the outline of the shape; the d attribute describes the curves
  • fill — the fill color (an attribute or a CSS property)

Controlling the color of an inline SVG

CSS
/* Remove the fill attribute from <path> in the markup! */
.icon path {
    fill: #333;
}
.link:hover .icon path {
    fill: #0066cc;
}
Attention

If fill is set as an attribute in the <path fill="..."> tag, the CSS fill property will not override it (just like inline styles). Remove the attribute in order to control the color from CSS.

Size of an inline SVG

CSS
.icon {
    width: 24px;
    height: 24px;
}

5. Comparison of Approaches to Decorative Graphics

CriterionPNG @2xIcon fontSVG
Quality when scalingLimited by file sizeLosslessLossless
Color change in CSSNoYes (color)Yes (fill, inline only)
Multi-colorYesNoYes (separate <path>)
SemanticsGraphicsText (semantically incorrect)Graphics
Browser supportAllAllAll modern

6. Styling Forms on Mobile Devices

Mobile browsers have their own default styles for form controls — rounded corners on <input>, a non-standard appearance for <select>.

Resetting the system appearance

CSS
input, select, textarea, button {
    -webkit-appearance: none;
    -moz-appearance: none;
    border-radius: 0;
}
Attention

The appearance property without a prefix is not supported by all browsers. Always use both prefixes: -webkit-appearance and -moz-appearance.

Bringing back the arrow for select

After resetting appearance, the arrow will disappear from <select>. Add it via an SVG background:

CSS
select {
    background-image: url("arrow-down.svg");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 12px;
    padding-right: 30px;
}
Tip

Always set styles explicitly for form controls: border, border-radius, background, color, font-size. Do not rely on default styles — they differ across platforms.

7. Automatic Phone Number Detection on iOS

iOS Safari automatically recognizes numbers that resemble phone numbers and turns them into blue underlined links with the tel: protocol.

Why a simple style reset doesn't work

The browser does not change your tag — it wraps the text in <a href="tel:..."> inside your element. That is why styles for the parent element won't help.

Solution 1 — an attribute selector

CSS
a[href^="tel"] {
    color: inherit;
    text-decoration: none;
}

Solution 2 (better) — make it a link right away

HTML
<a href="tel:+380441234567">+38 (044) 123-45-67</a>

Style the link as you need — and it will look the same on both desktop and mobile devices.

Solution 3 — completely disabling auto-detection

HTML
<meta name="format-detection" content="telephone=no">
Attention

Be careful! This meta tag disables auto-detection on the entire page. Make sure all phone numbers are marked up as <a href="tel:..."> links manually.

8. Attribute Selectors

SyntaxDescriptionExample
[attr="value"]Exact matchinput[type="text"]
[attr^="value"]Starts with...a[href^="tel"]
[attr$="value"]Ends with...a[href$=".pdf"]
[attr*="value"]Contains a substringa[href*="example"]
[attr|="value"]Equals value or starts with value-[class|="button"]

Simplifying modifier classes with [class|="..."]

The [class|="button"] selector selects an element whose class equals button or starts with button-. This lets you write the base styles once for all variants:

CSS
/* Applies to class="button", class="button-primary", class="button-large" */
[class|="button"] {
  padding: 1em 2em;
  border: none;
  border-radius: 4px;
}

/* A specific modifier */
.button-accent {
  background-color: hotpink;
}

Instead of <div class="button button-primary">, you can write just <div class="button-primary"> — the base styles are picked up automatically.

Tip

Performance: attribute selectors are slower than class selectors — the browser has to analyze the string. Use them only for specific cases (mass styling of tel:, mailto: links, etc.).

9. Testing Layouts with Dynamic Content

A layout must work with any amount of content — short text, long text, a varying number of elements.

What to test

  • Lots of text — does the layout break?
  • Little text — do empty gaps appear?
  • A varying number of columns / cards
  • Absence of images
Attention

Do not use a fixed margin-top for vertical centering! When the content changes (more/less text), the alignment will break. Use align-self: center or margin: auto in flex.

10. Input Types for Mobile Devices

HTML5 added new types for <input> that change the virtual keyboard on mobile devices:

TypeKeyboardAdditional features
type="text"Regular text—
type="search"Text with a "Search" buttonAn X to clear the field, a search button instead of Enter
type="email"Latin with an @ symbolFormat check with required
type="tel"Numeric with +, -, #, *Convenient number entry
type="number"NumericIncrement/decrement arrows, the step attribute

These types affect only the virtual keyboards. A computer's physical keyboard will not change.

Built-in validation with required

When required is combined with a typed input, the browser checks not only that the field is filled in but also its format:

HTML
<input type="email" required>
<!-- Will check for an @ and a domain -->

<input type="tel" required>
<!-- Will check that the field is filled in -->

The downside: the appearance and text of the messages differ between browsers and cannot be customized. For consistent behavior you need validation via JavaScript.

inputmode — an alternative to type="number"

type="number" has UX problems: the increment/decrement arrows get in the way, and an accidental scroll changes the value. A better alternative is a combination of type="text" with the inputmode attribute:

HTML
<input
  type="text"
  inputmode="numeric"
  pattern="[0-9]*"
  placeholder="Enter the code"
>

inputmode="numeric" brings up the numeric keyboard on mobile, while pattern provides validation — without unnecessary arrows or accidental changes while scrolling.

Tip

When to use what: type="number" — for fields where arrows are needed (product quantity, a step). inputmode="numeric" — for codes, PINs, and card numbers, where arrows get in the way.

11. The pattern Attribute for Validation

The pattern attribute lets you specify a regular expression (regex) to check the entered data:

HTML
<!-- Digits only, at least 3 characters -->
<input type="text" pattern="[0-9]{3,}" required>

<!-- International phone format -->
<input type="tel" pattern="\+[0-9]{10,13}" required>

HTML validation (required, pattern) is easily bypassed via DevTools. For reliable checking you need JavaScript on the client and a check on the server.

12. Summary Table

TopicKey points
Retina and DPRCSS pixel ≠ physical pixel; DPR = physical / CSS; raster on Retina — becomes blurry
Icon fontIcons as font characters; scales losslessly; an outdated approach
SVGVector graphics; the best solution; inline — allows fill and JS
Forms on mobile-webkit-appearance: none + explicit styles; the select arrow via an SVG background
Phones on iOSAuto-detection; a[href^="tel"] for styles; best — make it <a href="tel:..."> right away
Attribute selectors[attr="val"], [attr^="val"], [attr$="val"], [attr*="val"], [attr|="val"]
Input typessearch, email, tel, number — change the mobile keyboard
inputmodeinputmode="numeric" — a numeric keyboard without arrows; better than type="number" for codes and PINs
patternRegex validation in HTML; bypassed via DevTools; a server-side check is needed

More interesting articles

Read the blog
01 / 06