docs.rodeo

MDN Web Docs mirror

Picture-in-Picture API

{{DefaultAPISidebar("Picture-in-Picture API")}} 

The Picture-in-Picture API allow websites to create a floating, always-on-top video window. This allows users to continue consuming media while they interact with other sites or applications on their device.

[!NOTE] The Document Picture-in-Picture API extends the Picture-in-Picture API to allow the always-on-top window to be populated with any arbitrary HTML content, not just a video.

Interfaces

Instance methods

The Picture-in-Picture API adds methods to the {{DOMxRef("HTMLVideoElement")}}  and {{DOMxRef("Document")}}  interfaces to allow toggling of the floating video window.

Instance methods on the HTMLVideoElement interface

Instance methods on the Document interface

Instance properties

The Picture-in-Picture API augments the {{DOMxRef("HTMLVideoElement")}} , {{DOMxRef("Document")}} , and {{DOMxRef("ShadowRoot")}}  interfaces with properties that can be used to determine if the floating video window mode is supported and available, if picture-in-picture mode is currently active, and which video is floating.

Instance properties on the HTMLVideoElement interface

Instance properties on the Document interface

Instance properties on the Document or ShadowRoot interfaces

Events

The Picture-in-Picture API defines three events, which can be used to detect when picture-in-picture mode is toggled and when the floating video window is resized.

Adding Controls

If media action handlers have been set via the Media Session API, then appropriate controls for those actions will be added by the browser to the picture-in-picture overlay. For example, if a "nexttrack" action has been set, then a skip button might be displayed in the picture-in-picture view. There is no support for adding custom HTML buttons or controls.

Controlling styling

The :picture-in-picture CSS pseudo-class matches the video element currently in picture-in-picture mode, allowing you to configure your stylesheets to automatically adjust the size, style, or layout of content when a video switches back and forth between picture-in-picture and traditional presentation modes.

Controlling access

The availability of picture-in-picture mode can be controlled using Permissions Policy. The picture-in-picture mode feature is identified by the string "picture-in-picture", with a default allowlist value of *, meaning that picture-in-picture mode is permitted in top-level document contexts, as well as to nested browsing contexts loaded from the same origin as the top-most document.

Examples

Toggling picture-in-picture mode

In this example, we have a {{HTMLElement("video")}}  element in a web page, a {{HTMLElement("button")}}  to toggle picture-in-picture, and an element to log information relevant for the example. The {{HTMLElement("button")}}  element is disabled initially until we’ve determined browser support.

<video
  src="/shared-assets/videos/friday.mp4"
  id="video"
  muted
  controls
  loop
  width="300"></video>

<button id="pip-button" disabled>Toggle PiP</button>
<pre id="log"></pre>
body {
  font:
    14px "Open Sans",
    sans-serif;
  padding: 0.5em;
}

button {
  display: block;
  margin-block: 1rem;
}

We first check if the browser supports PiP with document.pictureInPictureEnabled, and if it’s not supported, we log that information to the <pre> element. If it is available in the browser, we can enable the toggle to enter and exit PiP.

For the controls, an event listener on the {{HTMLElement("button")}}  element calls a togglePictureInPicture() function that we’ve defined. In togglePictureInPicture(), an if statement checks the value of the {{DOMxRef("Document", "document")}} 's pictureInPictureElement attribute.

const video = document.getElementById("video");
const pipButton = document.getElementById("pip-button");
const log = document.getElementById("log");

if (document.pictureInPictureEnabled) {
  pipButton.removeAttribute("disabled");
} else {
  log.innerText = "PiP not supported. Check browser compatibility for details.";
}

function togglePictureInPicture() {
  if (document.pictureInPictureElement) {
    document.exitPictureInPicture();
  } else {
    video.requestPictureInPicture();
  }
}

pipButton.addEventListener("click", togglePictureInPicture);
:picture-in-picture {
  outline: 5px dashed green;
}

Clicking the “Toggle PiP” button lets the user toggle between playing the video in the page and in a floating window:

{{embedlivesample("toggling_picture-in-picture", , "350")}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN