files/en-us/web/api/element/requestfullscreen/index.md
{{APIRef("Fullscreen API")}}
The Element.requestFullscreen()
method issues an asynchronous request to make the element be displayed in fullscreen
mode.
It's not guaranteed that the element will be put into full screen mode. If permission to enter full screen mode is granted, the returned {{JSxRef("Promise")}} will resolve and the element will receive a {{domxref("Element/fullscreenchange_event", "fullscreenchange")}} event to let it know that it's now in full screen mode. If permission is denied, the promise is rejected and the element receives a {{domxref("Element/fullscreenerror_event", "fullscreenerror")}} event instead. If the element has been detached from the original document, then the document receives these events instead.
requestFullscreen()
requestFullscreen(options)
options {{optional_inline}}
navigationUI {{optional_inline}}
"auto", which indicates that the browser should decide what to do.
"hide"
"show"
"auto"
screen {{optional_inline}} {{experimental_inline}}
A {{JSxRef("Promise")}} which is resolved with a value of undefined when
the transition to full screen is complete.
Rather than throw a traditional exception, the requestFullscreen()
procedure announces error conditions by rejecting the Promise it has
returned. The rejection handler receives one of the following exception values:
TypeError exception may be delivered in any of the following
situations:
fullscreen feature,
either because of Permissions Policy configuration or other access control features.Transient user activation is required. The user has to interact with the page or a UI element in order for this feature to work.
An element that you wish to place into fullscreen mode has to meet a small number of simple requirements:
allowfullscreen
attribute applied to it.Additionally, any set Permissions Policies must allow the use of this feature.
You can determine whether or not your attempt to switch to fullscreen mode is
successful by using the {{jsxref("Promise")}} returned by
requestFullscreen(), as seen in the examples below.
To learn when other code has toggled fullscreen mode on and off, you should establish
listeners for the {{domxref("Document/fullscreenchange_event", "fullscreenchange")}} event on the {{domxref("Document")}}.
It's also important to listen for fullscreenchange to be aware when, for
example, the user manually toggles fullscreen mode, or when the user switches
applications, causing your application to temporarily exit fullscreen mode.
This example toggles the {{HTMLElement("video")}} element in and out of fullscreen mode when the <kbd>Enter</kbd> or <kbd>Shift</kbd> + <kbd>F</kbd> keys are pressed.
The script checks whether the document is currently in fullscreen using {{domxref("document.fullscreenElement")}}.
If the document is in fullscreen, it calls {{domxref("document.exitFullscreen()")}} to exit.
Otherwise, it calls requestFullscreen() on the <video> element:
const video = document.querySelector("video");
document.addEventListener("keydown", (event) => {
// Note that "F" is case-sensitive (uppercase):
if (event.key === "Enter" || event.key === "F") {
// Check if we're in fullscreen mode
if (document.fullscreenElement) {
document.exitFullscreen();
return;
}
// Otherwise enter fullscreen mode
video.requestFullscreen().catch((err) => {
console.error(`Error enabling fullscreen: ${err.message}`);
});
}
});
<p>
The video element below shows a time-lapse of a flower blooming. You can
toggle fullscreen on and off using <kbd>Enter</kbd> or <kbd>Shift</kbd> +
<kbd>F</kbd> (uppercase "F"). The embedded document needs to have
<a
href="https://developer.mozilla.org/en-US/docs/Web/API/Element/focus_event">
focus
</a>
for the example to work.
</p>
<video controls loop src="/shared-assets/videos/flower.mp4" width="420"></video>
body {
font-family:
"Benton Sans", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
margin: 2em;
}
video::backdrop {
background-color: #444488;
}
button {
display: block;
}
kbd {
border: 2px solid #cdcdcd;
border-radius: 3px;
box-shadow: inset 0 -1px 0 0 #cdcdcd;
font-size: 0.825rem;
padding: 0.25rem;
}
{{embedlivesample("requesting_fullscreen_mode", , "400", "", "", "", "fullscreen")}}
In this example, the entire document is placed into fullscreen mode by calling
requestFullscreen() on the document's
{{DOMxRef("Document.documentElement")}}, which is the document's root
{{HTMLElement("html")}} element.
let elem = document.documentElement;
elem
.requestFullscreen({ navigationUI: "show" })
.then(() => {})
.catch((err) => {
alert(
`An error occurred while trying to switch into fullscreen mode: ${err.message} (${err.name})`,
);
});
The promise's resolve handler does nothing, but if the promise is rejected, an error message is displayed by calling {{DOMxRef("Window.alert", "alert()")}}.
If you wanted to make the element fullscreen on the primary OS screen, you could use code like the following:
try {
const primaryScreen = (await getScreenDetails()).screens.find(
(screen) => screen.isPrimary,
);
await document.body.requestFullscreen({ screen: primaryScreen });
} catch (err) {
console.error(err.name, err.message);
}
The {{domxref("Window.getScreenDetails()")}} method is used to retrieve the {{domxref("ScreenDetails")}} object for the current device, which contains {{domxref("ScreenDetailed")}} objects representing the different available screens.
{{Specifications}}
{{Compat}}
allowfullscreen