Fullscreen API

Living Standard — Last Updated

Participate:
GitHub whatwg/fullscreen (file an issue, open issues)
IRC: #whatwg on Freenode
Commits:
GitHub whatwg/fullscreen/commits
Snapshot as of this commit
@fullscreenapi

Abstract

The Fullscreen API standard defines an API for elements to display themselves fullscreen.

1. Terminology

This specification depends on the Infra Standard. [INFRA]

Most terminology used in this specification is from CSS, DOM, HTML, and Web IDL. [CSS] [DOM] [HTML] [WEBIDL]

A browsing context A is called a descendant browsing context of a browsing context B if and only if B is an ancestor browsing context of A.

2. Model

All elements have an associated fullscreen flag. Unless stated otherwise it is unset.

All iframe elements have an associated iframe fullscreen flag. Unless stated otherwise it is unset.

All documents have an associated fullscreen element. The fullscreen element is the topmost element in the document’s top layer whose fullscreen flag is set, if any, and null otherwise.

To fullscreen an element, set element’s fullscreen flag and add it to its node document’s top layer.

To unfullscreen an element, unset element’s fullscreen flag and iframe fullscreen flag (if any), and remove it from its node document’s top layer.

To unfullscreen a document, unfullscreen all elements, within document’s top layer, whose fullscreen flag is set.


To fully exit fullscreen a document document, run these steps:

  1. If document’s fullscreen element is null, terminate these steps.

  2. Unfullscreen elements whose fullscreen flag is set, within document’s top layer, except for document’s fullscreen element.

  3. Exit fullscreen document.

Whenever the removing steps run with an oldNode, run these steps:

  1. Let nodes be oldNode’s shadow-including inclusive descendants that have their fullscreen flag set, in shadow-including tree order.

  2. For each node in nodes, run these substeps:

    1. If node is its node document’s fullscreen element, exit fullscreen that document.

    2. Otherwise, unfullscreen node within its node document.

Whenever the unloading document cleanup steps run with a document, fully exit fullscreen document.


Fullscreen is supported if there is no previously-established user preference, security risk, or platform limitation.

An algorithm is allowed to request fullscreen if one of the following is true:

3. API

partial interface Element {
  Promise<void> requestFullscreen();
};

partial interface Document {
  [LenientSetter] readonly attribute boolean fullscreenEnabled;
  [LenientSetter] readonly attribute boolean fullscreen; // historical

  Promise<void> exitFullscreen();

  attribute EventHandler onfullscreenchange;
  attribute EventHandler onfullscreenerror;
};

partial interface DocumentOrShadowRoot {
  [LenientSetter] readonly attribute Element? fullscreenElement;
};
promise = element . requestFullscreen()

Displays element fullscreen and resolves promise when done.

document . fullscreenEnabled

Returns true if document has the ability to display elements fullscreen and fullscreen is supported, or false otherwise.

promise = document . exitFullscreen()

Stops document’s fullscreen element from being displayed fullscreen and resolves promise when done.

document . fullscreenElement

Returns document’s fullscreen element.

shadowroot . fullscreenElement

Returns shadowroot’s fullscreen element.

A fullscreen element ready check for an element element returns true if all of the following are true, and false otherwise:

The requestFullscreen() method, when invoked, must run these steps:

  1. Let pending be the context object.

  2. Let error be false.

  3. Let promise be a new promise.

  4. If any of the following conditions are false, then set error to true:

  5. Return promise, and run the remaining steps in parallel.

  6. If error is false: Resize pending’s top-level browsing context’s active document’s viewport’s dimensions to match the dimensions of the screen of the output device. Optionally display a message how the end user can revert this.

  7. As part of the next animation frame task, run these substeps:

    1. If either error is true or the fullscreen element ready check for pending returns false, fire an event named fullscreenerror on pending’s node document, reject promise with a TypeError exception, and terminate these steps.

    2. Let fullscreenElements be an ordered set initially consisting of pending.

    3. While the first element in fullscreenElements is in a nested browsing context, prepend its browsing context container to fullscreenElements.

    4. Let eventDocs be an empty list.

    5. For each element in fullscreenElements, in order, run these subsubsteps:

      1. Let doc be element’s node document.

      2. If element is doc’s fullscreen element, terminate these subsubsteps.

        No need to notify observers when nothing has changed.

      3. Otherwise, append doc to eventDocs.

      4. If element is pending and pending is an iframe element, then set element’s iframe fullscreen flag.

      5. Fullscreen element within doc.

    6. For each doc in eventDocs, in order, fire an event named fullscreenchange on doc.

    7. Resolve promise with undefined.

    Animation frame task is not really defined yet, including relative order within that task, see bug 26440.

    Implementations with out-of-process browsing contexts are left as an exercise to the reader. Input welcome on potential improvements.

