Fullscreen API

Living Standard — Last Updated 27 June 2014

This Version:
http://fullscreen.spec.whatwg.org/
Participate:
Send feedback to whatwg@whatwg.org (archives) or file a bug (open bugs)
IRC: #whatwg on Freenode
Version History:
https://github.com/whatwg/fullscreen/commits
@FullscreenAPI
Editors:
Anne van Kesteren (Mozilla) <>
Tantek Çelik (Mozilla) <>

Abstract

Fullscreen defines the fullscreen API for the web platform.

Table of Contents

  1. 1 Conformance
  2. 2 Terminology
  3. 3 Model
  4. 4 API
  5. 5 UI
  6. 6 Rendering
    1. 6.1 New stacking layer
    2. 6.2 ::backdrop pseudo-element
    3. 6.3 :fullscreen pseudo-class
    4. 6.4 User-agent level style sheet defaults
  7. 7 Security and Privacy Considerations
  8. References
  9. Acknowledgments

1 Conformance

All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.

The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this specification are to be interpreted as described in RFC2119. For readability, these words do not appear in all uppercase letters in this specification. [RFC2119]

2 Terminology

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

The term context object means the object on which the method or attribute being discussed was called. When the context object is unambiguous, the term can be omitted.

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.

3 Model

The task source used by this specification is the user interaction task source.

All documents have an associated fullscreen enabled flag and fullscreen element stack. Unless otherwise stated the fullscreen enabled flag is unset and the fullscreen element stack is an initially empty ordered set.

HTML defines the exact conditions under which the fullscreen enabled flag is set. A document from a top-level browsing context will have it set. For a nested browsing context it is only set if the iframe element that is responsible for creating the nested browsing context has its allowfullscreen attribute set. That attribute is defined by HTML as well. [HTML]

To add an element on a document's fullscreen element stack, add, or move if already present, it on top of document's browsing context's top layer, and then add, or move if already present, it on top of document's fullscreen element stack.

To remove an element from a document's fullscreen element stack, remove it from document's browsing context's top layer, and then remove it from document's fullscreen element stack.

To empty a document's fullscreen element stack, remove all the elements in it from document's browsing context's top layer, and then empty document's fullscreen element stack.


To fully exit fullscreen act as if the exitFullscreen() method was invoked on the top-level browsing context's document and subsequently empty that document's fullscreen element stack.

Since a fullscreenchange event will be dispatched regardless, emptying the fullscreen element stack per above is fine.

HTML invokes the fully exit fullscreen algorithm for navigation. [HTML]

If an element at the top of a fullscreen element stack is removed from a document, fully exit fullscreen.

4 API

partial interface Element {
  void requestFullscreen();
};

partial interface Document {
  readonly attribute boolean fullscreenEnabled;
  readonly attribute Element? fullscreenElement;

  void exitFullscreen();

  attribute EventHandler onfullscreenchange;
  attribute EventHandler onfullscreenerror;
};
element . requestFullscreen()

Displays element fullscreen.

document . fullscreenEnabled

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

document . fullscreenElement

Returns the element that is displayed fullscreen, or null if there is no such element.

document . exitFullscreen()

Stops any elements within document from being displayed fullscreen.

To go fullscreen with an element element, a document document, and a top-level document flag, run these steps:

  1. If top-level document flag is set and document's fullscreen element stack is empty, resize its viewport to match the dimensions of the screen and optionally display a message indicating how the user can resize the viewport back to its "normal" dimensions.

  2. Add element on document's fullscreen element stack and fire an event named fullscreenchange on document.

The requestFullscreen() method must run these steps:

  1. If any of the following conditions are true, queue a task to fire an event named fullscreenerror on the context object's node document, and then terminate these steps:

  2. Return, and run the remaining steps asynchronously.

  3. Optionally, perform some animation.

  4. Let doc be element's node document.

  5. Let docs be all doc's ancestor browsing context's documents (if any) and doc, in order of furthest away from doc to doc.

  6. For each document in docs, in order, run these substeps:

    1. Let top-level document flag be set if document is the first document in docs, and unset otherwise.

    2. Let following document be the document after document in docs, or null if there is no such document.

    3. If following document is null, queue a task to go fullscreen with context object, document, and top-level document flag if set.

    4. Otherwise, if document's fullscreen element stack is either empty or its top element is not following document's browsing context container, queue a task to go fullscreen with following document's browsing context container, document, and top-level document flag if set.

    5. Otherwise, do nothing for document. It stays the same.

The fullscreenEnabled attribute must return true if the context object and all ancestor browsing context's documents have their fullscreen enabled flag set, or false otherwise.

The fullscreenElement attribute must return the top element of the context object's fullscreen element stack, or null otherwise.

The exitFullscreen() method must run these steps:

  1. Let doc be the context object.

  2. If doc's fullscreen element stack is empty, terminate these steps.

  3. Return, and run the remaining steps asynchronously.

  4. Optionally, perform some animation.

  5. Let descendants be all the doc's descendant browsing context's documents with a non-empty fullscreen element stack (if any), ordered so that the child of the doc is last and the document furthest away from the doc is first.

  6. For each descendant in descendants, queue a task to empty descendant's fullscreen element stack and fire an event named fullscreenchange on descendant.

  7. While doc is not null, run these substeps:

    1. Remove the top element of doc's fullscreen element stack.

      If doc's fullscreen element stack is non-empty and the element now at the top is either not in a document or its node document is not doc, run these substeps again.

      This is needed because a non-top element in the fullscreen element stack could have been removed from doc.

    2. Let lastDoc be doc.

    3. If doc's fullscreen element stack is empty and doc's browsing context has a browsing context container, set doc to that browsing context container's node document, and to null otherwise.

    4. Queue a task to run these subsubsteps:

      1. If doc is null, resize lastDoc's viewport to its "normal" dimensions.

      2. Fire an event named fullscreenchange on lastDoc.


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

5 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.

6 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.

6.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 browsing context 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.

An element in a top layer has the following characteristics:

6.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. This pseudo-element box has the same characteristics as any element box in the 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.

6.3 :fullscreen pseudo-class

The :fullscreen pseudo-class must match the top element of the document's fullscreen element stack (if any).

6.4 User-agent level style sheet defaults

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

*|*:not(:root):fullscreen {
  position:fixed;
  top:0; right:0; bottom:0; left:0;
  margin:0;
  box-sizing:border-box;
  width:100%;
  height:100%;
  object-fit:contain;
}

iframe:fullscreen {
  border:none;
}

*|*:fullscreen::backdrop {
  position:fixed;
  top:0; right:0; bottom:0; left:0;
  background:black;
}

7 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

[CSS]
CSS, Bert Bos, Tantek Çelik, Ian Hickson et al.. W3C.
[DOM]
DOM, Anne van Kesteren, Aryeh Gregor and Ms2ger. WHATWG.
[HTML]
HTML, Ian Hickson. WHATWG.
[RFC2119]
Key words for use in RFCs to Indicate Requirement Levels, Scott Bradner. IETF.
[WEBIDL]
Web IDL, Cameron McCormack. W3C.

Acknowledgments

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

Thanks to Chris Pearce, Darin Fisher, Edward O'Connor, fantasai, Giuseppe Pascale, Glenn Maynard, Ian Hickson, João Eiras, Josh Soref, Matt Falkenhagen, Øyvind Stenhaug, Pat Ladd, Philip Jägenstedt, Rafał Chłodnicki, Rune Lillesveen, Sigbjørn Vik, Simon Pieters, Tab Atkins, and Vincent Scheib for also being awesome.