A phone is the normal device for a gaming interface, and it is the hardest one. The screen is small, it rotates, parts of it are covered by the system's own UI, the only pointer is a thumb, the network is variable and the processor is a fraction of a laptop's. This guide covers what the game container and the controls around it must do to work under those conditions, with the CSS and browser APIs involved.
The responsive game container
The game frame should be a box whose size is derived from its container, never from fixed pixel dimensions. Two properties do most of the work. aspect-ratio keeps the box at the game's intended proportions as its width changes, and max-height keeps it from exceeding the viewport in landscape. Inside the box the canvas scales to fill it, and the game adjusts its internal resolution to match.
.game-frame {
width: 100%;
aspect-ratio: 16 / 9;
max-height: 100dvh;
background: #0a0a0a; /* poster or placeholder colour */
contain: layout paint; /* isolate layout work */
}
.game-frame canvas { width: 100%; height: 100%; display: block; }
Reserving the box before any script runs is what prevents a layout shift when the game boots. Declaring the aspect ratio in CSS, or width and height attributes on the poster image, gives the browser the information it needs at first paint.
Portrait versus landscape
Most phone sessions begin in portrait. Many games want landscape. There are three honest options: design the game to work in both, lock to one and show a rotate prompt, or let the frame adapt while the game picks a layout internally. Whichever you choose, the page must not break in the other orientation: no overflow, no controls under the keyboard, no frame taller than the screen.
CSS can switch layout with the orientation media feature, and JavaScript can observe it with matchMedia("(orientation: landscape)") or the screen.orientation API. Locking orientation programmatically is only possible in full-screen mode in most browsers, which is one reason full-screen matters below. Treat an orientation change as a resize: recompute the canvas resolution, reposition controls, and do it once, after the change settles, not on every intermediate event.
Viewport units and the address bar
Mobile browsers hide and show their toolbars as the user scrolls, which changes the visible height. The classic 100vh was defined against the largest viewport, so a frame set to 100vh overflows when the toolbar is visible. CSS now defines three families: svh for the small viewport (toolbars shown), lvh for the large viewport (toolbars hidden), and dvh for the dynamic viewport that tracks the current state. For a full-height game frame, 100dvh matches what the user sees; 100svh is the safe choice when you want no resizing at all. The CSS Values and Units specification defines them and MDN documents browser support.
Safe areas
Phones with a notch, a camera cut-out or a home-indicator bar reserve parts of the screen. By default the browser keeps page content out of those areas. A full-bleed game that wants to use the whole screen opts in with viewport-fit=cover in the viewport meta tag and then pads its controls back out of the unsafe zones with the env() function.
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
.controls {
padding-bottom: calc(12px + env(safe-area-inset-bottom));
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
}
The insets are zero on devices without cut-outs, so the rule is harmless where it is not needed. In landscape the left and right insets are the ones that matter, which is why the side controls in the diagram sit inside them.
Touch targets
WCAG 2.2 success criterion 2.5.8 (level AA) requires pointer targets of at least 24 by 24 CSS pixels unless an exception applies; the stricter 2.5.5 (level AAA) and most platform guidelines ask for about 44 by 44. For game controls that are tapped repeatedly and quickly, 44 pixels with spacing between targets is the practical minimum. The hit area can be larger than the visible glyph: pad the element rather than enlarging the icon.
Browsers add a delay to taps on pages that can be double-tap-zoomed. Setting touch-action: manipulation on controls removes it, and touch-action: none on the canvas itself hands all gestures to the game. Register scroll-related listeners as passive so the browser does not wait for JavaScript before scrolling the page around the game.
Sticky controls
In portrait, the controls that a player uses every few seconds belong in a fixed strip at the bottom of the screen, inside the safe area, not inside the game canvas where they would scale with it and shrink on small phones. Keep the strip's height constant so it never causes a shift, make it opaque so game motion behind it does not reduce the contrast of its labels, and ensure it does not overlap the frame's live area. In landscape the same controls usually move to a side column.
Mobile navigation
The page's own navigation should collapse into a single toggle on narrow screens, be operable by keyboard and screen reader (a real <button> with aria-expanded), and never cover the game while it is open. Do not use hover for anything essential; there is no hover on a touch screen. This site's header follows those rules and is a reasonable reference implementation at around ten lines of JavaScript.
Full-screen behaviour
The Fullscreen API lets a user gesture put the game container into full-screen mode, removing the browser chrome. It must be called from inside a click or tap handler, it can be refused, and the user can leave at any time with a system gesture, so the game must handle the fullscreenchange event and resize gracefully. Full-screen is also, in most browsers, the only context in which screen.orientation.lock() is permitted. Offer full-screen as a button, never force it, and make sure the non-full-screen layout works on its own.
Device orientation
Games that use the device's physical tilt read the deviceorientation event. On iOS this requires a permission prompt triggered from a user gesture, and on all platforms it consumes battery, so enable it only when the game mode needs it. Do not confuse it with screen orientation (portrait or landscape), which is a layout concern covered above.
Reduced bandwidth and low-powered devices
The field data for any gaming site includes users on cellular connections and on phones several years old. Two adaptive strategies are worth building in. The Network Information API (navigator.connection) exposes an effective connection type and a saveData flag in supporting browsers; a game can choose smaller atlases or skip optional audio when the connection is slow or data saving is on. And the game can measure its own frame time during the first seconds and drop its resolution scale, particle counts or post-processing when frames are being missed. Neither API is universally available, so treat them as hints and keep a sensible default.
Respect prefers-reduced-motion for the page's own animations and offer a reduced-effects option in the game itself. Both the page's and the game's heaviest effects should default off on devices that report a low deviceMemory or that drop frames in the first few seconds.
Checklist for the game frame
- Frame size comes from the container; aspect ratio declared in CSS; no fixed-pixel canvas.
- Box reserved before any script runs; poster image present with intrinsic dimensions.
- Works in both orientations; no horizontal overflow at 320 pixels wide.
- Full-height layouts use
dvhorsvh, notvh. viewport-fit=coverpaired withenv(safe-area-inset-*)padding on controls.- Controls at least 44 pixels, spaced, with
touch-action: manipulation. - Sticky control strip fixed in height, opaque, inside the safe area.
- Full-screen offered as a button; layout survives entering and leaving it.
- Reduced-motion respected; heavy effects adapt to slow connections and low-end devices.
The interactive checklist includes these as its Mobile section.
Sources
Primary documentation consulted for this guide. Thresholds and API behaviour were checked against these pages on 2026-11-02.
- Viewport meta element — MDN Web Docs
- env() CSS function (safe-area-inset-*) — MDN Web Docs
- CSS length units (including viewport units) — MDN Web Docs
- CSS Values and Units Module Level 4 — W3C
- Screen: orientation property — MDN Web Docs
- Fullscreen API — MDN Web Docs
- Using media queries — MDN Web Docs
- Window: matchMedia() — MDN Web Docs
- Touch events — MDN Web Docs
- Mobile touch controls (game development) — MDN Web Docs
- touch-action — MDN Web Docs
- Understanding SC 2.5.8: Target Size (Minimum) — W3C
- Accessible tap targets — web.dev
- Web Content Accessibility Guidelines (WCAG) 2.2 — W3C
- prefers-reduced-motion — MDN Web Docs
- Navigator: connection property (Network Information API) — MDN Web Docs
- Window: devicePixelRatio — MDN Web Docs