The fullscreenEnabled attribute’s getter must return true if the context object is allowed to use the feature indicated by attribute name allowfullscreen and fullscreen is supported, and false otherwise.

The fullscreen attribute’s getter must return false if context object’s fullscreen element is null, and true otherwise.

Use the fullscreenElement attribute instead.

The fullscreenElement attribute’s getter must run these steps:

  1. If the context object is a shadow root and its host is not connected, then return null.

  2. Let candidate be the result of retargeting fullscreen element against the context object.

  3. If candidate and the context object are in the same tree, then return candidate.

  4. Return null.

To collect documents to unfullscreen given doc, run these steps:

  1. If doc’s top layer consists of more than a single element that has its fullscreen flag set, return the empty set.

  2. Let docs be an ordered set consisting of doc.

  3. While docs’s last document has a browsing context container whose node document’s top layer consists of a single element that has its fullscreen flag set and does not have its iframe fullscreen flag set (if any), append that node document to docs.

  4. Return docs.

To exit fullscreen a document doc, run these steps:

  1. Let promise be a new promise.

  2. If doc is not fully active or doc’s fullscreen element is null, then reject promise with a TypeError exception and return promise.

  3. Let resize be false.

  4. Let docs be the result of collecting documents to unfullscreen given doc.

  5. Let topLevelDoc be doc’s top-level browsing context’s active document.

  6. If topLevelDoc is in docs, then set resize to true.

  7. Return promise, and run the remaining steps in parallel.

  8. If resize is true, resize topLevelDoc’s viewport to its "normal" dimensions.

  9. As part of the next animation frame task, run these substeps:

    1. Let exitDocs be the result of collecting documents to unfullscreen given doc.

    2. If resize is true and topLevelDoc is not in exitDocs, fully exit fullscreen topLevelDoc, reject promise with a TypeError exception, and terminate these steps.

    3. If exitDocs is the empty set, append doc to exitDocs.

    4. If exitDocs’s last document has a browsing context container, append that browsing context container’s node document to exitDocs.

    5. Let descendantDocs be an ordered set consisting of doc’s descendant browsing contexts' active documents whose fullscreen element is non-null, if any, in reverse tree order.

    6. For each descendantDoc in descendantDocs, in order, unfullscreen descendantDoc.

    7. For each exitDoc in exitDocs, in order, unfullscreen exitDoc’s fullscreen element.

    8. For each descendantDoc in descendantDocs, in order, fire an event named fullscreenchange on descendantDoc.

    9. For each exitDoc in exitDocs, in order, fire an event named fullscreenchange on exitDoc.

    10. Resolve promise with undefined.

    This results in events being fired from the innermost to the outermost document.

The exitFullscreen() method, when invoked, must return the result of running exit fullscreen on the context object.


The following are the event handlers (and their corresponding event handler event types) that must be supported on documents as attributes:

event handler event handler event type
onfullscreenchange fullscreenchange
onfullscreenerror fullscreenerror

4. UI

User agents are encouraged to implement native media fullscreen controls in terms of requestFullscreen() and exitFullscreen().

If the end user instructs the user agent to end a fullscreen session initiated via requestFullscreen(), fully exit fullscreen the top-level browsing context’s active document.

5. Rendering

This section is to be interpreted equivalently to the Rendering section of HTML. [HTML]

Long term CSS will define the top layer concept and its associated ::backdrop pseudo-element as part of CSS' stacking context model. Patching CSS as done here is sketchy as hell.

5.1. New stacking layer

This specification introduces a new stacking layer to the Elaborate description of Stacking Contexts of CSS 2.1. It is called the top layer, comes after step 10 in the painting order, and is therefore rendered closest to the user within a viewport. Each document has one associated viewport and therefore also one top layer. [CSS]

