Using the Fullscreen API

Published on March 11, 2020

William Le

Using the Fullscreen API

In this article, we’ll cover the Fullscreen API with ample amounts of code snippets, and interactive demos. It’s a suprisingly easy API to work with!

The Fullscreen API was introduced in 2013, and provides a native way for browsers to enter/exit fullscreen mode. This specification brought a slew of JavaScript APIs, and CSS selectors that we can use to refine this immersive user experience.

Basics of the Fullscreen API

It’s really easy to activate fullscreen mode on the web! Currently, some browsers still require prefixing the requestFullscreen method.

To check which Fullscreen API method is available, you can create a helper function similar to this:

function activateFullscreen(element) {
  if(element.requestFullscreen) {
    element.requestFullscreen();        // W3C spec
  else if (element.mozRequestFullScreen) {
    element.mozRequestFullScreen();     // Firefox
  else if (element.webkitRequestFullscreen) {
    element.webkitRequestFullscreen();  // Safari
  else if(element.msRequestFullscreen) {
    element.msRequestFullscreen();      // IE/Edge

The word “request” in requestFullscreen is due to the fact that browsers don’t have permissions (by default) to activate fullscreen mode.

Exiting fullscreen is pretty easy, but it also requires a bit of browser detection:

function deactivateFullscreen() {
  if(document.exitFullscreen) {
  } else if (document.mozCancelFullScreen) {
  } else if (document.webkitExitFullscreen) {

Using the above function, to activate fullscreen, simply pass the document HTMLElement!

  Go fullscreen!

  Leave fullscreen

See the Pen alligatorio-fullscreen-api-1 by wle8300 (@wle8300) on CodePen.

Using Fullscreen API on unconventional HTMLElements

As you might have guessed, other HTML hlements can go into fullscreen mode too, not just document!

In the demo below, try clicking the buttons to make <h1>, <img>, and <video> go into fullscreen mode:

  Fullscreen #my-text!
  Fullscreen #my-image
  Fullscreen #my-video

<h1 id="my-text">Hello world</h1>
<img id="my-image" src="alligatorio-logo.svg" width="200"/>
<video id="my-video" controls src="big-buck-bunny.mp4" />

See the Pen alligatorio-fullscreen-api-2 by wle8300 (@wle8300) on CodePen.

Properties & Events

There are additional properties that Fullscreen API brings:

  • document.fullScreenEnabled: returns a boolean denoting whether the webpage has permissions to enter fullscreen mode
  • document.fullScreenElement: returns a HTMLElement Node (only when fullscreen is activated)

You’ll also need to detect the browser:

const fullscreenEnabled = document.fullscreenEnabled
  || document.mozFullScreenEnable
  || document.webkitFullscreenEnabled;
const fullscreenElement = document.fullscreenElement
  || document.mozFullScreenElemen
  || document.webkitFullscreenElement;

There’s also an event called fullscreenchange when the user enters/exits fullscreen mode that you can listen to:

const fullscreenElement = document.fullscreenElement
  || document.mozFullScreenElement
  || document.webkitFullscreenElement;

document.addEventListener('fullscreenchange', (event) => {
  if (fullscreenElement) {
    console.log('Entered fullscreen:', document.fullscreenElement);
  } else {
    console.log('Exited fullscreen.');

Styling Fullscreen Elements

On top of the JavaScript API that’s available, there’s also a few CSS pseudo-classes you can use:

/* Targets the
  HTML element that's
  in fullscreen mode */
:-ms-fullscreen {
  /* ... */

/* Styling the
  backdrop */
::backdrop {
  /* ... */

Here’s an example where we add a groovy background-color, and opacity rules to the backdrop:

::backdrop {
  opacity: 0.8;
  background: #DFA612;

See the Pen alligatorio-fullscreen-api-3 by wle8300 (@wle8300) on CodePen.

Try clicking the button! You can read more about :fullscreen and :backdrop on the Mozilla Developer Network.

The W3C specification alternated between “fullscreen” and “full-screen” so you’ll see a discrepancy in older specs, browser prefixes, etc. Going forward browsers will stick with “fullscreen”

Can I Use fullscreen? Data on support for the fullscreen feature across the major browsers from caniuse.com.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
William Le


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel