CSS Viewport Module Level 1

W3C First Public Working Draft,

More details about this document
This version:
https://www.w3.org/TR/2024/WD-css-viewport-1-20240125/
Latest published version:
https://www.w3.org/TR/css-viewport-1/
Editor's Draft:
https://drafts.csswg.org/css-viewport/
Previous Versions:
History:
https://www.w3.org/standards/history/css-viewport-1/
Feedback:
CSSWG Issues Repository
CSSWG GitHub
Inline In Spec
Editors:
Florian Rivoal (Invited Expert)
(Mozilla)
Former Editors:
Matt Rakow (Microsoft)
(Opera Software)
(Adobe Systems)
(Opera Software)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This specification provides a way for an author to specify, in CSS, the size, zoom factor, and orientation of the viewport that is used as the base for the initial containing block.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

This document was published by the CSS Working Group as a First Public Working Draft using the Recommendation track. Publication as a First Public Working Draft does not imply endorsement by W3C and its Members.

This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-viewport” in the title, like this: “[css-viewport] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list [email protected].

This document is governed by the 03 November 2023 W3C Process Document.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

1. Introduction

This section is not normative.

CSS 2.1 [CSS21] specifies an initial containing block for continuous media that has the dimensions of the viewport. Since the viewport is generally no larger than the display, devices with smaller displays such as phones or tablets typically present a smaller viewport than larger devices like desktop or laptops.

Unfortunately, many documents have historically been designed against larger viewports and exhibit a variety of bugs when viewed in smaller viewports. These include unintended layout wrapping, clipped content, awkward scrollable bounds, and script errors. To avoid these issues, mobile browsers generally use a fixed initial containing block width that mimics common desktop browser window size (typically 980-1024px). The resulting layout is then scaled down to fit in the available screen space.

Although this approach mitigates the issues mentioned above, the downscaling means the CSS pixel size will be smaller than recommended by CSS 2.1. Users will likely need to zoom on the content to view it comfortably.

This mitigation is unnecessary for sites that have been designed to work well on small viewports.

This specification is written from an implementation-centric point of view, making it arguably difficult to read. Significant editorial work may be needed to make it more understandable to different audiences. It also should clarify which viewport is referred to by various js APIs. See this blog post by ppk for a good discussion of these issues.

Various issues about this specification and related specifications are listed in this report.

2. The viewport

In CSS 2.1 a viewport is a feature of a user agent for continuous media and used to establish the initial containing block for continuous media. For paged media, the initial containing block is based on the page area. The page area can be set through @page rules.

This specification introduces a way of overriding the size of the viewport provided by the user agent (UA). Because of this, we need to introduce the difference between the initial viewport and the actual viewport.

initial viewport
This refers to the viewport before any UA or author styles have overridden the viewport given by the window or viewing area of the UA. Note that the initial viewport size will change with the size of the window or viewing area.
actual viewport
This is the viewport you get after processing the viewport <meta> tag.

Make actual viewport the layout viewport, define visual viewport.

When the actual viewport cannot fit inside the window or viewing area, either because the actual viewport is larger than the initial viewport or the zoom factor causes only parts of the actual viewport to be visible, the UA should offer a scrolling or panning mechanism.

It is recommended that initially the upper-left corners of the actual viewport and the window or viewing area are aligned if the base direction of the document is ltr. Similarly, that the upper-right corners are aligned when the base direction is rtl. The base direction for a document is defined as the computed value of the direction property for the first <BODY> element of an HTML or XHTML document. For other document types, it is the computed direction for the root element.

3. Viewport <meta> element

Specify me

3.1. Properties

The recognized properties in the viewport <meta> element are:

3.2. Parsing algorithm

Below is an algorithm for parsing the content attribute of the <meta> tag produced from testing Safari on the iPhone. The testing was done on an iPod touch running iPhone OS 4. The UA string of the browser: "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7". The pseudo code notation used is based on the notation used in [Algorithms].

The whitespace class contains the following characters (ascii):

The recognized separator between property/value pairs is comma for the Safari implementation. Some implementations have supported both commas and semicolons. Because of that, existing content use semicolons instead of commas. Authors should be using comma in order to ensure content works as expected in all UAs, but implementors may add support for both to ensure interoperability for existing content.

The separator class contains the following characters (ascii), with comma as the preferred separator and semicolon as optional:

Parse-Content(S)
i ← 1
while ilength[S]
  do while ilength[S] and S[i] in [whitespace, separator, '=']
    do ii + 1
  if ilength[S]
    then iParse-Property(S, i)

Parse-Property(S, i)
starti
while ilength[S] and S[i] not in [whitespace, separator, '=']
  do ii + 1
if i > length[S] or S[i] in [separator]
  then return i
property-nameS[start .. (i - 1)]
while ilength[S] and S[i] not in [separator, '=']
  do ii + 1
if i > length[S] or S[i] in [separator]
  then return i
while ilength[S] and S[i] in [whitespace, '=']
  do ii + 1
if i > length[S] or S[i] in [separator]
  then return i