The terminology used in this and following subsection attempts to match CSS 2.1 Appendix E.

The top layer consists of an ordered set of elements, rendered in the order they have been added to the set. The last element added is rendered closest to the user.

The z-index property has no effect in the top layer.

Each element and ::backdrop pseudo-element in a top layer has the following characteristics:

To add an element to a top layer, add, or move if already present, element on top of top layer.

To remove an element from a top layer, remove element from top layer.

5.2. ::backdrop pseudo-element

Each element in a top layer has a ::backdrop pseudo-element. This pseudo-element is a box rendered immediately below the element (and above the element before the element in the set, if any), within the same top layer.

The ::backdrop pseudo-element can be used to create a backdrop that hides the underlying document for an element in a top layer (such as an element that is displayed fullscreen).

It does not inherit from any element and is not inherited from. No restrictions are made on what properties apply to this pseudo-element either.

5.3. :fullscreen pseudo-class

The :fullscreen pseudo-class must match any element element for which one of the following conditions is true:

This makes it different from the fullscreenElement API, which returns the topmost fullscreen element.

5.4. User-agent level style sheet defaults

@namespace "http://www.w3.org/1999/xhtml";

*|*:not(:root):fullscreen {
  position:fixed !important;
  top:0 !important; right:0 !important; bottom:0 !important; left:0 !important;
  margin:0 !important;
  box-sizing:border-box !important;
  min-width:0 !important;
  max-width:none !important;
  min-height:0 !important;
  max-height:none !important;
  width:100% !important;
  height:100% !important;
  transform:none !important;

  /* intentionally not !important */
  object-fit:contain;
}

iframe:fullscreen {
  border:none !important;
  padding:0 !important;
}

::backdrop {
  position:fixed;
  top:0; right:0; bottom:0; left:0;
}

*|*:not(:root):fullscreen::backdrop {
  background:black;
}

6. Security and Privacy Considerations

User agents should ensure, e.g. by means of an overlay, that the end user is aware something is displayed fullscreen. User agents should provide a means of exiting fullscreen that always works and advertise this to the user. This is to prevent a site from spoofing the end user by recreating the user agent or even operating system environment when fullscreen. See also the definition of requestFullscreen().

To enable content in a nested browsing context to go fullscreen, it needs to be specifically allowed via the allowfullscreen attribute of the HTML iframe element. This prevents e.g. content from third parties to go fullscreen without explicit permission.

References

Normative References

[CSS]
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/CSS2
[DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[INFRA]
Anne van Kesteren; Domenic Denicola. Infra Standard. Living Standard. URL: https://infra.spec.whatwg.org/
[MATHML]
Patrick D F Ion; Robert R Miner. Mathematical Markup Language (MathML) 1.01 Specification. 7 July 1999. REC. URL: https://www.w3.org/TR/MathML/
[SVG]
Jon Ferraiolo. Scalable Vector Graphics (SVG) 1.0 Specification. 4 September 2001. REC. URL: https://www.w3.org/TR/SVG/
[WEBIDL]
Cameron McCormack; Boris Zbarsky; Tobie Langel. Web IDL. URL: https://heycam.github.io/webidl/

Acknowledgments

Many thanks to Robert O’Callahan for designing the initial model and being awesome.

Thanks to Andy Earnshaw, Chris Pearce, Darin Fisher, fantasai, Giuseppe Pascale, Glenn Maynard, Ian Hickson, Ignacio Solla, João Eiras, Josh Soref, Matt Falkenhagen, Mihai Balan, Mounir Lamouri, Øyvind Stenhaug, Pat Ladd, Philip Jägenstedt, Rafał Chłodnicki, Riff Jiang, Rune Lillesveen, Sigbjørn Vik, Simon Pieters, Tab Atkins, Takayoshi Kochi, Theresa O’Connor, Vincent Scheib, and Xidorn Quan for also being awesome.

This standard is written by Anne van Kesteren (Mozilla, annevk@annevk.nl). Tantek Çelik (Mozilla, tantek@cs.stanford.edu) sorted out legal hassles.

Per CC0, to the extent possible under law, the editor has waived all copyright and related or neighboring rights to this work.