HTML DOM requestFullscreen() Method
Example
Show a <video> element in fullscreen mode:
/* Get the element you want displayed in fullscreen mode (a video in this
example): */
var elem = document.getElementById("myvideo");
/* When
the openFullscreen() function is executed, open the video in fullscreen.
Note that we must include prefixes for different browsers, as they don't
support the requestFullscreen property yet */
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen)
{ /* Chrome, Safari and Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen)
{ /* IE/Edge */
elem.msRequestFullscreen();
}
}
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The requestFullscreen() method opens an element in fullscreen mode.
Note: This method requires specific prefixes to work in different browsers (see Browser Support below).
Tip: Use the exitFullscreen() method to cancel fullscreen mode.
Browser Support
The numbers in the table specify the first browser version that fully supports the method. Note: Each browser requires a specific prefix (see parentheses):
Method | |||||
---|---|---|---|---|---|
requestFullscreen() | 15.0 (webkit) | 11.0 (ms) | 9.0 (moz) | 5.1 (webkit) | 12.10 (webkit) |
Syntax
HTMLElementObject.requestFullscreen()
Parameters
None |
Technical Details
Return Value: | No return value |
---|
More Examples
To open the HTML page in fullscreen, use the document.documentElement
instead of document.getElementById("element")
.
In this example, we also use a close function to close the fullscreen:
Example
/* Get the documentElement (<html>) to display the page in fullscreen */
var elem = document.documentElement;
/* View in fullscreen */
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen)
{ /* Chrome, Safari and Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen)
{ /* IE/Edge */
elem.msRequestFullscreen();
}
}
/*
Close fullscreen */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) { /* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen)
{ /* Chrome, Safari and Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { /* IE/Edge */
document.msExitFullscreen();
}
}
Try it Yourself »
You can also use CSS to style the page when it is in fullscreen mode:
Example
/* Chrome, Safari and Opera syntax */
:-webkit-full-screen {
background-color: yellow;
}
/* Firefox syntax */
:-moz-full-screen {
background-color: yellow;
}
/* IE/Edge syntax */
:-ms-fullscreen {
background-color: yellow;
}
/* Standard syntax */
:fullscreen {
background-color: yellow;
}
Try it Yourself »
❮ Element Object