starti
while ilength[S] and S[i] not in [whitespace, separator, '=']
  do ii + 1
property-valueS[start .. (i - 1)]
Set-Property(property-name, property-value)
return i

Set-Property matches the listed property names case-insensitively. The property-value strings are interpreted as follows:

  1. If a prefix of property-value can be converted to a number using strtod, the value will be that number. The remainder of the string is ignored.
  2. If the value can not be converted to a number as described above, the whole property-value string will be matched with the following strings case-insensitively: yes, no, device-width, device-height
  3. If the string did not match any of the known strings, the value is unknown.

3.3. extend-to-zoom

Specify extend-to-zoom behavior by the viewport meta tag

3.4. interactive-widget

Move the definition of visual viewport from CSSOM-View to this spec.

The interactive-widget property specifies the effect that interactive UI widgets have on the page’s viewports. It defines whether widgets overlay a given viewport or whether the viewport is shrunken so that it remains fully visible while the widget is showing. Interactive UI widgets are transient user agent or operating system UI through which a user can provide input.

The most common such UI widget is a virtual keyboard.

The following is a list of valid values for interactive-widget and the associated viewport-resizing behavior:

overlays-content
Interactive UI widgets MUST NOT resize the initial viewport nor the visual viewport. The user agent must perform the same steps as when VirtualKeyboard.overlaysContent is set to true.
resizes-content
Interactive UI widgets MUST resize the initial viewport by the interactive widget.
Since the visual viewport’s size is derived from the initial viewport, resizes-content will cause a resize of both the initial and visual viewports.
resizes-visual
Interactive UI widgets MUST resize the visual viewport but MUST NOT resize the initial viewport.

If no value, or an invalid value, is set for interactive-widget, the behavior implied by resizes-visual is used as the default.

To resize a viewport by an interactive widget, subtract from it the intersection of the viewport rect with the widget’s OS reported bounding rect. In cases where this would result in a non-rectangular viewport, the behavior is user agent defined.

Some examples where the result would non rectangular: a floating or split keyboard, or a keyboard that occupies only part of the viewport.

3.4.1. Interaction with virtualKeyboard.overlaysContent

[VIRTUAL-KEYBOARD] provides an imperative API to apply the overlays-content behavior via the VirtualKeyboard.overlaysContent attribute. This attribute shadows the value set to interactive-widget, namely:

When VirtualKeyboard.overlaysContent is set to true, the UA MUST ignore any value set to interactive-widget when determining the resizing behavior of interactive widgets.

When VirtualKeyboard.overlaysContent is set to false, the UA MUST use the value set to interactive-widget, or the default behavior if a value is not set, when determining the resizing behavior of interactive widgets.

Getting the value of VirtualKeyboard.overlaysContent MUST return only the value previously set to it.

That is, unless previously set, VirtualKeyboard.overlaysContent returns false even if interactive-widget=overlays-content is set via the <meta> tag.

Appendix A. Changes

This appendix is informative.

Since the 29 March 2016 Working Draft

Since the 15 September 2011 First Public Working Draft.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Partial implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at https://www.w3.org/Style/CSS/Test/. Questions should be directed to the [email protected] mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-PAGE-3]
Elika Etemad. CSS Paged Media Module Level 3. 14 September 2023. WD. URL: https://www.w3.org/TR/css-page-3/
[CSS-VIEWPORT]
CSS Viewport Module Level 1. Editor's Draft. URL: https://drafts.csswg.org/css-viewport/
[CSS-WRITING-MODES-3]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 3. 10 December 2019. REC. URL: https://www.w3.org/TR/css-writing-modes-3/
[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. REC. URL: https://www.w3.org/TR/CSS21/
[CSS3-CONDITIONAL]
David Baron; Elika Etemad; Chris Lilley. CSS Conditional Rules Module Level 3. 13 January 2022. CR. URL: https://www.w3.org/TR/css-conditional-3/
[CSSOM-VIEW-1]
Simon Pieters. CSSOM View Module. 17 March 2016. WD. URL: https://www.w3.org/TR/cssom-view-1/
[MEDIAQUERIES-5]
Dean Jackson; et al. Media Queries Level 5. 18 December 2021. WD. URL: https://www.w3.org/TR/mediaqueries-5/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119
[VIRTUAL-KEYBOARD]
Anupam Snigdha. VirtualKeyboard API. 5 May 2022. WD. URL: https://www.w3.org/TR/virtual-keyboard/

Informative References

[Algorithms]
Thomas H. Cormen; et al. Introduction to Algorithms, Second Edition, MIT Press.

Issues Index

This specification is written from an implementation-centric point of view, making it arguably difficult to read. Significant editorial work may be needed to make it more understandable to different audiences. It also should clarify which viewport is referred to by various js APIs. See this blog post by ppk for a good discussion of these issues.
Various issues about this specification and related specifications are listed in this report.
Make actual viewport the layout viewport, define visual viewport.
Specify me
Specify extend-to-zoom behavior by the viewport meta tag
Move the definition of visual viewport from CSSOM-View to this spec.